aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msw/handlers/posts/last-post-cursor.handler.ts
blob: f5c58dcf0eccce798ce96d20b96cf665eeabe581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { type ExecutionResult, graphql } from 'graphql';
import { HttpResponse } from 'msw';
import type { LastPostCursorResponse } from '../../../../src/services/graphql';
import { wpPostsFixture } from '../../../fixtures';
import { getConnection } from '../../../utils/graphql';
import { wordpressAPI } from '../../instances';
import { schema } from '../../schema';

export const lastPostCursorHandler = wordpressAPI.query<
  LastPostCursorResponse,
  Record<'first', number>
>('LastPostCursor', async ({ query, variables }) => {
  const pageParams = new URLSearchParams(window.location.search);
  const isError = pageParams.get('error') === 'true';

  if (isError) return HttpResponse.json({ data: { posts: null } });

  const { data, errors } = (await graphql({
    schema,
    source: query,
    variableValues: variables,
    rootValue: {
      posts({ first }: typeof variables) {
        return getConnection({
          after: null,
          data: wpPostsFixture,
          first,
        });
      },
    },
  })) as ExecutionResult<LastPostCursorResponse>;

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