Skip to content

Commit

Permalink
fix: variables in gql Query body are replaced (AnWeber/vscode-httpyac…
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Jun 15, 2024
1 parent 5cc3d3a commit d1b4ec2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [unreleased]
### Fix
- variables in gql Query body are replaced (AnWeber/vscode-httpyac#303)

## [6.14.0] ( 2024-06-01)
### Features
- add `--tag` support to cli to only execute httpRegion with defined tag (#693)
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/graphql/gqlAction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { log, userInteractionProvider } from '../../io';
import { ProcessorContext, Request } from '../../models';
import { ProcessorContext, Request, VariableType } from '../../models';
import * as utils from '../../utils';

export type GqlLoadData = string | ((context: ProcessorContext) => Promise<string | undefined>);
Expand Down Expand Up @@ -60,8 +60,9 @@ export class GqlAction {
}
}
}
const replacedQuery = await utils.replaceVariables(query, VariableType.body, context);
const gqlRequestBody: GqlPostRequest = {
query,
query: utils.isString(replacedQuery) ? replacedQuery : query,
};
if (this.gqlData.operationName) {
gqlRequestBody.operationName = this.gqlData.operationName;
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/graphql/test/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ query launchesQuery($limit: Int!){
);
});

it('should replace variables in query', async () => {
initFileProvider();
const requests = initHttpClientProvider();

await sendHttp(
`
@sku=1
POST /graphql
query getit {
product( id: "gid://shopify/Product/{{sku}}" ) {
id
status
}
}
`
);

expect(requests[0].url).toBe(`/graphql`);
expect(requests[0].body).toBe(
'{"query":"query getit {\\n product( id: \\"gid://shopify/Product/1\\" ) {\\n id\\n status\\n }\\n}","operationName":"getit"}'
);
});

it('query + operation + variable replacement', async () => {
initFileProvider();
const requests = initHttpClientProvider();
Expand Down

0 comments on commit d1b4ec2

Please sign in to comment.