FastAPI is a modern, fast web framework for building APIs with Python. It comes with built-in support for GraphQL, a powerful query language for APIs. In this blog post, we'll explore how to use FastAPI with GraphQL to build a high-performance API.

Step 1: Install FastAPI

        To get started with FastAPI, you'll need to install it first. You can do this using pip:

pip install fastapi

Step 2: Install Graphene

        Graphene is a Python library for building GraphQL APIs. To use Graphene with FastAPI, you'll need to install it:

pip install graphene

Step 3: Create a FastAPI app

            Now that you have FastAPI and Graphene installed, it's time to create a FastAPI app. Here's an example:

from fastapi import FastAPI
from graphene import ObjectType, String, Schema

app = FastAPI()

class Query(ObjectType):
    hello = String(name=String(default_value="stranger"))

    def resolve_hello(root, info, name):
        return f"Hello, {name}!"

schema = Schema(query=Query)

        In this example, we're creating a FastAPI app with a single GraphQL query called hello. This query takes an argument called name and returns a greeting string.

Step 4: Add a GraphQL route

        To add a GraphQL route to your FastAPI app, you can use the app.add_route method:

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from starlette.graphql import GraphQLApp
from graphene import ObjectType, String, Schema

app = FastAPI()

class Query(ObjectType):
    hello = String(name=String(default_value="stranger"))

    def resolve_hello(root, info, name):
        return f"Hello, {name}!"

schema = Schema(query=Query)

app.add_route("/", GraphQLApp(schema=schema))

        In this example, we're using the GraphQLApp class from Starlette to create a GraphQL route for our FastAPI app. The GraphQLApp class takes a Graphene schema as an argument and returns a Starlette endpoint that can be added to the FastAPI app using the app.add_route method.

Step 5: Test the GraphQL API

        Now that you've added a GraphQL route to your FastAPI app, you can test it using a GraphQL client such as Altair or GraphiQL.

        For example, to test the hello query that we defined earlier, you can send the following GraphQL query:

query {
  hello(name: "John")
}

        This should return the following JSON response:

{
  "data": {
    "hello": "Hello, John!"
  }
}

Step 6: Add mutations to the schema

        GraphQL is not just for querying data, but it can also be used for modifying data. In order to add mutations to our FastAPI app, we need to define a new class that inherits from graphene.Mutation.

class CreateUser(graphene.Mutation):
    class Arguments:
        username = graphene.String(required=True)
        email = graphene.String(required=True)

    success = graphene.Boolean()

    def mutate(self, info, username, email):
        # create user logic goes here
        success = True
        return CreateUser(success=success)

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

        In this example, we're defining a mutation called create_user, which takes two arguments (username and email) and returns a success boolean. In the mutate method, we can define the logic for creating a user with the provided username and email.

Step 7: Add the mutation route

        Now that we've defined the mutation in the schema, we need to add a new route to the FastAPI app to handle the mutation. We can do this using the GraphQLApp class again:

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

app.add_route("/", GraphQLApp(schema=schema, graphiql=True))

        Notice that we've set the graphiql parameter to True. This enables the GraphiQL interface, which provides a user-friendly interface for testing our API.

Step 8: Test the mutation

        Now that we've added a mutation to our FastAPI app, we can test it using the GraphiQL interface. To do this, navigate to http://localhost:8000/ in your web browser and click on the "Docs" button in the top-right corner. This will open the GraphiQL interface, which allows you to interactively test the GraphQL API.

        To test the create_user mutation, you can enter the following mutation query in the left-hand pane of the GraphiQL interface:

mutation {
  createUser(username: "johndoe", email: "johndoe@example.com") {
    success
  }
}

        This should return the following JSON response:

{
  "data": {
    "createUser": {
      "success": true
    }
  }
}

        Congratulations! You've just added mutations to your GraphQL API using FastAPI and Graphene. With FastAPI's high performance and Graphene's flexibility, you can build powerful and scalable APIs that can handle complex queries and mutations.