Operation file reference
The operation type definition file is generated next to each GraphQL operation file. It expresses the types of the GraphQL operation, especially the types of variables and the type of the response.
With the default setting, the file has a .d.graphql.ts
extension. This is to tell TypeScript what happens if you import the corresponding .graphql
file.
This page documents what you can import from the .graphql
file.
Operation Object
The operation object is the type of the object you can pass to a GraphQL client to execute the operation. It has the type TypedDocumentNode<Result, Variables>
, where Result
is the type of the response and Variables
is the type of the variables required by the operation.
With the default setting, the operation object is exported as a default export. You can import it like:
import getTodosQuery from "./getTodos.graphql";
By configuring the defaultExportForOperation
option, you can change this to a named export:
import { getTodosQuery } from "./operation.graphql";
The name of the export is determined by the name of the operation. See the references for generate.name
on how to configure generated names.
Results and Variables
By default, Result
and Variables
types are not exported. However, you can configure the generate.export
option to export those as well.
If you configure the option to export results and variables, they are exported as named exports. Example usage:
import { getTodosQuery, type GetTodosQueryResult, type GetTodosQueryVariables } from "./operation.graphql";
Fragment Object
A fragment definition also generates a fragment object. It has the type TypedDocumentNode<Result, never>
, where Result
is the shape of a query response that matches the fragment.
The fragment object is exported as a named export. Example usage:
import { todoFragment } from "./todo.graphql";