Skip to content

Commit 27f626c

Browse files
authored
fix(hasura): meta.gqlVariables passed to update Hasura data provider (#6776)
1 parent 82ea315 commit 27f626c

4 files changed

Lines changed: 220 additions & 0 deletions

File tree

.changeset/neat-tips-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@refinedev/hasura": patch
3+
---
4+
5+
meta.gqlVariables now passed to update query for Hasura data provider

packages/hasura/src/dataProvider/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ const dataProvider = (
353353
const response = await client.request<BaseRecord>(gqlOperation, {
354354
id,
355355
object: variables || {},
356+
...meta?.gqlVariables,
356357
});
357358

358359
return {

packages/hasura/test/update/index.mock.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,124 @@ nock("https://ruling-redbird-23.hasura.app:443", { encodedQueryParams: true })
459459
"8437d43c5966c1c6-BUD",
460460
],
461461
);
462+
463+
nock("https://flowing-mammal-24.hasura.app:443", { encodedQueryParams: true })
464+
.post("/v1/graphql", {
465+
query:
466+
"mutation UpdatePost($id: uuid!, $object: posts_set_input!, $includeCategory: Boolean = false) {\n update_posts_by_pk(pk_columns: {id: $id}, _set: $object) {\n id\n title\n content\n category_id\n category @include(if: $includeCategory) {\n id\n title\n }\n }\n}\n",
467+
variables: {
468+
id: "572708c7-840d-430a-befd-1416bdee799a",
469+
object: {
470+
title: "Updated Title",
471+
content: "Updated Content",
472+
category_id: "e27156c3-9998-434f-bd5b-2b078283ff26",
473+
},
474+
includeCategory: true,
475+
},
476+
operationName: "UpdatePost",
477+
})
478+
.reply(
479+
200,
480+
{
481+
data: {
482+
update_posts_by_pk: {
483+
id: "572708c7-840d-430a-befd-1416bdee799a",
484+
title: "Updated Title",
485+
content: "Updated Content",
486+
category_id: "e27156c3-9998-434f-bd5b-2b078283ff26",
487+
category: {
488+
id: "e27156c3-9998-434f-bd5b-2b078283ff26",
489+
title: "lorem1 integer tincidunt",
490+
},
491+
},
492+
},
493+
},
494+
[
495+
"Date",
496+
"Wed, 10 Jan 2024 20:56:53 GMT",
497+
"Content-Type",
498+
"application/json; charset=utf-8",
499+
"Content-Length",
500+
"274",
501+
"Connection",
502+
"close",
503+
"x-request-id",
504+
"83f68d28a0c651f19c430e2a6155ee90",
505+
"CF-Cache-Status",
506+
"DYNAMIC",
507+
"Content-Security-Policy",
508+
"upgrade-insecure-requests",
509+
"Referrer-Policy",
510+
"strict-origin-when-cross-origin",
511+
"Strict-Transport-Security",
512+
"max-age=31536000; includeSubDomains",
513+
"X-Content-Type-Options",
514+
"nosniff",
515+
"X-Frame-Options",
516+
"SAMEORIGIN",
517+
"X-XSS-Protection",
518+
"0",
519+
"Server",
520+
"cloudflare",
521+
"CF-RAY",
522+
"8437d441abf61cf1-BUD",
523+
],
524+
);
525+
526+
nock("https://flowing-mammal-24.hasura.app:443", { encodedQueryParams: true })
527+
.post("/v1/graphql", {
528+
query:
529+
"mutation UpdatePost($id: uuid!, $object: posts_set_input!, $includeCategory: Boolean = false) {\n update_posts_by_pk(pk_columns: {id: $id}, _set: $object) {\n id\n title\n content\n category_id\n category @include(if: $includeCategory) {\n id\n title\n }\n }\n}\n",
530+
variables: {
531+
id: "572708c7-840d-430a-befd-1416bdee799a",
532+
object: {
533+
title: "Updated Title",
534+
content: "Updated Content",
535+
category_id: "e27156c3-9998-434f-bd5b-2b078283ff26",
536+
},
537+
},
538+
operationName: "UpdatePost",
539+
})
540+
.reply(
541+
200,
542+
{
543+
data: {
544+
update_posts_by_pk: {
545+
id: "572708c7-840d-430a-befd-1416bdee799a",
546+
title: "Updated Title",
547+
content: "Updated Content",
548+
category_id: "e27156c3-9998-434f-bd5b-2b078283ff26",
549+
},
550+
},
551+
},
552+
[
553+
"Date",
554+
"Wed, 10 Jan 2024 20:56:53 GMT",
555+
"Content-Type",
556+
"application/json; charset=utf-8",
557+
"Content-Length",
558+
"274",
559+
"Connection",
560+
"close",
561+
"x-request-id",
562+
"83f68d28a0c651f19c430e2a6155ee90",
563+
"CF-Cache-Status",
564+
"DYNAMIC",
565+
"Content-Security-Policy",
566+
"upgrade-insecure-requests",
567+
"Referrer-Policy",
568+
"strict-origin-when-cross-origin",
569+
"Strict-Transport-Security",
570+
"max-age=31536000; includeSubDomains",
571+
"X-Content-Type-Options",
572+
"nosniff",
573+
"X-Frame-Options",
574+
"SAMEORIGIN",
575+
"X-XSS-Protection",
576+
"0",
577+
"Server",
578+
"cloudflare",
579+
"CF-RAY",
580+
"8437d441abf61cf1-BUD",
581+
],
582+
);

packages/hasura/test/update/index.spec.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,97 @@ describe("with gql", () => {
155155
);
156156
},
157157
);
158+
159+
it("correctly passes variables from meta.gqlVariables to the query", async () => {
160+
const id = "572708c7-840d-430a-befd-1416bdee799a";
161+
162+
const client = createClient("hasura-default");
163+
const { data } = await dataProvider(client, {
164+
namingConvention: "hasura-default",
165+
}).update({
166+
resource: "posts",
167+
id,
168+
variables: {
169+
title: "Updated Title",
170+
content: "Updated Content",
171+
category_id: "e27156c3-9998-434f-bd5b-2b078283ff26",
172+
},
173+
meta: {
174+
gqlMutation: gql`
175+
mutation UpdatePost(
176+
$id: uuid!
177+
$object: posts_set_input!
178+
$includeCategory: Boolean = false
179+
) {
180+
update_posts_by_pk(
181+
pk_columns: { id: $id }
182+
_set: $object
183+
) {
184+
id
185+
title
186+
content
187+
category_id
188+
category @include(if: $includeCategory) {
189+
id
190+
title
191+
}
192+
}
193+
}
194+
`,
195+
gqlVariables: {
196+
includeCategory: true,
197+
},
198+
},
199+
});
200+
201+
expect(data["id"]).toEqual(id);
202+
expect(data["title"]).toEqual("Updated Title");
203+
expect(data["content"]).toEqual("Updated Content");
204+
expect(data["category"].id).toEqual("e27156c3-9998-434f-bd5b-2b078283ff26");
205+
});
206+
207+
it("doesn't pass extra variables to the query without meta.gqlVariables", async () => {
208+
const id = "572708c7-840d-430a-befd-1416bdee799a";
209+
210+
const client = createClient("hasura-default");
211+
const { data } = await dataProvider(client, {
212+
namingConvention: "hasura-default",
213+
}).update({
214+
resource: "posts",
215+
id,
216+
variables: {
217+
title: "Updated Title",
218+
content: "Updated Content",
219+
category_id: "e27156c3-9998-434f-bd5b-2b078283ff26",
220+
},
221+
meta: {
222+
gqlMutation: gql`
223+
mutation UpdatePost(
224+
$id: uuid!
225+
$object: posts_set_input!
226+
$includeCategory: Boolean = false
227+
) {
228+
update_posts_by_pk(
229+
pk_columns: { id: $id }
230+
_set: $object
231+
) {
232+
id
233+
title
234+
content
235+
category_id
236+
category @include(if: $includeCategory) {
237+
id
238+
title
239+
}
240+
}
241+
}
242+
`,
243+
},
244+
});
245+
246+
expect(data["id"]).toEqual(id);
247+
expect(data["title"]).toEqual("Updated Title");
248+
expect(data["content"]).toEqual("Updated Content");
249+
expect(data["category"]).toBeUndefined();
250+
});
158251
});

0 commit comments

Comments
 (0)