Skip to content

Unit Testing for Resolvers #961

Answered by hayes
KoichiKiyokawa asked this question in Q&A
Jul 20, 2023 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

My general approach to testing GraphQL apis is to not unit test resolvers, because things generally fall into 2 categories:

  1. The resolver is very simple, and it makes more sense to test it as part of a GraphQL query
  2. The logic is complex, and the complex parts of the resolver should be moved into a function that can be tested independently, and the resolver is just a wrapper that calls your logic with the right arguments

That being said, it's pretty easy to get a resolver from your schema to test. Here is an example:

import {
  GraphQLFieldResolver,
  GraphQLObjectType,
  GraphQLSchema,
  defaultFieldResolver,
} from 'graphql';

const schema = builder.toSchema()

const resolver = getResolver

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@KoichiKiyokawa
Comment options

@awinograd
Comment options

@hayes
Comment options

@awinograd
Comment options

Answer selected by KoichiKiyokawa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants