OkGraphQl


Source link: https://github.com/florent37/OkGraphQl

GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data. A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type.

Learn more at : http://graphql.org/learn/

OkGraphQl is an Android client implementation, thinked to be easy to use, fluent, and completely modular

Download

In your module

compile 'com.github.florent37:okgraphql:0.0.1'  //dependencies compile 'com.squareup.okhttp3:okhttp:3.8.1' compile 'io.reactivex.rxjava2:rxjava:2.1.0' compile 'com.google.code.gson:gson:2.8.0' compile 'com.github.florent37:android-nosql:1.0.0'

Creation

First, initialize you OkGraphQl client with

  • The GraphQl sever url
  • An OkHttpClient (optional)
  • A converter (optional)
OkGraphql okGraphql = new OkGraphql.Builder()

  .okClient(okHttpClient)

  .baseUrl("http://192.168.1.16:8888/graphql")

  .converter(new GsonConverter(new Gson()))

  .build();

Usage

Please note that following examples have been tested on the StarWars GraphQl server : https://github.com/apollographql/starwars-server

Create your GraphQl query with query(string), then execute with enqueue(SuccessCallback, ErrorCallback)

By default the success response is the Json (as String)

okGraphql

.query("{
" +

  "  hero {
" +

  "
 name" +

  "  
}
" +

  "
}
"

)

.enqueue(responseString -> {

 //play with your responseString

}
, error -> {

 //display the error

}
);

You can also inflate a POJO with the returned Json using .cast(CLASS) This will use the converter assigned to your OkGraphQl

class Character {

String name; 
}

  class StarWarsResponse {

 Character hero;

}
  okGraphql

.query("{
" +

  "  hero {
" +

  "
 name" +

  "  
}
" +

  "
}
"

)

.cast(StarWarsResponse.class) //will automatically cast the data json to

 .enqueue(response -> {

 //play with your StarWarsResponse

}
, error -> {

 //display the error

}
);

RxJava !

You can also use RxJava methods to your query using .toSingle()

okGraphql

 .query(

"Hero($episode: Episode, $withFriends: Boolean!) {
" +

  "  hero(episode: $episode) {
" +

  "
 name" +

  "
 friends @include(if: $withFriends) {
" +

  "

name" +

  "
 
}
" +

  "  
}
"

 )

  .variable("episode", "JEDI")

 .variable("withFriends", false)

  .cast(StarWarsResponse.class) //will automatically cast the data json to

  .toSingle()

 .observeOn(AndroidSchedulers.mainThread())

 .subscribe(

data -> textView.setText(data.toString()),

throwable -> textView.setText(throwable.getLocalizedMessage())

 );

Query Builder

Use Fields Builders instead of Strings to create dynamically your queries

okGraphql

  .query(newField()

.field(newField("human").argument("id: \"1000\"")

  .field("name")

  .field("height")

)

 )

  ...

This example will generate the query :

{

human(id: "1000") {

  name
  height

}
 
}
 

Mutations

Mutations are easy to execute with OkGraphQl

 okGraphql

  .mutation("CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
" +

 "  createReview(episode: $ep, review: $review) {
" +

 "
 stars" +

 "
 commentary" +

 "  
}
" +

 "
}
")

  .variable("ep", "JEDI")

  .variable("review", "{
 \"stars\": 5, \"commentary\": \"This is a great movie!\"")

  .enqueue(responseString -> {

text1.setText(responseString);

  
}
, error -> {

text1.setText(error.getLocalizedMessage());

  
}
);

Fragments

Use .fragment(code) to append a fragment on your request

okGraphql

.body("{
" +

 "  leftComparison: hero(episode: EMPIRE) {
" +

 "
 ...comparisonFields" +

 "  
}
" +

 "  rightComparison: hero(episode: JEDI) {
" +

 "
 ...comparisonFields" +

 "  
}
" +

 "
}
"

  )

.fragment("comparisonFields on Character {
" +

 "  name" +

 "  appearsIn" +

 "  friends {
" +

 "
 name" +

 "  
}
" +

 "
}
")

.enqueue(responseString -> {

query_hero_enqueue.setText(responseString);

  
}
, error -> {

query_hero_enqueue.setText(error.getLocalizedMessage());

  
}
);

Cache

//TODO will use https://github.com/florent37/Android-NoSql

Credits

Author: Florent Champigny http://www.florentchampigny.com/

License

Copyright 2016 florent37, Inc.  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

Triad is a tiny Android library which enables use of the Model-View-Presenter pattern in an easy way. It uses custom Views to replace the dreaded Fragments, and introduces Presenter classes to separate business logic from view logic. Since the Presenters are plain Java objects, tests for these classes can run blazingly fast on a local JVM.

Soot is a Java optimization framework. It provides four intermediate representations for analyzing and transforming Java bytecode:

  • Baf: a streamlined representation of bytecode which is simple to manipulate.
  • Jimple: a typed 3-address intermediate representation suitable for optimization.
  • Shimple: an SSA variation of Jimple.
  • Grimp: an aggregated version of Jimple suitable for decompilation and code inspection.
  • 3D Style PageFlip like iBooks.
  • Implemented by pure Java.
  • Based on OpenGL 2.0.
  • Supports single page and double pages flip.

This tool will remove all the class in R.java except the styleable class and replace the reference into the constant value. So you can reduce the dex files number and apk size.

SQLite is simple and lightweight; it follows that managing SQLite databases on Android should be as well.

Squeaky strives to be a straightforward approach to creating, migrating, and accessing SQLite databases.

A TextView library that allows the user to increase/decrease font size with a two finger gesture by the user.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes