jestjs - Looking for examples of how to unit test GraphQL resolvers? - Stack Overflow

I have a graphQL resolver that looks like this:import { GraphQLSchema } from 'graphql';impo

I have a graphQL resolver that looks like this:

import { GraphQLSchema } from 'graphql';
import { buildSubgraphSchema } from '@apollo/subgraph';

...

const schema: GraphQLSchema = buildSubgraphSchema([
  {
...
    resolvers: {
...
      OutputGreenSLDs: {
...
        reviewedGreenSLDs(sld: SLD) {
          return ...
        },
...
    },
  },
]);

export default shema;

How can I write Jest unit tests to test the reviewedGreenSLDs function? The challenge is that I don't know how to use shema to access reviewedGreenSLDs directly. The only other way I know of to invoke the reviewedGreenSLDs function is to make a graphQL query from the frontend (the code above is backend), but then how do I mock the data being passed to reviewedGreenSLDs. From the frontend, the query only takes an SDL id, and it's up to the resolver to find the actual SLD. I can mock the id but that does me no good if the resolver expects the id to match some SDL that doesn't exist. I'm also unfamiliar with how Jest performs tests from frontend to backend and back again. Most examples I've seen are setup to return mock data immediately when a backend endpoint is called (it doesn't actually reach the backend), but this would defeat the purpose since I want it to actually reach the backend in order to test the reviewedGreenSLDs function.

So I'm looking for some examples of how I can either 1) use the schema object to access the reviewedGreenSLDs function directly or 2) write unit tests on the frontend which will make a graphql call (with a fake id if necessary) and actually hit the backend but with some mock data to pass to the reviewedGreenSLDs function.

Are either of these possible?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744580061a4581980.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信