This post is a summary of the best python libraries for GraphQL. Python in recent years is starting to be on the list of top programming language. GraphQL is emerging but very promising query language and execution engine tied to any backend service.
Python is one of the most popular languages used in data science, machine learning and AI systems. GraphQL was introduced by Facebook as an alternative to REST and it’s popular of flexibility on handling complex systems.
Ariadne is a Python library for implementing GraphQL servers using schema-first approach.
Ariadne is a Python library for implementing GraphQL servers.
Features
pip install ariadne
The following example creates an API defining Person type and single query field people returning a list of two persons. It also starts a local dev server with GraphQL Playground available on the http://127.0.0.1:8000 address. Start by installing uvicorn, an ASGI server we will use to serve the API:
Start by installing uvicorn, an ASGI server we will use to serve the API:
pip install uvicorn
Then create an example.py file for your example application:
from ariadne import ObjectType, QueryType, gql, make_executable_schema
from ariadne.asgi import GraphQL
# Define types using Schema Definition Language (https://graphql.org/learn/schema/)
# Wrapping string in gql function provides validation and better error traceback
type_defs = gql("""
type Query {
people: [Person!]!
}
type Person {
firstName: String
lastName: String
age: Int
fullName: String
}
""")
# Map resolver functions to Query fields using QueryType
query = QueryType()
# Resolvers are simple python functions
@query.field("people")
def resolve_people(*_):
return [
{"firstName": "John", "lastName": "Doe", "age": 21},
{"firstName": "Bob", "lastName": "Boberson", "age": 24},
]
# Map resolver functions to custom type fields using ObjectType
person = ObjectType("Person")
@person.field("fullName")
def resolve_person_fullname(person, *_):
return "%s %s" % (person["firstName"], person["lastName"])
# Create executable GraphQL schema
schema = make_executable_schema(type_defs, [query, person])
# Create an ASGI app using the schema, running in debug mode
app = GraphQL(schema, debug=True)
Strawberry is a new GraphQL library for Python 3, inspired by dataclasses. An initial version of Strawberry has been released on GitHub. Strawberry was created by @patrick91 who is also an organizer of @pyconit. It was originally announced during Python Pizza Berlin.
pip install strawberry-graphql
Create a file called app.py with the following code:
import strawberry
@strawberry.type
class User:
name: str
age: int
@strawberry.type
class Query:
@strawberry.field
def user(self, info) -> User:
return User(name="Patrick", age=100)
schema = strawberry.Schema(query=Query)
This will create a GraphQL schema defining a User type and a single query field user that will return a hard-coded user.
To run the debug server run the following command:
strawberry run server app
Open the debug server by clicking on the following link: http://0.0.0.0:8000/graphql
This will open a GraphQL playground where you can test the API.
Graphene is a Python library for building GraphQL schemas/types fast and easily.
Graphene has multiple integrations with different frameworks:
Also, Graphene is fully compatible with the GraphQL spec, working seamlessly with all GraphQL clients, such as Relay, Apollo and gql.
For instaling graphene, just run this command in your shell
pip install "graphene>=2.0"
Here is one example for you to get started:
class Query(graphene.ObjectType):
hello = graphene.String(description='A typical hello world')
def resolve_hello(self, info):
return 'World'
schema = graphene.Schema(query=Query)
Then Querying graphene.Schema is as simple as:
query = '''
query SayHello {
hello
}
'''
result = schema.execute(query)
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
#python #graphql #web-development