Introduction

GraphQL has emerged as a powerful alternative to REST APIs, offering more flexibility, efficiency, and precise data fetching. In this guide, we’ll explore how to integrate GraphQL with Flask using Graphene, a popular GraphQL library for Python.

✅ Setting up Flask + GraphQL
✅ Defining GraphQL schemas & resolvers
✅ Handling queries & mutations
✅ Implementing authentication & optimizations


Why Use GraphQL Instead of REST?

Feature REST GraphQL
Data Fetching Fixed endpoints return full data Clients request only what they need
Over-fetching Yes (returns unnecessary fields) No (fetches exact data)
Multiple Requests Needed for nested data Single query for nested data
API Evolution New endpoints required for updates Schema remains flexible

GraphQL eliminates over-fetching and under-fetching, making APIs more efficient.


Step 1: Setting Up Flask and Graphene

Install Dependencies

pip install flask graphene flask-graphql

Create a Flask App

from flask import Flask
from flask_graphql import GraphQLView
import graphene

app = Flask(__name__)

@app.route("/")
def home():
return "GraphQL API with Flask"

if __name__ == "__main__":
app.run(debug=True)

Step 2: Defining a GraphQL Schema

Creating a Simple Schema

class User(graphene.ObjectType):
id = graphene.Int()
name = graphene.String()
email = graphene.String()

class Query(graphene.ObjectType):
users = graphene.List(User)

    def resolve_users(self, info):
        return [
            User(id=1, name="Alice", email="alice@example.com"),
            User(id=2, name="Bob", email="bob@example.com"),
        ]

schema = graphene.Schema(query=Query)

Step 3: Adding a GraphQL Endpoint

app.add_url_rule(
"/graphql",
view_func=GraphQLView.as_view(
"graphql",
schema=schema,
graphiql=True,  # Enables GraphiQL UI
),
)

🚀 Now, open http://127.0.0.1:5000/graphql to run queries in the GraphiQL interface.


Step 4: Handling Mutations (Data Updates)

Adding a Mutation

class CreateUser(graphene.Mutation):
class Arguments:
name = graphene.String()
email = graphene.String()

    user = graphene.Field(User)

    def mutate(self, info, name, email):
        new_user = User(id=3, name=name, email=email)
        return CreateUser(user=new_user)

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

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

Now, run the following GraphQL mutation in GraphiQL:

mutation {
createUser(name: "Charlie", email: "charlie@example.com") {
user {
id
name
email
}
}
}

Step 5: Implementing Authentication

For authentication, we use JWT tokens. Install pyjwt:

pip install pyjwt

Protecting Queries

import jwt

SECRET_KEY = "mysecret"

def resolve_users(self, info):
auth = info.context.headers.get("Authorization")
if not auth or not auth.startswith("Bearer "):
raise Exception("Unauthorized")

    token = auth.split(" ")[1]
    try:
        jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
    except jwt.ExpiredSignatureError:
        raise Exception("Token expired")
    
    return [
        User(id=1, name="Alice", email="alice@example.com"),
        User(id=2, name="Bob", email="bob@example.com"),
    ]

Now, send a JWT token in the request headers for authentication.


Step 6: Optimizing Query Performance

  • Use DataLoader to batch requests
  • Cache responses with Redis
  • Paginate large datasets

Example DataLoader implementation:

pip install promise
from promise import Promise
from graphene import relay
from graphene.relay import Node

class UserNode(graphene.ObjectType):
class Meta:
interfaces = (Node,)

class UserConnection(relay.Connection):
class Meta:
node = UserNode

class Query(graphene.ObjectType):
users = relay.ConnectionField(UserConnection)

schema = graphene.Schema(query=Query)

Conclusion

🚀 Flask + GraphQL with Graphene offers powerful API capabilities with flexible data retrieval and efficient queries.

📌 Next Steps:
✅ Connect GraphQL with PostgreSQL using SQLAlchemy
✅ Implement WebSockets for real-time GraphQL subscriptions
✅ Deploy with AWS Lambda or Docker

💡 Start building your GraphQL API today!