【问题标题】:API.swift file not update : Apollo GraphQL iOSAPI.swift 文件未更新:Apollo GraphQL iOS
【发布时间】:2019-04-14 19:26:33
【问题描述】:

我在 iOS 项目中使用 GraphQL API。我的 .graphql 文件是,

mutation SigninQuery($email: String! , $password: String!) {
    signin(email: $email, password: $password) {
        result {
            token
            firstName
            lastName
            profileImage
        }
    }
}

我的文件按以下顺序排列,

项目

  • ->appDelegate
  • ->info.plist
  • ->schema.json
  • -->Graphql[文件夹]

  • ->API.swift

  • ->ApiFile.graphql

我的 API.swift 文件仍然存在,只有标题

进口阿波罗

。谁能帮我找出解决办法

【问题讨论】:

  • 1.在构建之前更新您的 schema.json。 2. 在 XCode 中正常构建(如果您附加了运行 GraphQL 解析器的 shell 脚本)。它应该更新文件API.swift。根据您的 GraphQL 文件,您应该可以使用名称 SigninQueryMutation(email: , password:) 调用该类
  • @CosmosMan 感谢您的回复,但请您解释一下如何更新 schema.json 文件...
  • 更新的真正意思是重新生成schema.json。我认为您的 XCode 构建阶段缺少一些脚本。 graphql 的工作原理是,1. 您检查 graphql 模式并将其生成为schema.jsonsome_other_name.json。 2. 然后将schema.jsonsome_other_name.json 提供给您的graphql 解析器以解析您的.graphql 文件。如果在 schema.json.graphql 文件之间遗漏了某些内容,它将无法正常工作。 user 中缺少类似 isUserPretty 的内容。如果什么都没发生,它应该可以正常工作。
  • 例如,生成schema.json的脚本如下所示。 apollo-codegen introspect-schema http://www.your-server:8080/graphql --header "Authorization: Bearer some kind of authorization code" --output schema.json.
  • 用于生成API.swift 的脚本如下所示。 APOLLO_FRAMEWORK_PATH="$(eval find $FRAMEWORK_SEARCH_PATHS -name "Apollo.framework" -maxdepth 1)" if [ -z "$APOLLO_FRAMEWORK_PATH" ]; then echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project." exit 1 fi cd "${SRCROOT}/${TARGET_NAME}" $APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema schema.json --output API.swift

标签: ios swift graphql apollo


【解决方案1】:

如果你正确初始化了 apollo 客户端,那么你就不会有这样的问题了。 我使用 Swift 包 安装了 Apollo。我做了以下步骤:

  1. 层次结构

  1. 生成API.swift的脚本(注意要生成的文件夹和模式的名称。在我的例子中,文件夹是GraphQL,名称是tetsTet):李>
    DERIVED_DATA_CANDIDATE="${BUILD_ROOT}"

     while ! [ -d "${DERIVED_DATA_CANDIDATE}/SourcePackages" ]; do
       if [ "${DERIVED_DATA_CANDIDATE}" = / ]; then
         echo >&2 "error: Unable to locate SourcePackages directory from BUILD_ROOT: '${BUILD_ROOT}'"
        exit 1
       fi

  DERIVED_DATA_CANDIDATE="$(dirname "${DERIVED_DATA_CANDIDATE}")"
done

# Grab a reference to the directory where scripts are checked out
SCRIPT_PATH="${DERIVED_DATA_CANDIDATE}/SourcePackages/checkouts/apollo-ios/scripts"

if [ -z "${SCRIPT_PATH}" ]; then
    echo >&2 "error: Couldn't find the CLI script in your checked out SPM packages; make sure to add the framework to your project."
    exit 1
fi

cd "${SRCROOT}/${TARGET_NAME}/GraphQL"
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="tetsTets.json" API.swift
  1. 将生成的API.swift文件拖到你的文件夹中

取消选中“如果需要复制文件”复选框

【讨论】:

  • 有什么区别:apollo codegen:generate "Batuta/models/API.swift" --localSchemaFile="Batuta/schema/schema.json" --includes="./**/ *.graphql" --target="swift"
  • @user3154785 实际上,我不知道:) 但只有这种方式对我有用。
猜你喜欢
  • 2021-08-18
  • 2021-03-03
  • 2019-01-19
  • 2018-03-20
  • 2019-04-27
  • 2021-10-31
  • 2021-12-10
  • 2020-10-13
  • 2017-08-28
相关资源
最近更新 更多