Create a Tartiflette server
Make sure you followed the Project initialization steps.
To help you with the code to build the "Tartiflette recipes manager" application, we will provide some code blocks. Simply copy/paste the code into the appropriate file (which has been created at the previous step and which should be empty for now).
Write code
recipes_manager/sdl/Query.graphql
As explained on the homepage, Tartiflette use the official Schema Definition Language to define the GraphQL API schema.
We will start to define the Query
schema part, first:
enum UnitMeasurement {
GRAM
LITER
UNIT
}
type Ingredient {
name: String!
quantity: Float!
unitMeasurement: UnitMeasurement!
}
type Recipe {
id: Int!
name: String!
ingredients: [Ingredient!]!
cookingTime: Int!
}
type Query {
recipes: [Recipe!]
recipe(id: Int!): Recipe
}
recipes_manager/app.py
The following file will be in charge of running the HTTP server (web.run_app
function).
register_graphql_handlers
will attach HTTP handlers to theaiohttp
applicationapp
: theaiohttp
application (created withweb.Application()
)engine_sdl
: the Tartiflette engine which will be used by the HTTP handlersengine_modules
: the modules list which will be loaded by Tartiflette at cooking time (building process)executor_http_endpoint
: endpoint path where the GraphQL API will be exposedexecutor_http_methods
: HTTP methods on which the GraphQL API will be exposed (we recommend to expose it only onPOST
)graphiql_enabled
: a GraphiQL client to browse the GraphQL, used mostly for development
import os
from aiohttp import web
from tartiflette_aiohttp import register_graphql_handlers
def run() -> None:
"""
Entry point of the application.
"""
web.run_app(
register_graphql_handlers(
app=web.Application(),
engine_sdl=os.path.dirname(os.path.abspath(__file__)) + "/sdl",
engine_modules=[
"recipes_manager.query_resolvers",
"recipes_manager.mutation_resolvers",
"recipes_manager.subscription_resolvers",
"recipes_manager.directives.rate_limiting",
"recipes_manager.directives.auth",
],
executor_http_endpoint="/graphql",
executor_http_methods=["POST"],
graphiql_enabled=True,
)
)
recipes_manager/__main__.py
This is the entrypoint of our application, we will not go into more details here.
#!/usr/bin/env python
import sys
from recipes_manager.app import run
if __name__ == "__main__":
sys.exit(run())
Launch the app
We are now ready to launch our GraphQL API. (make sure that your shell is inside the virtual environment with the pipenv shell
command).
python -m recipes_manager
Your Tartiflette recipes manager is now live.
But there is nothing in it... yet. We are now going to implement our first resolver.