【问题标题】:Are array types incorrect when generating React/Apollo hooks with graphql-code-generator?使用 graphql-code-generator 生成 React/Apollo 钩子时,数组类型是否不正确?
【发布时间】:2021-02-22 16:52:16
【问题描述】:

我想知道在 graphql 查询/突变中定义的数组(可以是有效的 graphql 实现中的单例或元组)是否被 graphql-codegen 转换为严格的数组类型。这是设计的吗?

鉴于此架构:

schema {
  query: Query
}

input CustomFieldFilterInput {
  id: ID
  value: String
}

type Query {
  me: User!
  users(ids: [ID], customFields: [CustomFieldFilterInput]): [User]
}

type CustomField {
  id: ID
  value: String
}

interface Node {
  id: ID!
}

type User implements Node {
  id: ID!
  username: String!
  email: String!
  customfields: [CustomField]
}

这个查询:

query findUserOrUsers($userIds: [ID], $customFields: [CustomFieldFilterInput]) {
  users(ids: $userIds, customFields: $customFields) {
    ...UserFields
  }
}
fragment UserFields on User {
  id
  username
}

graphql-code-generator 将生成:

export type FindUserOrUsersQueryVariables = Exact<{
  userIds?: Maybe<Array<Maybe<Scalars['ID']>>>;
  customFields?: Maybe<Array<Maybe<CustomFieldFilterInput>>>;
}>;

但是在我见过的(大多数)graphql 实现中,放入一个数组或单个值是有效的:

query findUsersString {
  users(ids: 123, customFields: { value: "123", id: 123 }) {
    ...UserFields
  }
}

query findUsersArray {
  users(ids: [123], customFields: [{ value: "123", id: 123 }]) {
    ...UserFields
  }
}

我错过了什么吗?有没有办法覆盖它?

这是一个重现“问题”的小型 repo:https://github.com/kweestian/arrays-gql-gen

【问题讨论】:

    标签: typescript graphql react-apollo graphql-codegen


    【解决方案1】:

    操作中的 GraphQL 输入支持强制,但 codegen 不支持(请参阅:https://github.com/dotansimha/graphql-code-generator/issues/4888) 我们正在努力并希望在即将发布的版本中解决这个问题;)

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2020-02-13
      • 2020-03-25
      • 2022-08-18
      • 2019-12-31
      • 2021-03-11
      • 2021-06-14
      • 2022-07-31
      • 2020-03-22
      相关资源
      最近更新 更多