在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):florent37/Android-OkGraphQl开源软件地址(OpenSource Url):https://github.com/florent37/Android-OkGraphQl开源编程语言(OpenSource Language):Java 99.7%开源软件介绍(OpenSource Introduction):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 DownloadIn your module compile 'com.github.florent37:okgraphql:(last version)'
//dependencies
compile 'io.reactivex.rxjava2:rxjava:2.1.9'
compile 'com.squareup.okhttp3:okhttp:3.9.1'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.florent37:android-nosql:1.0.0' CreationFirst, initialize you OkGraphQl client with
OkGraphql okGraphql = new OkGraphql.Builder()
.okClient(okHttpClient)
.baseUrl("http://192.168.1.16:8888/graphql")
.converter(new GsonConverter(new Gson()))
.build(); UsagePlease note that following examples have been tested on the StarWars GraphQl server : https://github.com/apollographql/starwars-server Create your GraphQl query with 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 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 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 BuilderUse 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 :
MutationsMutations 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());
}); FragmentsUse 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 CreditsAuthor: Florent Champigny Fiches Plateau Moto : https://www.fiches-plateau-moto.fr/ License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论