|
import type { Preview } from '@storybook/react' |
|
import DefaultDecorator from '../web/src/app/storybook/decorators' |
|
import { initialize, mswLoader } from 'msw-storybook-addon' |
|
import { defaultConfig } from '../web/src/app/storybook/graphql' |
|
import { graphql, HttpResponse } from 'msw' |
|
|
|
initialize({ |
|
onUnhandledRequest: 'bypass', |
|
}) |
|
|
|
export type GraphQLRequestHandler = (variables: unknown) => object |
|
export type GraphQLParams = Record<string, GraphQLRequestHandler> |
|
|
|
const componentConfig: Record<string, GraphQLParams> = {} |
|
|
|
export const mswHandler = graphql.operation((params) => { |
|
const url = new URL(params.request.url) |
|
const gql = componentConfig[url.pathname] |
|
const handler = gql[params.operationName] |
|
if (!handler) { |
|
return HttpResponse.json({ |
|
errors: [ |
|
{ message: `No mock defined for operation '${params.operationName}'.` }, |
|
], |
|
}) |
|
} |
|
|
|
if (typeof handler === 'function') { |
|
return HttpResponse.json(handler(params.variables)) |
|
} |
|
|
|
return HttpResponse.json(handler) |
|
}) |
|
|
|
interface LoaderArg { |
|
id: string |
|
parameters: { |
|
graphql?: GraphQLParams |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function graphQLLoader(arg: LoaderArg): void { |
|
const path = '/' + encodeURIComponent(arg.id) + '/api/graphql' |
|
componentConfig[path] = arg.parameters.graphql || {} |
|
} |
|
|
|
const preview: Preview = { |
|
parameters: { |
|
controls: { |
|
matchers: { |
|
color: /(background|color)$/i, |
|
date: /Date$/i, |
|
}, |
|
}, |
|
graphql: { |
|
|
|
RequireConfig: { data: defaultConfig }, |
|
useExpFlag: { data: { experimentalFlags: [] } }, |
|
}, |
|
msw: { handlers: [mswHandler] }, |
|
}, |
|
decorators: [DefaultDecorator], |
|
loaders: [graphQLLoader, mswLoader], |
|
} |
|
|
|
export default preview |
|
|