postmanlabs / postmanlabs/postman-code-generators
Improve Python requests GraphQL codegen readability and fix missing json import
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 1k
- Forks
- 381
- PR merge metrics
- No merged PRs in 30d
Description
Summary
I’ve been using the python-requests code generator for GraphQL requests and ran into two issues:
- The generated Python snippet makes the GraphQL query and variables hard to read, because they effectively end up in a single JSON string.
- In some GraphQL cases, the snippet uses
json.dumps(...)but does not always importjson, which leads to a runtime error.
This issue describes the current behavior, why it’s problematic, and a proposed improvement. I’ve also implemented a small change set that I can open as a PR.
Current behavior
1. GraphQL query and variables are not clearly separated
For GraphQL requests, the generated Python code:
- Builds
payloadin a way that makes the GraphQL query and variables live inside one opaque JSON string. - Does not expose
queryandvariablesas separate Python variables. - As a result, when I copy the generated code into my editor, it’s hard to:
- See the actual GraphQL query at a glance.
- Inspect and tweak the variables without digging into a long JSON string.
From a user perspective, I want to think about GraphQL as “query” + “variables,” but the current codegen doesn’t reflect that structure clearly.
2. json.dumps used without guaranteed import json
In some GraphQL scenarios, the generated Python snippet:
- Calls
json.dumps(...)to build the request body. - But
import jsonis only added when theContent-Typeheader is explicitly JSON (e.g.application/jsonor something ending in+json).
If the GraphQL request doesn’t have a JSON Content-Type header (or has a non-standard one), the snippet can end up using json.dumps without importing json, causing:
NameError: name 'json' is not defined
This breaks the “copy and run” experience for the generated snippet.
User story
As a developer using Postman’s Python requests code generator for GraphQL:
- I want the generated snippet to show:
- A clear, readable GraphQL query as a Python string.
- A clear, readable variables object as a Python dictionary or similar structure.
- I also want the snippet to:
- Run without modification (no
NameErrorfrom missing imports).
- Run without modification (no
Right now, the generated code makes it hard to read and edit the query and variables separately, and it may fail at runtime if json wasn’t imported.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the python-requests code generator and reproduce a GraphQL request with a non-JSON Content-Type to inspect the generated snippet. Done means the query and variables are separately readable in the output and any use of json.dumps is accompanied by the required import.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100