Skip to main content

Complete Domain Specific Language (DSL) for GraphQL query in Python.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphql_query-1.4.0.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

graphql_query-1.4.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file graphql_query-1.4.0.tar.gz.

File metadata

  • Download URL: graphql_query-1.4.0.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for graphql_query-1.4.0.tar.gz
Algorithm Hash digest
SHA256 1cfe5eeaad8b0ed67ac3d9c4023ee9743851f98c6b2f673c67088cf42ebb57bb
MD5 e93672cfdd6fb8c8cc224f9d3760021c
BLAKE2b-256 8b64377beef6c10b798f2ece54cfd3577db20102176c3a155469b92b4a3e3881

See more details on using hashes here.

File details

Details for the file graphql_query-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for graphql_query-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 376ed550a7812425befbefb870daa21ce1696590fcb78c015215a43a5d7e51b7
MD5 00b3cb9ca2ff7abb0084e24ebf79662f
BLAKE2b-256 47edec732d18dd016eb4d4fa590e392d22dd35c93f26f17f709596deeb780497

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page