Files
msw/test/browser/graphql-api/document-node.mocks.ts
8c8e959b49 feat!: adopt the global Fetch API (#1436)
Co-authored-by: Frederik Rabøl <[email protected]>
Co-authored-by: Christoph Fricke <[email protected]>
Co-authored-by: Piotr <[email protected]>
Co-authored-by: Kristján Oddsson <[email protected]>
Co-authored-by: thepassle <[email protected]>
Co-authored-by: Kevin Østerkilde <[email protected]>
Co-authored-by: Laryssa Rocha <[email protected]>
Co-authored-by: Matthew Costabile <[email protected]>
2023-10-23 09:45:04 +02:00

72 lines
1.3 KiB
TypeScript

import { parse } from 'graphql'
import { graphql, HttpResponse } from 'msw'
import { setupWorker } from 'msw/browser'
const GetUser = parse(`
query GetUser {
user {
firstName
}
}
`)
const Login = parse(`
mutation Login($username: String!) {
session {
id
}
user {
username
}
}
`)
const GetSubscription = parse(`
query GetSubscription {
subscription {
id
}
}
`)
const github = graphql.link('https://api.github.com/graphql')
const worker = setupWorker(
// "DocumentNode" can be used as the expected query/mutation.
graphql.query(GetUser, () => {
return HttpResponse.json({
data: {
// Note that inferring the query body and variables
// is impossible with the native "DocumentNode".
// Consider using tools like GraphQL Code Generator.
user: {
firstName: 'John',
},
},
})
}),
graphql.mutation<object, { username: string }>(Login, ({ variables }) => {
return HttpResponse.json({
data: {
session: {
id: 'abc-123',
},
user: {
username: variables.username,
},
},
})
}),
github.query(GetSubscription, () => {
return HttpResponse.json({
data: {
subscription: {
id: 123,
},
},
})
}),
)
worker.start()