Skip to content

Commit

Permalink
Merge pull request #73 from postmanlabs/feature/fix-gql-operationname…
Browse files Browse the repository at this point in the history
…-issue

Fixed an issue where GraphQL bodies were not generated correctly.
  • Loading branch information
VShingala authored Dec 12, 2023
2 parents ec9be75 + 039ad8f commit 134b4bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ var program,
identifyGraphqlRequest: function (dataString, contentType) {
try {
const rawDataObj = _.attempt(JSON.parse, this.escapeJson(dataString));

if (contentType === 'application/json' && rawDataObj && !_.isError(rawDataObj)) {
if (!_.has(rawDataObj, 'query') || !_.isString(rawDataObj.query)) {
return { result: false };
Expand All @@ -721,17 +722,14 @@ var program,
if (!_.isString(rawDataObj.operationName)) {
return { result: false };
}
delete rawDataObj.operationName;
}
else {
rawDataObj.operationName = '';
}
if (_.keys(rawDataObj).length === 3) {
if (_.keys(rawDataObj).length === 2) {
const graphqlVariables = JSON.stringify(rawDataObj.variables, null, 2);
return {
result: true,
graphql: {
query: rawDataObj.query,
operationName: rawDataObj.operationName,
variables: graphqlVariables === '{}' ? '' : graphqlVariables
}
};
Expand Down
12 changes: 12 additions & 0 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,18 @@ describe('Curl converter should', function() {
done();
});

it('[Github #12349]: should correctly convert graphql queries without operationName', function(done) {
var result = Converter.convertCurlToRequest(`curl --location 'https://spacex-production.up.railway.app' \\
--header 'Content-Type: application/json' \\
--data '{"query":"query getCompanyData {\r\n company {\r\n ceo\r\n }\r\n}","variables":{}}'`);

expect(result.body).to.have.property('mode', 'graphql');
expect(result.body.graphql.query).to.eql('query getCompanyData {\r\n company {\r\n ceo\r\n }\r\n}');
expect(result.body.graphql.variables).to.eql('');
expect(result.body.graphql).to.not.have.property('operationName');
done();
});

describe('[Github #8843]: It should recognize non-apostrophed ("...") url with multi-param', function() {
it('in case where there is multiple params with & in between in the url (https)', function(done) {
convert({
Expand Down

0 comments on commit 134b4bb

Please sign in to comment.