aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/posts/post.handler.ts
blob: 4abdfed0ffafc7d2a4a8a4c1c6fcf1d97db73715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { type ExecutionResult, graphql as executeGraphql } from 'graphql';
import { HttpResponse, graphql } from 'msw';
import type { PostResponse } from '../../../../src/services/graphql';
import { wpPostsFixture } from '../../../fixtures';
import { schema } from '../../schema';

export const postHandler = graphql.query<PostResponse, Record<'slug', string>>(
  'Post',
  async ({ query, variables }) => {
    const { data, errors } = (await executeGraphql({
      schema,
      source: query,
      variableValues: variables,
      rootValue: {
        post: wpPostsFixture.find((wpPost) => wpPost.slug === variables.slug),
      },
    })) as ExecutionResult<PostResponse>;

    return HttpResponse.json({ data, errors });
  }
);