mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-29 09:19:49 +03:00
Add first part of the tutorial
This commit is contained in:
parent
315df258ef
commit
b4a2f20b1e
|
@ -3,6 +3,53 @@
|
||||||
Create your first schema
|
Create your first schema
|
||||||
========================
|
========================
|
||||||
|
|
||||||
|
What is a schema?
|
||||||
|
-----------------
|
||||||
|
|
||||||
TODO: explain what a schema is
|
A GraphQL schema describes your data model, and provides a GraphQL
|
||||||
TODO: explain what a query is
|
server with an associated set of resolve methods that know how to fetch
|
||||||
|
data.
|
||||||
|
|
||||||
|
For a more in depth explaination you can refer to
|
||||||
|
`the GraphQL documentation <http://graphql.org/learn/schema/>`_.
|
||||||
|
|
||||||
|
Let's start by creating a simple schema with a ``Query`` that has only
|
||||||
|
one field: ``hello``. When we query it will return ``"Hello world"``.
|
||||||
|
|
||||||
|
Let's create a ``schema.py`` file and let's add the following content in it:
|
||||||
|
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
hello = graphene.String()
|
||||||
|
|
||||||
|
def resolve_hello(self, info):
|
||||||
|
return 'Hello world'
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query)
|
||||||
|
|
||||||
|
All GraphQL schemas are typed, so we need to specify the types in python.
|
||||||
|
We do this by creating a ``Query`` class that extends ``graphene.ObjectType``,
|
||||||
|
which is Graphene's way to specify a GraphQL type.
|
||||||
|
|
||||||
|
We are specifying a single field in our Query. This field will be called ``hello``
|
||||||
|
and will be of type ``String``.
|
||||||
|
|
||||||
|
For each field then we have to specify a resolver method. In this case we are
|
||||||
|
creating a basic method called ``resolve_hello`` that just returns ``"Hello world"``.
|
||||||
|
|
||||||
|
Using the schema
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Then we can start querying our schema:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
result = schema.execute('{ hello }')
|
||||||
|
print(result.data['hello']) # "Hello world"
|
||||||
|
|
||||||
|
Later we will show you how to serve the schema under an HTTP endpoint
|
||||||
|
so that it can be used with JavaScript clients.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user