aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/topics/topic.handler.ts
blob: 1cb565b804c17dd5265a11d74741cf71c3c26db3 (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 { TopicResponse } from '../../../../src/services/graphql';
import { wpTopicsFixture } from '../../../fixtures';
import { wordpressAPI } from '../../instances';
import { schema } from '../../schema';

export const topicHandler = wordpressAPI.query<
  TopicResponse,
  Record<'slug', string>
>('Topic', async ({ query, variables }) => {
  const { data, errors } = (await graphql({
    schema,
    source: query,
    variableValues: variables,
    rootValue: {
      topic: wpTopicsFixture.find((wpTopic) => wpTopic.slug === variables.slug),
    },
  })) as ExecutionResult<TopicResponse>;

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