I am using graphql codegen
programmatically.
import * as typescriptReactApolloPlugin from "@graphql-codegen/typescript-react-apollo";
const config: Types.GenerateOptions = {
documents: [{ document: doc }],
config: {},
// returns the string output, rather than writing to a file
filename: "index.ts",
schema: undefined,
schemaAst: schema,
plugins: [
{
typescriptReactApollo: {},
},
],
pluginMap: {
typescriptReactApollo: typescriptReactApolloPlugin,
},
};
const result = await codegen(config);
This is my document (Query):
query myquery {
Users {
limit
}
}
And this is the generated result:
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
const defaultOptions = {} as const;
export const MyqueryDocument = gql`
query myquery {
Users {
limit
}
}
`;
/**
* __useMyqueryQuery__
*
* To run a query within a React component, call `useMyqueryQuery` and pass it any options that fit your needs.
* When your component renders, `useMyqueryQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useMyqueryQuery({
* variables: {
* },
* });
*/
export function useMyqueryQuery(baseOptions?: Apollo.QueryHookOptions<MyqueryQuery, MyqueryQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useQuery<MyqueryQuery, MyqueryQueryVariables>(MyqueryDocument, options);
}
export function useMyqueryLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<MyqueryQuery, MyqueryQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useLazyQuery<MyqueryQuery, MyqueryQueryVariables>(MyqueryDocument, options);
}
export type MyqueryQueryHookResult = ReturnType<typeof useMyqueryQuery>;
export type MyqueryLazyQueryHookResult = ReturnType<typeof useMyqueryLazyQuery>;
export type MyqueryQueryResult = Apollo.QueryResult<MyqueryQuery, MyqueryQueryVariables>;
Problem
The problem is that some types are missing e.g. MyqueryQuery
, MyqueryQueryVariables
.
How to fix that?
Update:
Adding the plugins typescript
and typescript-operations
will generate additional types. But the hooks are no longer generated.
I made a Codesandbox showing the problem.