ios graphql
To communicate with the GraphQL server in the iOS app, there are some third-party libraries, like Apollo iOS client and AWS AppSync. But they also introduce unnecessary code to your project.
为了与iOS应用中的GraphQL服务器通信,有一些第三方库,例如Apollo iOS客户端和AWS AppSync。 但是它们也会为您的项目引入不必要的代码。
For instance, Apollo iOS client normalizes query results to construct a client-side cache by using the SQLite database. AWS AppSync wraps Apollo client with some additional features to handle AWS authentication.
例如,Apollo iOS客户端使用SQLite数据库规范化查询结果以构建客户端缓存。 AWS AppSync为Apollo客户端包装了一些其他功能,以处理AWS身份验证。
I always feel hesitant when binding my project with third-party libraries. Some concerns are:
将项目与第三方库绑定时,我总是会犹豫。 一些问题是:
- How is the security of the library? 图书馆的安全性如何?
- What if the maintainer stops maintaining the library? 如果维护者停止维护库该怎么办?
- How long does it take for the maintainer to fix an issue and release the patch version? 维护者需要花费多长时间来解决问题并发布补丁程序版本?
- Do you tell your clients that you cannot fix the issue because of a third party? 您是否告诉客户您由于第三方无法解决此问题?
Luckily, there is an easy and lightweight way to perform GraphQL by using the raw HTTP Request.
幸运的是,有一种通过使用原始HTTP请求来执行GraphQL的简单轻便的方法。
This article demonstrates how to build it step by step against the Star War GraphQL API.
本文演示了如何针对《 星际大战GraphQL API》逐步构建它。
1.查询操作 (1. Query Operation)
There are three types of GraphQL operation: query
, mutation
, and subscription.
GraphQL操作共有三种类型: query
, mutation
和subscription.
The query
operation is to retrieve data from the API. It can be performed by GET
or POST
HTTP method depending on the GraphQL API.
query
操作是从API检索数据。 可以通过GET
或POST
HTTP方法执行此操作,具体取决于GraphQL API。
For the Star War GraphQL API consumed in this article, it uses POST
method.
对于本文中使用的Star War GraphQL API ,它使用POST
方法。
The following is a sample form for the standard GraphQL POST request. The request uses the application/json
as Content-type
. And the form is its JSON-encoded body.
以下是标准GraphQL POST请求的示例表单。 该请求使用application/json
作为Content-type
。 表单是其JSON编码的主体。
Let’s start with the query
string.
让我们从query
字符串开始。
2.查询字符串 (2. Query String)
- It defines the query string to get films by using the filter. 它定义查询字符串以使用过滤器获取电影。
The keyword
query
at the string defines the operation type you’re intending to do.字符串处的关键字
query
定义了您要执行的操作类型。allFilms
is the operation name. It is not required, but a meaningful and explicit name helps you to identify the operation.allFilms
是操作名称。 它不是必需的,但是有意义的显式名称可以帮助您识别操作。$filter
is one of the variables accepted by the query, followed by the typeFilter
. We can pass a different variable rather than constructing it in our query.$filter
是查询接受的变量之一,其后是Filter
类型。 我们可以传递一个不同的变量,而不是在查询中构造它。The
fields
block describes the specific fields we are asking and has the same shape as what we get from the GraphQL API.fields
块描述了我们要查询的特定字段,并且形状与从GraphQL API获得的形状相同。
3.可编码变量 (3. Encodable Variables)
As docs of Star War GraphQL API state, the augments of the query allFilms
are:
作为Star War GraphQL API状态的文档,查询allFilms
的增强是:
They are both optional. We use Filter
here for the purpose of demonstration.
它们都是可选的。 我们在这里使用Filter
进行演示。
There are also a handful of optional properties of the Filter
. We only pick up director
to simplify the demonstration code.
Filter
还有一些可选属性。 我们仅选择director
来简化演示代码。
The Filter
and Variable
structs adopt Encodable
protocol for preparing the JSON-encoded body.
Filter
和Variable
结构采用Encodable
协议来准备JSON编码的主体。
4.构建HTTP请求 (4. Build the HTTP Request)
Let’s break this a bit:
让我们打破一点:
The struct
Payload
containsvariables
andquery
. It also adoptsEncodable
protocol.Payload
结构包含variables
和query
。 它还采用可Encodable
协议。The
request
is an instance ofURLRequest
built with the Star War API URL: https://swapi.graph.cool.该
request
是使用Star War API URL构建的URLRequest
实例: https : //swapi.graph.cool 。The
payload
is JSON-encoded and added to the body of therequest
.payload
经过JSON编码,并添加到了request
的正文中。POST
method is used as required by the Star War GraphQL API.星球大战GraphQL API要求使用
POST
方法。We use
application/json
for the values of Http Header fieldContent-Type
andAccept
.我们将
application/json
用于Http Header字段Content-Type
和Accept
。The
request
is simply fired with the shared instance ofURLSession
.只需使用
URLSession
的共享实例触发该request
。Data responded is in JSON format and can be deserialized by using
JSONSerialization
.响应的数据为JSON格式,可以使用
JSONSerialization
反序列JSONSerialization
。
结论 (Conclusion)
The article describes the more lightweight way to communicate with GraphQL service. By using the raw HTTP request, there is no need to couple with any third-party libraries. Clean and swift code can be all under the developer’s control.
本文介绍了一种与GraphQL服务进行通信的更轻量级的方法。 通过使用原始HTTP请求,无需与任何第三方库耦合。 干净,快速的代码可以完全在开发人员的控制之下。
I will show you how to perform the GraphQL mutation with raw HTTP Request in the next article. Stay tuned!
在下一篇文章中,我将向您展示如何使用原始HTTP请求执行GraphQL突变。 敬请关注!
翻译自: https://medium.com/better-programming/the-easy-way-to-perform-graphql-queries-in-ios-47b11ea2508e
ios graphql