Skip to content

Commit

Permalink
Fix GraphQL variables
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier committed Oct 22, 2024
1 parent d91e60f commit 59b0b73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion plugins/exporter-curl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function pluginHookExport(_ctx: Context, request: Partial<HttpReque
xs.push(NEWLINE);
}
} else if (typeof request.body?.query === 'string') {
const body = { query: request.body.query, variables: request.body.variables ?? undefined };
const body = { query: request.body.query || '', variables: maybeParseJSON(request.body.variables, {}) };
xs.push('--data-raw', `${quote(JSON.stringify(body))}`);
xs.push(NEWLINE);
} else if (typeof request.body?.text === 'string') {
Expand Down Expand Up @@ -92,3 +92,11 @@ function quote(arg: string): string {
function onlyEnabled(v: { name?: string; enabled?: boolean }): boolean {
return v.enabled !== false && !!v.name;
}

function maybeParseJSON(v: any, fallback: any): string {
try {
return JSON.parse(v);
} catch (err) {
return fallback;
}
}
2 changes: 1 addition & 1 deletion plugins/exporter-curl/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('exporter-curl', () => {
bodyType: 'graphql',
body: {
query : '{foo,bar}',
variables: {a: 'aaa', b: 'bbb'},
variables: '{"a": "aaa", "b": "bbb"}',
},
}),
).toEqual(
Expand Down

0 comments on commit 59b0b73

Please sign in to comment.