2016-09-12 07:47:34 +03:00
Getting started
===============
2019-06-10 02:49:56 +03:00
Introduction
------------
2016-11-05 17:31:56 +03:00
What is GraphQL?
2019-06-10 02:49:56 +03:00
~~~~~~~~~~~~~~~~
GraphQL is a query language for your API.
It provides a standard way to:
* *describe data provided by a server* in a statically typed **Schema**
* *request data* in a **Query** which exactly describes your data requirements and
* *receive data* in a **Response** containing only the data you requested.
For an introduction to GraphQL and an overview of its concepts, please refer to `the official GraphQL documentation`_ .
.. _the official GraphQL documentation: http://graphql.org/learn/
What is Graphene?
~~~~~~~~~~~~~~~~~
Graphene is a library that provides tools to implement a GraphQL API in Python using a *code-first* approach.
2019-09-26 02:57:53 +03:00
Compare Graphene's *code-first* approach to building a GraphQL API with *schema-first* approaches like `Apollo Server`_ (JavaScript) or Ariadne_ (Python). Instead of writing GraphQL **Schema Definition Language (SDL)** , we write Python code to describe the data provided by your server.
2019-06-10 02:49:56 +03:00
.. _Apollo Server: https://www.apollographql.com/docs/apollo-server/
2021-09-21 20:00:19 +03:00
.. _Ariadne: https://ariadnegraphql.org/
2019-06-10 02:49:56 +03:00
2019-12-26 23:02:28 +03:00
Graphene is fully featured with integrations for the most popular web frameworks and ORMs. Graphene produces schemas that are fully compliant with the GraphQL spec and provides tools and patterns for building a Relay-Compliant API as well.
2019-06-10 02:49:56 +03:00
An example in Graphene
----------------------
Let’ s build a basic GraphQL schema to say "hello" and "goodbye" in Graphene.
2022-11-16 23:27:34 +03:00
When we send a **Query** requesting only one **Field** , `` hello `` , and specify a value for the `` firstName `` **Argument** ...
2019-06-10 02:49:56 +03:00
.. code ::
{
2022-11-16 23:27:34 +03:00
hello(firstName: "friend")
2019-06-10 02:49:56 +03:00
}
2016-11-05 17:31:56 +03:00
2019-06-10 02:49:56 +03:00
...we would expect the following Response containing only the data requested (the ``goodbye`` field is not resolved).
.. code ::
{
"data": {
"hello": "Hello friend!"
}
}
2016-11-05 17:31:56 +03:00
2016-09-12 07:47:34 +03:00
Requirements
2019-06-10 02:49:56 +03:00
~~~~~~~~~~~~
2016-09-12 07:47:34 +03:00
2024-06-13 17:34:16 +03:00
- Python (3.8, 3.9, 3.10, 3.11, 3.12, pypy)
2021-11-14 01:15:18 +03:00
- Graphene (3.0)
2016-09-12 07:47:34 +03:00
Project setup
2019-06-10 02:49:56 +03:00
~~~~~~~~~~~~~
2016-09-12 07:47:34 +03:00
.. code :: bash
2021-11-14 01:15:18 +03:00
pip install "graphene>=3.0"
2016-09-12 07:47:34 +03:00
Creating a basic Schema
2019-06-10 02:49:56 +03:00
~~~~~~~~~~~~~~~~~~~~~~~
2016-09-12 07:47:34 +03:00
2019-06-10 02:49:56 +03:00
In Graphene, we can define a simple schema using the following code:
2016-09-12 07:47:34 +03:00
.. code :: python
2019-06-10 02:49:56 +03:00
from graphene import ObjectType, String, Schema
class Query(ObjectType):
2022-11-16 23:27:34 +03:00
# this defines a Field `hello` in our Schema with a single Argument `first_name`
# By default, the argument name will automatically be camel-based into firstName in the generated schema
hello = String(first_name=String(default_value="stranger"))
2019-06-10 02:49:56 +03:00
goodbye = String()
# our Resolver method takes the GraphQL context (root, info) as well as
2022-11-16 23:27:34 +03:00
# Argument (first_name) for the Field and returns data for the query Response
def resolve_hello(root, info, first_name):
return f'Hello {first_name}!'
2019-06-10 02:49:56 +03:00
def resolve_goodbye(root, info):
return 'See ya!'
schema = Schema(query=Query)
2016-09-12 07:47:34 +03:00
2019-06-10 02:49:56 +03:00
A GraphQL **Schema** describes each **Field** in the data model provided by the server using scalar types like *String* , *Int* and *Enum* and compound types like *List* and *Object* . For more details refer to the Graphene :ref: `TypesReference` .
2016-09-12 07:47:34 +03:00
2019-06-10 02:49:56 +03:00
Our schema can also define any number of **Arguments** for our **Fields** . This is a powerful way for a **Query** to describe the exact data requirements for each **Field** .
2016-09-12 07:47:34 +03:00
2019-06-10 02:49:56 +03:00
For each **Field** in our **Schema** , we write a **Resolver** method to fetch data requested by a client's **Query** using the current context and **Arguments** . For more details, refer to this section on :ref: `Resolvers` .
Schema Definition Language (SDL)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-05-28 16:41:38 +03:00
In the `GraphQL Schema Definition Language`_ , we could describe the fields defined by our example code as shown below.
2019-06-10 02:49:56 +03:00
.. _GraphQL Schema Definition Language: https://graphql.org/learn/schema/
.. code ::
type Query {
2022-11-16 23:27:34 +03:00
hello(firstName: String = "stranger"): String
2019-06-10 02:49:56 +03:00
goodbye: String
}
Further examples in this documentation will use SDL to describe schema created by ObjectTypes and other fields.
2016-09-12 07:47:34 +03:00
Querying
2019-06-10 02:49:56 +03:00
~~~~~~~~
2016-09-12 07:47:34 +03:00
2019-06-10 02:49:56 +03:00
Then we can start querying our **Schema** by passing a GraphQL query string to `` execute `` :
2016-09-12 07:47:34 +03:00
.. code :: python
2019-06-10 02:49:56 +03:00
# we can query for our field (with the default argument)
query_string = '{ hello }'
result = schema.execute(query_string)
print(result.data['hello'])
2020-02-21 14:15:51 +03:00
# "Hello stranger!"
2016-09-12 07:47:34 +03:00
2018-10-16 14:59:25 +03:00
# or passing the argument in the query
2022-11-16 23:27:34 +03:00
query_with_argument = '{ hello(firstName: "GraphQL") }'
2019-06-10 02:49:56 +03:00
result = schema.execute(query_with_argument)
print(result.data['hello'])
# "Hello GraphQL!"
Next steps
~~~~~~~~~~
Congrats! You got your first Graphene schema working!
2018-10-16 14:59:25 +03:00
2019-06-10 02:49:56 +03:00
Normally, we don't need to directly execute a query string against our schema as Graphene provides many useful Integrations with popular web frameworks like Flask and Django. Check out :ref: `Integrations` for more information on how to get started serving your GraphQL API.