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

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

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