Skip to content

Commit d8388b6

Browse files
committed
Add graphql mutation
1 parent 5810137 commit d8388b6

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

packages/workgrid-client/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@workgrid/client",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"main": "dist/client.js",
55
"types": "dist/client.d.ts",
66
"files": [
@@ -33,4 +33,4 @@
3333
]
3434
},
3535
"license": "ISC"
36-
}
36+
}

packages/workgrid-client/src/client.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,39 @@ const server = setupServer(
131131
suggestions: ['ask another'],
132132
})
133133
)
134+
}),
135+
rest.post(`https://company-code.workgrid.com/graphql`, (req, res, ctx) => {
136+
return res(
137+
ctx.json({
138+
data: {
139+
me: {
140+
id: '12345',
141+
space: {
142+
id: '17dacf8d-ee8d-45ff-9d6b-f984e789c4e3',
143+
apps: {
144+
edges: [
145+
{
146+
node: {
147+
entrypoint: 'f19b6a81-7648-4c72-97df-87f45e24191f',
148+
title: 'Intranet Admin Tools',
149+
},
150+
},
151+
{
152+
node: {
153+
entrypoint: '2b4d2ae8-fde4-4c8a-b4f1-8e349c3fcf68',
154+
title: 'Quick Links',
155+
},
156+
},
157+
],
158+
pageInfo: {
159+
hasNextPage: false,
160+
},
161+
},
162+
},
163+
},
164+
},
165+
})
166+
)
134167
})
135168
)
136169

@@ -204,6 +237,63 @@ describe('@workgrid/client', () => {
204237
`)
205238
})
206239

240+
test('graphql', async () => {
241+
const query = `
242+
query appsQuery($spaceId: ID!) {
243+
me {
244+
space(spaceId: $spaceId) {
245+
id,
246+
apps {
247+
edges {
248+
node {
249+
entrypoint,
250+
title
251+
}
252+
},
253+
pageInfo {
254+
hasNextPage
255+
}
256+
}
257+
}
258+
}
259+
}`
260+
261+
const variables = {
262+
spaceId: '17dacf8d-ee8d-45ff-9d6b-f984e789c4e3',
263+
}
264+
const result = await client.mutate('graphql', { query, variables })
265+
266+
expect(result).toMatchInlineSnapshot(`
267+
Object {
268+
"me": Object {
269+
"id": "12345",
270+
"space": Object {
271+
"apps": Object {
272+
"edges": Array [
273+
Object {
274+
"node": Object {
275+
"entrypoint": "f19b6a81-7648-4c72-97df-87f45e24191f",
276+
"title": "Intranet Admin Tools",
277+
},
278+
},
279+
Object {
280+
"node": Object {
281+
"entrypoint": "2b4d2ae8-fde4-4c8a-b4f1-8e349c3fcf68",
282+
"title": "Quick Links",
283+
},
284+
},
285+
],
286+
"pageInfo": Object {
287+
"hasNextPage": false,
288+
},
289+
},
290+
"id": "17dacf8d-ee8d-45ff-9d6b-f984e789c4e3",
291+
},
292+
},
293+
}
294+
`)
295+
})
296+
207297
test('customMutate', async () => {
208298
const options = { mutationFn: async () => ({ message: 'Hello, world!' }) }
209299
const result = client.customMutate('mutation-key', undefined, options)

packages/workgrid-client/src/client.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,27 @@ setTypedMutationDefaults('deleteNotification', (client) => ({
528528
},
529529
}))
530530

531+
// graphql
532+
// ================================================================================================================================
533+
534+
/** @beta */
535+
export interface Mutations {
536+
graphql: Mutation<['graphql'], { query: string; variables: unknown }>
537+
}
538+
539+
setTypedMutationDefaults('graphql', (client) => ({
540+
mutationFn: async (payload) => {
541+
const { query, variables } = payload
542+
const data = JSON.stringify({
543+
query,
544+
variables,
545+
})
546+
547+
const response = await client.httpClient.post('graphql', data)
548+
return response.data.data /* unwrap jsend */
549+
},
550+
}))
551+
531552
// getActivity
532553
// ================================================================================================================================
533554

0 commit comments

Comments
 (0)