On this page

Configure GraphQL Code Generator

GraphQL Mesh v0 documentation (superseded by v1): Configure GraphQL Code Generator in GraphQL Mesh to generate fully typed resolvers and SDK. Use the default configuration or customize it to fit your needs.

Mesh relies on GraphQL Code Generator to generate the Resolvers type that give you access to:

  • fully typed resolvers map
  • fully typed SDK (through the context) to fetch data from Sources

The generated typed Mesh SDK can also be accessed directly, as shown below:

test.ts
import { getMeshSDK } from './.mesh'

async function test() {
  // Load mesh config and get the sdkClient from it
  const sdk = getMeshSDK()

  // Execute `myQuery` and get a type-safe result
  // Variables and result are typed: { getSomething: { fieldA: string, fieldB: number }, errors?: GraphQLError[] }
  const { getSomething } = await sdk.myQuery({ someVar: 'foo' })
}

GraphQL Code Generator default configuration

Mesh provides a default GraphQL Code Generator configuration, shown below:

{
  "skipTypename": true,
  "flattenGeneratedTypes": false,
  "onlyOperationTypes": false,
  "preResolveTypes": false,
  "namingConvention": "keep",
  "documentMode": "graphQLTag",
  "gqlImport": "@graphql-mesh/utils#gql",
  "enumsAsTypes": true,
  "ignoreEnumValuesFromSchema": true,
  "useIndexSignature": true,
  "contextType": "MeshContext"
}

Customizing the GraphQL Code Generator configuration

The above default configuration can be overridden with the codegen root parameter, as shown below:

.meshrc.yaml
codegen:
  skipTypename: false
  contextType: './context#MyContextType'