【问题标题】:Installing GraphQL iOS Framework安装 GraphQL iOS 框架
【发布时间】:2019-01-20 05:10:14
【问题描述】:

我想将 apollo 框架添加到我的 iOS 项目中。我正在关注documentation,但无法构建它。

  1. 我通过 cocoapods 安装 apollo
  2. 我向我的目标添加了一个代码生成构建步骤(将它放在编译源之上...)
  3. 我通过 apollo 模式下载模式:下载 --endpoint=http://localhost:4000/graphqlschema.json。我将 schema.json 文件添加到项目的根目录中。
  4. 我构建它并收到以下错误:

    • apollo@1.2.0 在 20.385 秒内增加了 416 个包 ++ exec /Users/PATH/node_modules/.bin/apollo codegen:generate --queries= --schema=schema.json API.swift › 警告:从 1.2.0 到 1.7.0 的 apollo 更新可用 › 错误:标志 --queries 需要一个值
      命令 /bin/sh 失败,退出代码为 2

我的 schema.grapqhl 文件类似于:

type Query {
  """ User """
  users: [User!]!
  user(username: String!): User

  """ Question """
  questions: [Question!]!
  question(id: Int!): Question

  }

  type Mutation {
  """ User """
    createUser(username: String!): User
    updateUser(username: String!, newUsername: String!): [Int!]!
    deleteUser(username: String!): Int!

  """ Question """
    createQuestion(text: String!, category: Int!, active: Boolean!): 
    Question
    updateQuestion(id: Int!, text: String!, category: Int!, active: Boolean!): [Int!]!
    deleteQuestion(id: Int!): Int!
 }

 type Subscription {
   answerAdded: Answer
 }

 type Question {
  id: Int!
  text: String!
  categoryId: QuestionCategory!
  active: Boolean!
  createdAt: String!
  updatedAt: String!
  }
  ......

查询参数似乎没有输入: apollo codegen:generate --queries= --schema=schema.json API.swift

如果我自己通过终端尝试一下

apollo codegen:generate --queries=schema.graphql --schema=schema.json API.swift

我收到以下错误:

Warning: apollo update available from 1.2.0 to 1.7.0
.../schema.graphql: The Query definition is not executable.
.../schema.graphql: The Mutation definition is not executable.
.../schema.graphql: The Subscription definition is not executable.
.../schema.graphql: The Question definition is not executable.
... some more ...
:heavy_check_mark: Scanning for GraphQL queries (1 found)
:heavy_check_mark: Loading GraphQL schema
:heavy_check_mark: Parsing GraphQL schema
:heavy_multiplication_x: Generating query files with 'swift' target
 → Validation of GraphQL query document failed
ToolError: Validation of GraphQL query document failed
at Object.validateQueryDocument 
....

能否请您帮忙找出我无法构建该项目的原因?是不是因为我的 schema.graphl 文件不对?

【问题讨论】:

    标签: ios graphql apollo


    【解决方案1】:

    感谢开放的 Apollo Slack iOS 频道上的 Martijn Walraven 我解决了它。

    问题是我的 schema.graphql 文件有误。

    您的schema.graphql 文件是您的架构在架构定义语言中的表示,而不是查询文档。 apollo ios 希望您在 .graphql 文件中定义您的应用程序使用的查询,以便它可以从中生成代码。 这些是客户端发送到服务器的查询,类似于你在 graphiql 中的查询

    ... 查询需要命名。所以如果你把它放在.graphql 文件中:

    query AllQuestions {
      queries {
        text
      }
    }
    

    这将生成一个AllQuestionsQuery swift 类,您可以将其传递给client.fetch(query:) 它还将生成具有您请求的确切字段的结果类型(已编辑) 例如,您可以访问text,但不能访问active 因此,与全局模型类型相比,特定于查询的类型为您的数据获取提供了完整的类型安全

    【讨论】:

    【解决方案2】:

    正如@Sergej Birklin 回答的那样,我想与您分享另一个问题 -

    Profile.graphql

    query MyProfile{
        player {
            me {
                id
                secret
                name
                email
                state
                country
                timezone
                picture
                pictureType
                audio
                rank
            }
        }
    }
    

    我们只是在查询我的个人资料信息(我是 玩家 类型),查询将返回我需要的信息,例如我的 ID、姓名、电子邮件、状态等.

    正如您所说,它将创建 MyProfileQuery

    希望对你有所帮助??

    #swith #GraphQL #iOS

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 2021-07-02
      • 2012-08-02
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2011-01-05
      相关资源
      最近更新 更多