ios graphql_在iOS中执行GraphQL查询的最简单方法

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)

Image for post
Mel on 梅尔(Mel)摄于 Unsplash Unsplash

There are three types of GraphQL operation: query, mutation, and subscription.

GraphQL操作共有三种类型: querymutationsubscription.

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检索数据。 可以通过GETPOST 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编码的主体。

JSON-Encoded body format of GraphQL POST request by Eric Yang
Eric Yang的GraphQL POST请求的JSON编码主体格式

Let’s start with the query string.

让我们从query字符串开始。

Image for post
Photo by Maarten Deckers on Unsplash
Maarten DeckersUnsplash上的 照片

2.查询字符串 (2. Query String)

Query string of the body by Eric Yang
通过Eric Yang查询正文
  • 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 type Filter. 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获得的形状相同。

Image for post
Photo by Jill Heyer on Unsplash
吉尔·海耶 ( Jill Heyer)Unsplash

3.可编码变量 (3. Encodable Variables)

As docs of Star War GraphQL API state, the augments of the query allFilms are:

作为Star War GraphQL API状态的文档,查询allFilms的增强是:

allFilms query by Star War GraphQL API
通过Star War GraphQL API查询allFilms

They are both optional. We use Filter here for the purpose of demonstration.

它们都是可选的。 我们在这里使用Filter进行演示。

Variable of the body by Eric Yang
身体的变化,Eric Yang

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.

FilterVariable结构采用Encodable协议来准备JSON编码的主体。

Image for post
Photo by Samantha Gades on Unsplash
萨曼莎·加德斯 ( Samantha Gades)Unsplash

4.构建HTTP请求 (4. Build the HTTP Request)

Let’s break this a bit:

让我们打破一点:

  • The struct Payload contains variables and query. It also adopts Encodable protocol.

    Payload结构包含variablesquery 。 它还采用可Encodable协议。

  • The request is an instance of URLRequest 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 the request.

    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 field Content-Type and Accept.

    我们将application/json用于Http Header字段Content-TypeAccept

  • The request is simply fired with the shared instance of URLSession.

    只需使用URLSession的共享实例触发该request

  • Data responded is in JSON format and can be deserialized by using JSONSerialization.

    响应的数据为JSON格式,可以使用JSONSerialization反序列JSONSerialization

Image for post
Photo by Anton Sukhinov on Unsplash
Anton SukhinovUnsplash上的 照片

结论 (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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值