【问题标题】:All GraphQL Scalars coming up as String (Apollo)所有 GraphQL 标量都以字符串形式出现(Apollo)
【发布时间】:2018-02-11 18:26:08
【问题描述】:

我正在使用 Apollo 客户端进行 GraphQL 客户端集成。我添加了以下运行脚本,官方文档中建议使用。

cd "${SRCROOT}/${TARGET_NAME}/GraphQL/Open"
$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find 
. -name '*.graphql') --schema schema.json 
 --output APIClient.swift

但即将出现的问题是所有标量现在都以字符串形式出现。

例如:- 在登录时,如果我创建了电子邮件和密码的突变,我的架构以 JSON 形式返回响应,而创建的 APIClient 将响应显示为字符串(而不是 JSON)。

因此收到一个错误提示

Apollo.GraphQLResultError(path: ["login", "response"], underlying: Apollo.JSONDecodingError.couldNotConvert

这是因为接收的是String而不是JSON,并且无法将字符串转换为所需的JSON。

有人遇到同样的问题吗?

【问题讨论】:

    标签: ios swift graphql apollo apollo-client


    【解决方案1】:

    所以我想通了。解决方法是添加

    --passthrough-custom-scalars
    

    在运行脚本中。这将传递自定义标量,如 JSON。所以完整的runscript就变成了

    cd "${SRCROOT}/${TARGET_NAME}/GraphQL/Open"
    $APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find 
    . -name '*.graphql') --schema schema.json --passthrough-custom-scalars 
    --output APIOpen.swift
    

    现在,当代码与此运行脚本一起重新编译时,JSON 标量变为有效。

    这花了我很多时间才弄清楚。希望它可以帮助某人并节省他们的时间。谢谢

    【讨论】:

    • 需要明确的是,在 Apollo SDK for iOS 的最新版本中,此标志实际上是 --passthroughCustomScalars。所以你的构建脚本看起来像这样: cd "${SRCROOT}/${TARGET_NAME}" $APOLLO_FRAMEWORK_PATH/check-and-run-apollo-cli.sh codegen:generate --queries="$(find . -name '*.graphql')" --passthroughCustomScalars --schema=schema.json API.swift
    猜你喜欢
    • 2014-05-27
    • 2011-05-23
    • 2022-01-03
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2018-08-27
    • 1970-01-01
    相关资源
    最近更新 更多