gql-defrag
Do you have complex GraphQL queries that are made up of lot of fragments? Do you want to figure out who is querying all those fields? Then gql-defrag can help you. It takes in the text of all your queries and fragments, and returns a new query that inlines all the fragments and labels fields with their origin.
Example:
>>> from gql_defrag import Defragmenter
>>> defrag = Defragmenter(["""
... query SomeQuery {
... field
... name
... ...SomeFragment
... }
...
... fragment SomeFragment on SomeType {
... fragmentField
... field
... }
... """])
>>> print(defrag.defragment("SomeQuery"))
query SomeQuery {
field @gql_defrag_source(name: "SomeQuery") @gql_defrag_source(name: "SomeQuery -> SomeFragment")
fragmentField @gql_defrag_source(name: "SomeQuery -> SomeFragment")
name @gql_defrag_source(name: "SomeQuery")
}
Programmatic usage
gql_defrag.Defragmenter(documents: Sequence[str])
Instantiate a Defragmenter to start defragmenting some queries. Pass in a series of
GraphQL documents. Each may contain one or more queries, fragments, or both.
Defragmenter.add_document(document: str) -> None
Add a document to the Defragmenter. The document may contain any number of queries or
fragments.
Defragmenter.defragment(query_name: str, *, add_source: bool = True) -> str
Return a defragmented version of the operation (query, mutation, or subscription) named
query_name. If add_source is True, add @gql_defrag_source directives explaining
what sequence of fragments triggered each field.
Defragmenter.defragment_all(*, add_source: bool = True) -> Iterable[tuple[str, str]]
Yields pairs of (query name, defragmented query) for all operations that the
Defragmenter knows about.
Command-line usage
$ python -m gql_defrag --help
usage: __main__.py [-h] [--js-dir JS_DIR] [--graphql-dir GRAPHQL_DIR] [--relay-dir RELAY_DIR] [--output-dir OUTPUT_DIR] [--include-source]
Defrag GraphQL queries
options:
-h, --help show this help message and exit
--js-dir JS_DIR Directory with JavaScript files containing GraphQL fragments and queries
--graphql-dir GRAPHQL_DIR
Directory with GraphQL files with .graphql extensions
--relay-dir RELAY_DIR
Directory with Relay files with .graphql.ts extensions
--output-dir OUTPUT_DIR
Directory to write defragmented queries to
--include-source Include verbose source information
The command-line interface collects GraphQL queries from one or more directories and outputs defragmented queries to an output directory.
There are three ways to find GraphQL queries:
--graphql-dirlooks at a directory containing.graphqlfiles that contain GraphQL queries--js-dirlooks at a directory containing JavaScript or TypeScript files that contain pieces of GraphQL within template literals prefixed withgraphqlorgql. The code looks at files with the extensions.js,.jsx,.ts, and.tsx.--relay-dirlooks at a directory containing Relay generated files with the.graphql.tsextension.
Defragmented files are placed in the directory specified by --output-dir. If the
directory does not exist, it is created. The file names are of the form
<query name>.graphql.
If --include-source is given, @gql_defrag_source are added with precise source
information for each field.
Changelog
Version 0.2.1 (November 6, 2024)
- Fix bug where type conditions on fragments were not preserved. Fix contributed by Donggu Kang.
Version 0.2.0 (September 28, 2023)
- No changes.
Version 0.1.1 (July 25, 2023)
- Fix bug with handling of fields that contain "null" in their name
Version 0.1.0 (May 3, 2023)
Initial public release.
Release files for gql-defrag 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gql_defrag-0.2.1.tar.gz | 6.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gql_defrag-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / gql_defrag-0.2.1.tar.gz
| Download URL | gql_defrag-0.2.1.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c08339352a87a1b0bba3b5c92c0a899f1ad2f4f8d72fb6d44887beb632411cb
|
|
BLAKE2b-256 checksum How to use checksums |
8204855fd0dcdee736e6655e25b2b71f85fa6b4c6589df0398d851cd0a0f7fa2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2024.
Transparency logRelease files / gql_defrag-0.2.1-py3-none-any.whl
| Download URL | gql_defrag-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
69df21110cbf600897fc87f04fa6c01b66cf1ba839c6a8dcaa4aef5de4c0affe
|
|
BLAKE2b-256 checksum How to use checksums |
d325c28f7e7a01abb3a48acaa7719be88cdff8fb7d37c3430e5e15d69deee482
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2024.
Transparency log