Make Python GraphQL snippets more readable and ensure json is imported#814
Open
Dev22603 wants to merge 2 commits intopostmanlabs:developfrom
Open
Make Python GraphQL snippets more readable and ensure json is imported#814Dev22603 wants to merge 2 commits intopostmanlabs:developfrom
Dev22603 wants to merge 2 commits intopostmanlabs:developfrom
Conversation
… triple-quoted string and variables as a separate Python structure, then build payload via json.dumps({"query": query, "variables": variables}).
Always add import json for GraphQL requests in python-requests snippets (in addition to existing JSON Content-Type logic) so generated code no longer fails with NameError: name 'json' is not defined.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make Python GraphQL snippets more readable and fix
jsonimportThis PR solves the #813
This PR updates the
python-requestscode generator for GraphQL requests to:import jsonwhen GraphQL payloads usejson.dumps.Changes
codegens/python-requests/lib/util/parseBody.jsgraphqlbranch:Emit a triple-quoted
queryvariable.Convert GraphQL variables to a Python structure (
variables) using the existing helper.Build
payloadas:codegens/python-requests/lib/python-requests.jsjsonfor JSONContent-Type.jsonwheneverrequest.body.mode === 'graphql'so GraphQL snippets can always calljson.dumpssafely.Rationale
Previously, the GraphQL query and variables effectively ended up inside a single JSON string, which made the generated snippet hard to read and edit.
In some GraphQL cases,
json.dumpswas used withoutimport json, causingNameError: name 'json' is not defined.These changes address both readability and that runtime error.
Testing
npm test python-requestspython-requestspass, including the GraphQL case (POST graphql body(json) with raw).query,variables,payloadas described above, and includesimport json.