2016-09-12 07:47:34 +03:00
|
|
|
|
Getting started
|
|
|
|
|
===============
|
|
|
|
|
|
2016-11-05 17:31:56 +03:00
|
|
|
|
What is GraphQL?
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
For an introduction to GraphQL and an overview of its concepts, please refer
|
2017-03-10 11:05:01 +03:00
|
|
|
|
to `the official introduction <http://graphql.org/learn/>`_.
|
2016-11-05 17:31:56 +03:00
|
|
|
|
|
2016-09-12 07:47:34 +03:00
|
|
|
|
Let’s build a basic GraphQL schema from scratch.
|
|
|
|
|
|
|
|
|
|
Requirements
|
|
|
|
|
------------
|
|
|
|
|
|
2017-07-24 10:01:44 +03:00
|
|
|
|
- Python (2.7, 3.4, 3.5, 3.6, pypy)
|
|
|
|
|
- Graphene (2.0)
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
Project setup
|
|
|
|
|
-------------
|
|
|
|
|
|
|
|
|
|
.. code:: bash
|
|
|
|
|
|
2017-07-24 10:01:44 +03:00
|
|
|
|
pip install "graphene>=2.0"
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
Creating a basic Schema
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
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
|
2017-04-13 20:32:49 +03:00
|
|
|
|
one field: ``hello`` and an input name. And when we query it, it should return ``"Hello {name}"``.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
import graphene
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
2017-07-24 10:01:44 +03:00
|
|
|
|
hello = graphene.String(name=graphene.String(default_value="stranger"))
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
2017-07-27 13:00:21 +03:00
|
|
|
|
def resolve_hello(self, info, name):
|
2017-07-24 09:16:51 +03:00
|
|
|
|
return 'Hello ' + name
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
|
|
|
|
|
|
Querying
|
|
|
|
|
--------
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
Then we can start querying our schema:
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
result = schema.execute('{ hello }')
|
2017-04-13 20:32:49 +03:00
|
|
|
|
print result.data['hello'] # "Hello stranger"
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
Congrats! You got your first graphene schema working!
|