最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论