Python classes for generate of GraphQL queries.
Project description
GraphQL-Query
graphql_query is python classes for generating GraphQL queries. 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 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 the 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
andgraphql_query.InlineFragment
.
With arguments, variables and directive
For generation of the following query
query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
name
}
}
}
we have
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
# }
# }
# }
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for graphql_query-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3269e75ba6d3717e27b8bd26744560b5f6a66ff9f4600eede3315542ec37c0ab |
|
MD5 | 6480cc0a8b236a322f51db002be76c7c |
|
BLAKE2b-256 | f7f3a6011d0c83d513c80f120f3d6b8ac4b8654a45541e9a514d533754ca98bd |