Skip to content

Commit 82ea315

Browse files
authored
fix(hasura): meta.gqlVariables passed to create Hasura data provider (#6777)
1 parent 272ac4f commit 82ea315

4 files changed

Lines changed: 191 additions & 0 deletions

File tree

.changeset/six-cooks-check.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 create 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
@@ -264,6 +264,7 @@ const dataProvider = (
264264
if (gqlOperation) {
265265
const response = await client.request<BaseRecord>(gqlOperation, {
266266
object: variables || {},
267+
...meta?.gqlVariables,
267268
});
268269

269270
return {

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,117 @@ nock("https://ruling-redbird-23.hasura.app:443", { encodedQueryParams: true })
783783
"84373e8abc566850-BUD",
784784
],
785785
);
786+
787+
nock("https://flowing-mammal-24.hasura.app:443", { encodedQueryParams: true })
788+
.post("/v1/graphql", {
789+
query:
790+
"mutation CreatePost($object: posts_insert_input!, $includeCategory: Boolean = false) {\n insert_posts_one(object: $object) {\n id\n title\n content\n category @include(if: $includeCategory) {\n id\n }\n }\n}\n",
791+
variables: {
792+
object: {
793+
content: "Lorem ipsum dolor sit amet.",
794+
title: "Lorem ipsum dolore",
795+
category_id: "ef49aebd-abcc-4bac-b064-a63b31f2e8ce",
796+
},
797+
includeCategory: true,
798+
},
799+
operationName: "CreatePost",
800+
})
801+
.reply(
802+
200,
803+
{
804+
data: {
805+
insert_posts_one: {
806+
id: "d87e5a0f-1e31-4f90-8242-4a29a4ea6d9c",
807+
title: "Lorem ipsum dolore",
808+
content: "Lorem ipsum dolor sit amet.",
809+
category: { id: "ef49aebd-abcc-4bac-b064-a63b31f2e8ce" },
810+
},
811+
},
812+
},
813+
[
814+
"Date",
815+
"Wed, 10 Jan 2024 19:14:42 GMT",
816+
"Content-Type",
817+
"application/json; charset=utf-8",
818+
"Content-Length",
819+
"201",
820+
"Connection",
821+
"close",
822+
"x-request-id",
823+
"c4e58f92ba1d7aef3294c1d7a72e9f10",
824+
"CF-Cache-Status",
825+
"DYNAMIC",
826+
"Content-Security-Policy",
827+
"upgrade-insecure-requests",
828+
"Referrer-Policy",
829+
"strict-origin-when-cross-origin",
830+
"Strict-Transport-Security",
831+
"max-age=31536000; includeSubDomains",
832+
"X-Content-Type-Options",
833+
"nosniff",
834+
"X-Frame-Options",
835+
"SAMEORIGIN",
836+
"X-XSS-Protection",
837+
"0",
838+
"Server",
839+
"cloudflare",
840+
"CF-RAY",
841+
"84373e8ffac14532-BUD",
842+
],
843+
);
844+
845+
nock("https://flowing-mammal-24.hasura.app:443", { encodedQueryParams: true })
846+
.post("/v1/graphql", {
847+
query:
848+
"mutation CreatePost($object: posts_insert_input!, $includeCategory: Boolean = false) {\n insert_posts_one(object: $object) {\n id\n title\n content\n category @include(if: $includeCategory) {\n id\n }\n }\n}\n",
849+
variables: {
850+
object: {
851+
content: "Lorem ipsum dolor sit amet.",
852+
title: "Lorem ipsum dolore",
853+
category_id: "ef49aebd-abcc-4bac-b064-a63b31f2e8ce",
854+
},
855+
},
856+
operationName: "CreatePost",
857+
})
858+
.reply(
859+
200,
860+
{
861+
data: {
862+
insert_posts_one: {
863+
id: "e98f2a1f-c42c-4b17-91d5-f853a4c7e3d1",
864+
title: "Lorem ipsum dolore",
865+
content: "Lorem ipsum dolor sit amet.",
866+
},
867+
},
868+
},
869+
[
870+
"Date",
871+
"Wed, 10 Jan 2024 19:14:43 GMT",
872+
"Content-Type",
873+
"application/json; charset=utf-8",
874+
"Content-Length",
875+
"139",
876+
"Connection",
877+
"close",
878+
"x-request-id",
879+
"d3a75b91cba6e4d77c9e8f4a23b5f1a9",
880+
"CF-Cache-Status",
881+
"DYNAMIC",
882+
"Content-Security-Policy",
883+
"upgrade-insecure-requests",
884+
"Referrer-Policy",
885+
"strict-origin-when-cross-origin",
886+
"Strict-Transport-Security",
887+
"max-age=31536000; includeSubDomains",
888+
"X-Content-Type-Options",
889+
"nosniff",
890+
"X-Frame-Options",
891+
"SAMEORIGIN",
892+
"X-XSS-Protection",
893+
"0",
894+
"Server",
895+
"cloudflare",
896+
"CF-RAY",
897+
"84373e95a9fb683c-BUD",
898+
],
899+
);

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,75 @@ describe("with gql", () => {
108108
);
109109
},
110110
);
111+
112+
it("correctly passes variables from meta.gqlVariables to the query", async () => {
113+
const client = createClient("hasura-default");
114+
const { data } = await dataProvider(client).create({
115+
resource: "posts",
116+
variables: {
117+
content: "Lorem ipsum dolor sit amet.",
118+
title: "Lorem ipsum dolore",
119+
category_id: "ef49aebd-abcc-4bac-b064-a63b31f2e8ce",
120+
},
121+
meta: {
122+
gqlMutation: gql`
123+
mutation CreatePost(
124+
$object: posts_insert_input!,
125+
$includeCategory: Boolean = false
126+
) {
127+
insert_posts_one(object: $object) {
128+
id
129+
title
130+
content
131+
category @include(if: $includeCategory) {
132+
id
133+
}
134+
}
135+
}
136+
`,
137+
gqlVariables: {
138+
includeCategory: true,
139+
},
140+
},
141+
});
142+
143+
expect(data["title"]).toEqual("Lorem ipsum dolore");
144+
expect(data["content"]).toEqual("Lorem ipsum dolor sit amet.");
145+
expect(data["category"]["id"]).toEqual(
146+
"ef49aebd-abcc-4bac-b064-a63b31f2e8ce",
147+
);
148+
});
149+
150+
it("doesn't pass extra variables to the query without meta.gqlVariables", async () => {
151+
const client = createClient("hasura-default");
152+
const { data } = await dataProvider(client).create({
153+
resource: "posts",
154+
variables: {
155+
content: "Lorem ipsum dolor sit amet.",
156+
title: "Lorem ipsum dolore",
157+
category_id: "ef49aebd-abcc-4bac-b064-a63b31f2e8ce",
158+
},
159+
meta: {
160+
gqlMutation: gql`
161+
mutation CreatePost(
162+
$object: posts_insert_input!,
163+
$includeCategory: Boolean = false
164+
) {
165+
insert_posts_one(object: $object) {
166+
id
167+
title
168+
content
169+
category @include(if: $includeCategory) {
170+
id
171+
}
172+
}
173+
}
174+
`,
175+
},
176+
});
177+
178+
expect(data["title"]).toEqual("Lorem ipsum dolore");
179+
expect(data["content"]).toEqual("Lorem ipsum dolor sit amet.");
180+
expect(data["category"]).toBeUndefined();
181+
});
111182
});

0 commit comments

Comments
 (0)