简体   繁体   中英

Are array types incorrect when generating React/Apollo hooks with graphql-code-generator?

I am wondering if arrays defined in graphql queries/mutations, which can be either singletons or tuples in valid graphql implementation get converted to strict array types by graphql-codegen. Is this is by design ?

Given this schema:

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]
}

And this query:

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

graphql-code-generator will generate:

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

But putting in an array or a single value is valid in (most) graphql implementations I've seen:

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

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

Am I missing something ? Is there a way to override this ?

Here is a small repo that reproduces the 'issue': https://github.com/kweestian/arrays-gql-gen

操作中的 GraphQL 输入支持强制转换,但 codegen 不支持(参见: https : //github.com/dotansimha/graphql-code-generator/issues/4888 )我们正在研究它并希望在即将推出的版本中修复它;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM