首页 > 解决方案 > Apollo ios codegen 生成可选值

问题描述

我正在尝试使用所有最新版本的 apollo-ios,但我想解决这个挥之不去的问题:我不断获得可选值(见下图)。

代码

这是我探索过的(但仍然找不到whyyy)

当我创建表时,Nullable 为 false。然后,我创建了一个供公众访问的视图。

桌子

看法

使用 apollo schema:download 命令,这是生成的 json:schema.json

使用 graphqurl 命令,这是生成的 schema.graphql:schema.graphql。这是片段。

"""
columns and relationships of "schedule"
"""
type schedule {
  activity: String
  end_at: timestamptz
  id: Int

  """An array relationship"""
  speakers(
    """distinct select on columns"""
    distinct_on: [talk_speakers_view_select_column!]

    """limit the number of rows returned"""
    limit: Int

    """skip the first n rows. Use only with order_by"""
    offset: Int

    """sort the rows by one or more columns"""
    order_by: [talk_speakers_view_order_by!]

    """filter the rows returned"""
    where: talk_speakers_view_bool_exp
  ): [talk_speakers_view!]!
  start_at: timestamptz
  talk_description: String
  talk_type: String
  title: String
}

我怀疑它看起来好像在模式id: Int中丢失!,是 codegen 将其解释为可选的原因。但我也可能是错的。这是完整参考的回购https://github.com/vinamelody/MyApolloTest/tree/test

标签: iosgraphqlapollohasura

解决方案


这是因为 Postgres 出于某种未知原因将视图列标记为显式可为空,而不管底层列的可空性如何。

Vamshi(核心 Hasura 服务器开发人员)在本期中对此进行了解释: https ://github.com/hasura/graphql-engine/issues/1965

不过,您不需要该视图 - 这与执行查询相同:

query {
  talks(
    where: { activity: { _like: "iosconfig21%" } },
    order_by: { start_at: "asc" }
  }) {
    id
    title
    start
    <rest of fields>
}

除了现在,您需要在 Hasura 元数据中管理一个视图,并在它从中选择的表的顶部创建权限,如常规表。反正我的 0.02 美元。

如果您真的坚持在 JSON 响应中将其称为“计划”, 您甚至可以使用 GraphQL 别名https://graphql.org/learn/queries/


推荐阅读