Skip to content

hellocomet/graphql-codegen-typescript-operations-tester

Repository files navigation

graphql-codegen-typescript-operations-tester

Version Test workflow Release workflow

Install

npm i -D @graphql-codegen/typescript @graphql-codegen/typescript-operations graphql-codegen-typescript-operations-tester

In codegen.yml

generated/tests.ts:
  plugins:
    - typescript
    - typescript-operations
    - graphql-codegen-typescript-operations-tester

Given a schema like so

type Author {
  firstname: String
  lastname: String
  fullname: String
}

type Book {
  title: String
  author: Author
}

type Query {
  books: [Book]
}

Create a graphQL requester somewhere in your tests

import { graphql, GraphQLSchema, ExecutionResult } from 'graphql'
import { testGetBooksQuery } from './generated/tests.ts'
import { schema } from 'path/to/my/schema'

type Options = {
  silenceError?: boolean
}

export function graphqlRequester<TResultDataType>(options: Options = {}) {
  return async (
    query: string,
    variables?: Record<string, any>
  ): Promise<ExecutionResult<TResultDataType>> => {
    const result = await graphql({
      variableValues: variables,
      schema,
      source: query,
    })

    if (!options.silenceError && result.errors?.length > 0) {
      result.errors.forEach((error) => console.error(error))
    }

    // Allow typing the return value of the query which is by default:
    // `Record<string, any>`.
    return result as ExecutionResult<TResultDataType>
  }
}

describe('Test something cool', () => {
  it('testGetBooksQuery should return something', async () => {
    const res = await testGetBooksQuery(graphqlRequester())
    expect(res.data?.books).toBeAwesome()
  })
})