graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Generated hook types don't align.

Open vivirancy opened this issue 7 years ago • 2 comments

Describe the bug When generating react hooks, there are many typescript errors throughout the generated file, all caused because the Query type does not match the Query Variables type.

export const EntitiesDocument = gql`
    query Entities($where: WhereEntityInput!) {
        entities(where: $where) {
            ...EntityAllFields
            address {
                name
            }
        }
    }
    ${EntityAllFieldsFragmentDoc}
`;

export const EntitiesComponent = (
    props: Omit<Omit<ReactApollo.QueryProps<EntitiesQuery, EntitiesQueryVariables>, 'query'>, 'variables'> & {
        variables: EntitiesQueryVariables;
    },
) => <ReactApollo.Query<EntitiesQuery, EntitiesQueryVariables> query={EntitiesDocument} {...props} />;

export function useEntitiesQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<EntitiesQueryVariables>) {
    return ReactApolloHooks.useQuery<EntitiesQuery, EntitiesQueryVariables>(EntitiesDocument, baseOptions);
}

Error:(986, 95) TS2345: Argument of type 'QueryHookOptions<EntitiesQueryVariables, Record<string, any>> | undefined' is not assignable to parameter of type 'QueryHookOptions<EntitiesQuery, EntitiesQueryVariables> | undefined'. Type 'QueryHookOptions<EntitiesQueryVariables, Record<string, any>>' is not assignable to type 'QueryHookOptions<EntitiesQuery, EntitiesQueryVariables>'. Property 'where' is missing in type 'EntitiesQuery' but required in type 'EntitiesQueryVariables'.

To Reproduce To reproduce I just run graphql-codegen --config codegen.yml.

  1. My GraphQL schema:
schema {
    query: Query
    mutation: Mutation
}

type Entity {
    address: Address!
    id: Int!
    isCarrier: Boolean!
    isCustomer: Boolean!
    isVendor: Boolean!
    isWarehouse: Boolean!
    itemRelations: [ItemEntity]
    type: Int!
}

type Query {
   entities(where: WhereEntityInput): [Entity]
}

fragment EntityAllFields on Entity {
    id
    type
    isCustomer
    isVendor
    isCarrier
    isWarehouse
}
  1. My GraphQL operations:
query Entities($where: WhereEntityInput!) {
    entities(where: $where) {
        ...EntityAllFields
        address{
            name
        }
    }
}
  1. My codegen.yml config file:
overwrite: true
schema: "http://localhost:4000"
documents: "./src/graphql/*.graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
    config:
      withHooks: true
      withHOC: false
      withComponents: false
      hooksImportFrom: "@apollo/react-hooks"
  ./graphql.schema.json:
    plugins:
      - "introspection"

  1. Related dependancies:
"dependencies": {
    "@apollo/react-hooks": "^3.0.0",
    "apollo-boost": "^0.3.1",
    "graphql": "^14.2.1",
    "react-apollo": "^2.5.5",
    "apollo-client": "^2.6.3",
    "apollo-utilities": "^1.3.2",
    "graphql-tag": "^2.0.0",
    "typescript": "3.4.5",
  },
  "devDependencies": {
    "@graphql-codegen/cli": "^1.1.3",
    "@graphql-codegen/introspection": "^1.1.3",
    "@graphql-codegen/typescript": "1.1.3",
    "@graphql-codegen/typescript-operations": "1.1.3",
    "@graphql-codegen/typescript-react-apollo": "1.1.3",
  }

Expected behavior A graphql.tsx that does not contain typescript errors.

Environment:

  • OS: Windows 10 1809
  • @graphql-codegen/...: 1.1.3
  • NodeJS: 10.15.3

Additional context As far as I can tell this should be working. Though I'm having trouble finding in-depth documentation on this subject.

vivirancy avatar Aug 07 '19 16:08 vivirancy

You’re using an old version. Could you try with the latest version 1.5.0?

ardatan avatar Aug 07 '19 17:08 ardatan

@ardatan It looks like this was only an issue in the older version. Thanks!

vivirancy avatar Aug 07 '19 17:08 vivirancy