【问题标题】:How to read results using Apollo Client for iOS如何使用 Apollo Client for iOS 读取结果
【发布时间】:2018-05-15 02:38:42
【问题描述】:

我正在尝试通过 Apollo 客户端在 iOS 中使用 GraphQL。我有以下突变:

login(user: String!, password: String!): UserType

UserType 如下所示:

id: ID
user: String!
password: String!
name: String
lastname: String
email: Email!
groups: [GroupType]

在 iOS 中,我按照文档所述配置了 aopllo 客户端,并且运行良好,但我不知道如何访问响应中的每个字段。当登录成功时,我想读取我收到的 json 作为 UserType 字段的响应,所以,我正在这样做:

apolloClient.perform(mutation: loginMutation) {
                resultsGQL, error in
...
}

我的问题是,如何从 resultGQL 中读取属于我的 grapql 架构中定义的 UserType 数据的每个字段?

问候

【问题讨论】:

    标签: ios swift graphql apollo-client apollo-ios


    【解决方案1】:

    这个问题不是 100% 清楚的,因为它缺少一些代码你的突变:一个 GraphQL 突变必须返回至少一个值,你必须定义它。因为我不确定你的方法

    login(user: String!, password: String!): UserType
    

    我给你一个简单的例子,用 GraphQL 突变更新现有的 userProfile,然后返回在你的模式中为 userType 定义的每个字段。 让我们假设您有一个 userType(因此知道相应的 userId)并且您想修改电子邮件:

    mutation updateUserProfile($id: ID!, $newEmail: String) {
    updateUser(id: $id, email: $newEmail) {
        id
        user
        password
        name
        lastName
        email 
       }
    }
    

    如你所见,执行后

    updateUser(id: $id, email: $newEmail)
    

    所有返回值都在以下 {...} 括号内定义,因此可以在回调变量中访问

    resultsGQL
    

    这意味着:

    apolloClient.perform(mutation: loginMutation) { resultsGQL, error in
        if let results = resultsGQL?.data {
          // "results" can now access all data from userType
      }
    }
    

    由于您定义了要从突变返回的 userType 架构的所有实体,您现在可以在回调变量中访问它们。

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 2018-01-20
      • 2021-02-13
      • 1970-01-01
      • 2019-06-08
      • 2018-09-05
      • 2020-10-24
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多