Skip to content

Commit f618973

Browse files
author
Danier Rivas
authored
feat: add support for query and mutation variables (#18)
1 parent ebd48d0 commit f618973

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/plugin.test.ts

+92
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,95 @@ it('should support `contextValue` configuration', async () => {
186186
},
187187
})
188188
})
189+
190+
it('should support schema with query and mutation `variables`', async () => {
191+
const spy = vi.fn()
192+
193+
await createServer([
194+
plugin({
195+
schema: {
196+
typeDefs: gql`
197+
input MessageInput {
198+
content: String
199+
}
200+
201+
type Message {
202+
content: String
203+
}
204+
205+
type Query {
206+
getMessage(id: ID!): Message
207+
}
208+
209+
type Mutation {
210+
createMessage(input: MessageInput): Message
211+
}
212+
`,
213+
resolvers: {
214+
Query: {
215+
getMessage: (_, args) => {
216+
spy(args)
217+
return {
218+
content: `Message: ${args.id}`,
219+
}
220+
},
221+
},
222+
Mutation: {
223+
createMessage: (_, args) => {
224+
spy(args)
225+
return {
226+
content: `Message: ${args.input.content}`,
227+
}
228+
},
229+
},
230+
},
231+
},
232+
}),
233+
])
234+
235+
const query = await request(
236+
gql`
237+
query getMessage($id: ID!) {
238+
getMessage(id: $id) {
239+
content
240+
}
241+
}
242+
`,
243+
{
244+
id: 'Hello-World',
245+
},
246+
)
247+
248+
expect(query).toEqual<ExecutionResult>({
249+
errors: undefined,
250+
data: {
251+
getMessage: {
252+
content: 'Message: Hello-World',
253+
},
254+
},
255+
})
256+
257+
const mutation = await request(
258+
gql`
259+
mutation createMessage($input: MessageInput!) {
260+
createMessage(input: $input) {
261+
content
262+
}
263+
}
264+
`,
265+
{
266+
input: {
267+
content: 'Hello World',
268+
},
269+
},
270+
)
271+
272+
expect(mutation).toEqual<ExecutionResult>({
273+
errors: undefined,
274+
data: {
275+
createMessage: {
276+
content: 'Message: Hello World',
277+
},
278+
},
279+
})
280+
})

src/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function handleGraphQLRequest(
2525
const result = await graphql({
2626
schema,
2727
source: body.query,
28+
variableValues: body.variables,
2829
contextValue,
2930
})
3031

0 commit comments

Comments
 (0)