For an introduction to GraphQL and an overview of its concepts, please refer to the official introduction.
Let’s build a basic GraphQL schema from scratch.
- Python (3.6+, pypy)
- Graphene (2.0)
pip install "graphene>=2.0"A GraphQL schema describes your data model, and provides a GraphQL server with an associated set of resolve methods that know how to fetch data.
We are going to create a very simple schema, with a Query with only
one field: hello and an input name. And when we query it, it should return "Hello
{argument}".
from graphene import ObjectType, Schema, String
class Query(ObjectType):
hello = String(argument=String(default_value="stranger"))
def resolve_hello(self, info, argument):
return f"Hello {argument}"
schema = Schema(query=Query)Then we can start querying our schema:
result = schema.execute("{ hello }")
print(result.data["hello"]) # "Hello stranger"
# or passing the argument in the query
result = schema.execute('{ hello (argument: "graph") }')
print(result.data["hello"]) # "Hello graph"Congrats! You got your first graphene schema working!