Apollo Elements Apollo Elements Guides API Blog Toggle darkmode

Getting Started: Codegen

Using the @graphql-codegen/cli package and its plugins, you can automatically generate TypeScript typings from your schema and query documents.

Install Dependencies

npm i -D \
  @graphql-codegen/cli \
  @graphql-codegen/typescript \

Or if you use yarn:

yarn add -D \
  @graphql-codegen/cli \
  @graphql-codegen/typescript \

Configure GraphQL Code Generator

Create a file called .graphqlrc.yml in your project root and paste in these contents:

schema: 'https://api.app.dev/graphql' # replace with url to your graphql server

    config: # season to taste
      constEnums: true
      declarationKind: interface
      dedupeOperationSuffix: true
      immutableTypes: true
      namingConvention: keep
      noSchemaStitching: true
      operationResultSuffix: Data
      optionalResolveType: true
      useIndexSignature: true

      src/codegen/schema.ts: # replace with path to the file you wish to generate
        schema: src/client.schema.graphql # replace with path to client-side schema
          - typescript
          - typescript-operations

          - packages/test-helpers/*.query.graphql
          - packages/test-helpers/*.mutation.graphql
          - packages/test-helpers/*.subscription.graphql
          - packages/test-helpers/*.fragment.graphql

Now, to automatically generate typings, run

npx graphql-codegen --watch

This has the added benefit of linting your graphql files for errors.

Read more about graphql-codegen on their webpage