如何在Android上将GraphQL与Retrofit一起使用? [英] How to use GraphQL with Retrofit on Android?

查看:97
本文介绍了如何在Android上将GraphQL与Retrofit一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GraphQL的新手,但是我已经使用了Retrofit一段时间了,它易于使用且快速.GraphQL在传递数据方面与rest api有很大不同.确实没有太多关于在Android上使用GraphQL的教程,我只能找到此视频( https://www.youtube.com/watch?v=P0uJHI7GjIc&t=1s ),但是里面没有真实的代码.

I'm new to GraphQL but I have been using Retrofit for a while now and its easy to use and fast. GraphQL is much different than rest apis in terms of how you pass data. There really are not too many tutorials out there on using GraphQL with Android, I was only able to find this video (https://www.youtube.com/watch?v=P0uJHI7GjIc&t=1s) but there is no real code in there.

在当前的改造调用代码中,我有一个设置为如下的端点:

In my current code for retrofit calls I have an endpoint I set like so:

final RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(endPoint)
                .build();
        T service = restAdapter.create(clazz);

然后我叫这样的休息服务:

Then I call a rest service like this:

@GET("/users/{login}")
    Observable<Github> getUser(@Path("login") String login);

现在使用GraphQL,您只有基本网址,没有服务路径.另外,如果您要像userId = 1一样进行查询,则必须以带正文参数的Post形式发送:

Now with GraphQL you only have a base url and no service path. Also if you are querying like userId=1 then you have to send as Post with Body parameters:

operationName: arbitrary name ,
query: "{users(userid:$userId){username}}, 
variables: "{"userId":1}"

我只是不确定这将如何转换为翻新

I'm just not sure how this translates to Retrofit

推荐答案

建筑查询&解析GraphQL的响应并不简单.如果您在宠物项目上使用此工具,我建议开始探索Apollo.该客户正在认真开发中,但是您已经可以使用它了.

Building Queries & Parsing responses for GraphQL is not straightforward. If you are using this on pet-projects, i recommend to start exploring Apollo. This client is under serious development but you can already have a look and play with it.

https://github.com/apollographql/apollo-android

到目前为止,我很喜欢与Apollo一起工作,并且他们的路线图上有不错的功能:RxJava集成,改进,订阅和对AutoValue的支持

I enjoyed working with Apollo so far, and there are nice features on their roadmap: RxJava Integration, Retrofit, Subscriptions and support for AutoValue

这篇关于如何在Android上将GraphQL与Retrofit一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆