【发布时间】: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.json或some_other_name.json。 2. 然后将schema.json或some_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