mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
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]>
72 lines
1.3 KiB
TypeScript
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()
|