In this activity we develop and deploy a GraphQL server to handle information related to a tournament.
Here is the GraphQL schema of our service:
The client can query information about the teams ("allTeams
"), in which case he can request the name of the team, the list of players or the captain, and information about a player contains the name, the birth year and the gender. The client can also request information about a player designated by his name. For instance, if you want the name of the teams and the gender of the captains, the request looks like:
{
allTeams {
name
captain {
gender
}
}
Multiple libraries provide GraphQL capabilities to your Python programs. graphene is a popular solution, with a Flask integration. It follows a code-first approach: the schema is generated from the Python code. To follow a design-first approach, we'll favor a popular alternative: ariadne.
Check to see if the necessary packages are installed: pip show ariadne graphql-core
and if not, install them: pip install ariadne graphql-core
Copy the GraphQL schema above in a file named schema.graphql
.
Copy the Python code below in server.py
.
Start the server. If you open a web browser to http://localhost:5000/graphql a GraphQL Client (called GraphiQL) opens and lets you compose your request in the left frame, send it using the arrow button, and the result is displayed in the right frame.
You can also use the Altair Firefox extension to explore the schema and get the skeleton of requests.
You can also use curl.