python-gql
Python schema-first GraphQL library based on GraphQL-core.
Requirements
Python 3.7+
Installation
pip install python-gql
Getting start
import graphql
from gql import gql, make_schema, query, mutate
type_defs = gql("""
type Query {
hello(name: String!): String!
}
type Post {
author: String!
comment: String!
}
type Mutation {
addPost(author: String, comment: String): Post!
}
""")
@query
def hello(parent, info, name: str) -> str:
return name
@mutate
def add_post(parent, info, author: str = None, comment: str = None) -> dict:
return {'author': author, 'comment': comment}
schema = make_schema(type_defs)
q = """
query {
hello(name: "graphql")
}
"""
result = graphql.graphql_sync(schema, q)
print(result.data)
# result: {'hello': 'graphql'}
q = """
mutation {
addPost(author: "syfun", comment: "This is a good library.") {
author
comment
}
}
"""
result = graphql.graphql_sync(schema, q)
print(result.data)
# result: {'addPost': {'author': 'syfun', 'comment': 'This is a good library.'}}
Build schema
This library is schema-first, so you must build a schema explicitly.
Here, we have two methods to build a schema, by a type definitions or a schema file.
from gql import gql, make_schema
type_defs = gql("""
type Query {
hello(name: String!): String!
}
""")
schema = make_schema(type_defs)
gqlfunction will check your type definitions syntax.
from gql import make_schema_from_file
schema = make_schema_from_file('./schema.graphql')
Resolver decorators
In Python,
decoratoris my favorite function, it save my life!
We can use query, mutation, subscribe to bind functions to GraphQL resolvers.
@query
def hello(parent, info, name: str) -> str:
return name
These decorators will auto convert the snake function to camel one.
# add_port => addPost
@mutate
def add_post(parent, info, author: str = None, comment: str = None) -> dict:
return {'author': author, 'comment': comment}
When the funcation name different from the resolver name, you can give a name argument to these decorators.
@query('hello')
def hello_function(parent, info, name: str) -> str:
return name
About subscribe, please see gql-subscriptions.
Enum type decorator
Use enum_type decorator with a python Enum class.
from enum import Enum
from gql import enum_type
@enum_type
class Gender(Enum):
MALE = 1
FEMALE = 2
Custom Scalar
Use scalar_type decorator with a python class.
from gql import scalar_type
@scalar_type
class JSONString:
description = "The `JSONString` represents a json string."
@staticmethod
def serialize(value: Any) -> str:
return json.dumps(value)
@staticmethod
def parse_value(value: Any) -> dict:
if not isinstance(value, str):
raise TypeError(f'JSONString cannot represent non string value: {inspect(value)}')
return json.loads(value)
@staticmethod
def parse_literal(ast, _variables=None):
if isinstance(ast, StringValueNode):
return json.loads(ast.value)
return INVALID
Custom directive
from gql import gql, make_schema, query, SchemaDirectiveVisitor
from gql.resolver import default_field_resolver
type_defs = gql("""
directive @upper on FIELD_DEFINITION
type Query {
hello(name: String!): String! @upper
}
""")
class UpperDirective(SchemaDirectiveVisitor):
def visit_field_definition(self, field, object_type):
original_resolver = field.resolve or default_field_resolver
def resolve_upper(obj, info, **kwargs):
result = original_resolver(obj, info, **kwargs)
if result is None:
return None
return result.upper()
field.resolve = resolve_upper
return field
schema = make_schema(type_defs, directives={'upper': UpperDirective})
Apollo Federation
Thanks to Ariadne
Framework support
Metadata
Release files for python-gql 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-gql-0.2.4.tar.gz | 23.5 kB | Without provenance | Details for python-gql-0.2.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_gql-0.2.4-py3-none-any.whl | Python 3 | none | any | Details for python_gql-0.2.4-py3-none-any.whl |
Total release size: 48.2 kB
Release files / python-gql-0.2.4.tar.gz
| Download URL | python-gql-0.2.4.tar.gz |
|---|---|
| Size | 23.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ecfd415791f8a1d73e76ecc46d8087ba7a5471820ea27581af20f71149f386ab
|
|
BLAKE2b-256 checksum How to use checksums |
e782c8fd07c29caca56cdda7f05d89b4af82ea95ebe4d326709eca5c1df7d6e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.9.0 Darwin/20.3.0
|
Release files / python_gql-0.2.4-py3-none-any.whl
| Download URL | python_gql-0.2.4-py3-none-any.whl |
|---|---|
| Size | 24.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f29c045e1fc50ad430f8aa9aaa6fef01a8dba4b3f6c5641f6a1bc5c3279f5a11
|
|
BLAKE2b-256 checksum How to use checksums |
d5be9bf9ade6c7b864cd24d8e5f543ecee7a0d903d5b676e59d246be401936ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.9.0 Darwin/20.3.0
|
Security for python-gql 0.2.4
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.