Skip to main content

graphql-query

tag last-commit PyPI - Python Version license


graphql_query is a complete Domain Specific Language (DSL) for GraphQL query in Python. With this package you can to

  • generate a correct GraphQL query string from a python classes;
  • use and share similar Arguments, Variables and e.t.c between different queries;
  • easily add new fields to your query;
  • add Fragments and Directives to queries;
  • generate graphql_query classes from pydantic data-model;

The documentation for graphql_query can be found at https://denisart.github.io/graphql-query/.

Quickstart

Install with pip

pip install graphql_query

Simple query

Code for the simple query

{
  hero {
    name
  }
}

it is

from graphql_query import Operation, Query

hero = Query(name="hero", fields=["name"])
operation = Operation(type="query", queries=[hero])

print(operation.render())
"""
query {
  hero {
    name
  }
}
"""

The render method for the graphql_query.Operation object just returns the final string with a query. Inside the fields array of the graphql_query.Query object you can use

  • str (a field name);
  • object of graphql_query.Field type;
  • graphql_query.Fragment and graphql_query.InlineFragment.

Arguments, Variables and Directives

For generation of the following query

query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
    }
  }
}

we can use the following python code

from graphql_query import Argument, Directive, Field, Operation, Query, Variable

episode = Variable(name="episode", type="Episode")
withFriends = Variable(name="withFriends", type="Boolean!")

arg_episode = Argument(name="episode", value=episode)
arg_if = Argument(name="if", value=withFriends)

hero = Query(
    name="hero",
    arguments=[arg_episode],
    fields=[
        "name",
        Field(
            name="friends",
            fields=["name"],
            directives=[Directive(name="include", arguments=[arg_if])]
        )
    ]
)
operation = Operation(
    type="query",
    name="Hero",
    variables=[episode, withFriends],
    queries=[hero]
)
print(operation.render())
"""
query Hero(
  $episode: Episode
  $withFriends: Boolean!
) {
  hero(
    episode: $episode
  ) {
    name
    friends @include(
      if: $withFriends
    ) {
      name
    }
  }
}
"""

You can find other examples in the documentation.

Metadata

Release files for graphql-query 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphql-query 1.4.0
File Size Uploaded
graphql_query-1.4.0.tar.gz 26.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for graphql-query 1.4.0
File Interpreter ABI Platform
graphql_query-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.6 kB

Release files / graphql_query-1.4.0.tar.gz

Download URL graphql_query-1.4.0.tar.gz
Size 26.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1cfe5eeaad8b0ed67ac3d9c4023ee9743851f98c6b2f673c67088cf42ebb57bb
BLAKE2b-256 checksum
How to use checksums
8b64377beef6c10b798f2ece54cfd3577db20102176c3a155469b92b4a3e3881
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.17

Release files / graphql_query-1.4.0-py3-none-any.whl

Download URL graphql_query-1.4.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
376ed550a7812425befbefb870daa21ce1696590fcb78c015215a43a5d7e51b7
BLAKE2b-256 checksum
How to use checksums
47edec732d18dd016eb4d4fa590e392d22dd35c93f26f17f709596deeb780497
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.17

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page