首页 > 解决方案 > 如何将 Hasura/postgres uuid 与 apollo-ios 和 swift 一起使用

问题描述

我在 Hasura (Postgres) 中使用 nativeuuid作为字段数据类型定义了一个表。我的目标是从 Swift 创建一个更新突变,但 uuid 类型并没有从 Postgres 通过 Hasura 和 Apollo 隐式映射到 Swift。Hasura 文档声明 uuid 类型支持使用String,但是当我在更新突变中定义变量时String!

mutation RedeemMagicLinkCode($code: String!, $iosIdentifierForVendor: String!) {
  __typename
  update_entityname(where: {code: {_eq: $code}, iosIdentifierForVendor: {_is_null: true}}, _set: {iosIdentifierForVendor: $iosIdentifierForVendor}) {
    returning {
      id
    }
  }
}

我从 Apollo 代码生成构建步骤中得到一个错误,即Variable "$code" of type "String!" used in position expecting type "uuid".. 当我将其更改uuid!


mutation RedeemMagicLinkCode($code: uuid!, $iosIdentifierForVendor: uuid!) {
  __typename
  update_en...

代码生成步骤完成,生成 API.swift,但出现了几个编译错误实例,Use of undeclared type 'uuid'现在由于 Apollo 生成的 API.swift 编译过程中出现故障。

/// API.swift
...
public let operationName = "RedeemMagicLinkCode"

  public var code: uuid
  public var iosIdentifierForVendor: uuid
...

有人可以为我指出正确的方向,让 Swift 知道什么是 uuid 或定义我的查询,以便我可以通过 Apollo 管理的突变将字符串作为参数传递?非常感谢!

标签: swifthasuraapollo-ios

解决方案


OM(天哪),我可能需要为这个解决方案多么简单而脸红。

我考虑了错误Use of undeclared type 'uuid'以及 Hasura 将接受String此自定义标量的文档...

/// API+typealias.swift
public typealias uuid = String

我将上述类型别名代码单独放在我的 GraphQL 文件所在的文件夹中的一个文件中,瞧,效果很好!

我对现在在全球范围内闲逛的这个公共类型别名并不感到兴奋,但我对工作数据服务感到兴奋


推荐阅读