Generated hook types don't align.
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.
- 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
}
- My GraphQL operations:
query Entities($where: WhereEntityInput!) {
entities(where: $where) {
...EntityAllFields
address{
name
}
}
}
- My
codegen.ymlconfig 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"
- 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.
You’re using an old version. Could you try with the latest version 1.5.0?
@ardatan It looks like this was only an issue in the older version. Thanks!