In this tutorial, we will explore how to integrate GraphQL with FastAPI, a modern web framework for building APIs with Python. GraphQL is a query language for APIs that allows clients to request only the data they need. FastAPI is a high-performance web framework for building APIs quickly and efficiently.
To get started, make sure you have Python installed on your machine. You can install FastAPI and GraphQL by running the following commands in your terminal:
pip install fastapi
pip install graphene
pip install uvicorn
Once you have all the necessary packages installed, let’s create a new project directory and navigate into it:
mkdir fastapi-graphql
cd fastapi-graphql
Create a new Python file called main.py
and open it in your favorite text editor. We will start by importing the necessary modules and creating a FastAPI app:
from fastapi import FastAPI
from graphene import ObjectType, String, Schema
from starlette.graphql import GraphQLApp
app = FastAPI()
Next, we will define a simple GraphQL schema using the graphene
library. In this example, we will create a Query
object with a single field called hello
that returns a greeting message:
class Query(ObjectType):
hello = String(name=String(default_value="stranger"))
def resolve_hello(self, info, name):
return f"Hello, {name}!"
Now, we will create a GraphQL schema using the Schema
class, passing in our Query
object:
schema = Schema(query=Query)
To serve our GraphQL API, we will create a new route in our FastAPI app that uses the GraphQLApp
class to expose our GraphQL schema:
@app.get("/graphql")
async def graphql():
return GraphQLApp(schema=schema)
Finally, we will run our FastAPI server using the uvicorn
package:
uvicorn main:app --reload
Navigate to http://localhost:8000/graphql
in your web browser to access the GraphiQL web interface for testing queries. You can execute the following query to test our hello
field:
query {
hello(name: "Alice")
}
You should see the following response:
{
"data": {
"hello": "Hello, Alice!"
}
}
Congratulations! You have successfully integrated GraphQL with FastAPI. You can now expand your schema with additional types and queries to build more complex APIs. Happy coding!
you haven't used ariadne library. but you said in the video.
Amazing Information ❤
Thanks for posting. I’m transitioning to a BE dev at my job and we use python on the BE. I’ve done this before with NodeJS, but Python’s code is even more simple than Node, and Node is pretty simple in the grand scheme of things.
starlette.graphql is deprecated in Starlette version >= 0.17.0. Any suggestion for replacement library to get along with this tutorial? Thanks.
starlette.graphql give me error please your help
I've one question, is it mandatory that Graphql is implemented in Backend than only we can use it in Frontend ?
Hello again ;
I must confess this, coming from MVC pattern with node js MERN stack, I find it hard to wrap my head around this fastapi beyond just single file todo-app.
I would like your guide on this, to create a module application having routes, models, controllers,.env files to I can learn the big picture.
If it's not too much of a request.
Greetings from Tanzania 🇹🇿
can we integrate Django and Fast API?
Dude, Ijust have to ask this; Is that project you promised to prepare is not done? I've been binge watching your videos lately and I'm so hyped for the project hopefully authentication won't be forgotten.
Cheers fro Tanzania
Thanks for kindness! I have one tiny question. Why fucntion named 'resolve_hello' is excuted? when class Query is requested. There is only def. not an excution code.
Great content dude. Can you make a tutorial including authentication and authorization withing FastAPI please ?
can you make video for javascript with graphql in future ? or with angular framwork please ? good video
Keep them coming, brother, sweet short tutorial
Ps : I know nothing about graphql
It is an interesting video, however can you create a more in-depth tutorial with a front end and back end frameworks ? Thanks for your effort
amazing , thank you very much sir, and God bless you