Skip to content

Make Python GraphQL snippets more readable and ensure json is imported#814

Open
Dev22603 wants to merge 2 commits intopostmanlabs:developfrom
Dev22603:develop
Open

Make Python GraphQL snippets more readable and ensure json is imported#814
Dev22603 wants to merge 2 commits intopostmanlabs:developfrom
Dev22603:develop

Conversation

@Dev22603
Copy link

@Dev22603 Dev22603 commented Mar 15, 2026

Make Python GraphQL snippets more readable and fix json import

This PR solves the #813

This PR updates the python-requests code generator for GraphQL requests to:

  • Generate clearer Python snippets where the GraphQL query and variables are separate variables, and
  • Always include import json when GraphQL payloads use json.dumps.

Changes

  • codegens/python-requests/lib/util/parseBody.js

    • In the graphql branch:
      • Emit a triple-quoted query variable.

      • Convert GraphQL variables to a Python structure (variables) using the existing helper.

      • Build payload as:

        payload = json.dumps({
            "query": query,
            "variables": variables
        })
  • codegens/python-requests/lib/python-requests.js

    • Keep the existing logic that imports json for JSON Content-Type.
    • Additionally import json whenever request.body.mode === 'graphql' so GraphQL snippets can always call json.dumps safely.

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.dumps was used without import json, causing NameError: name 'json' is not defined.
These changes address both readability and that runtime error.

Testing

  • npm test python-requests
    • ESLint: no new errors (only existing warnings remain).
    • Newman + Mocha tests for python-requests pass, including the GraphQL case (POST graphql body(json) with raw).
    • Manually verified that the generated GraphQL Python snippet now has query, variables, payload as described above, and includes import json.

… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant