Flask-GraphQL

2.0.1 · active · verified Sun Apr 12

Flask-GraphQL adds GraphQL support to your Flask application, providing an easy way to integrate a GraphQL API endpoint and an optional GraphiQL IDE. The library is currently at version 2.0.1 and has a stable, though not rapid, release cadence, focusing on compatibility with Flask and core GraphQL Python libraries.

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Flask application with a GraphQL endpoint at `/graphql`. It defines a simple 'hello world' GraphQL query using Graphene and enables the interactive GraphiQL interface for easy testing in the browser. It also demonstrates how to enable batch query support.

import os
from flask import Flask
from flask_graphql import GraphQLView
import graphene

class Query(graphene.ObjectType):
    hello = graphene.String(description='A typical hello world')

    def resolve_hello(self, info):
        # In a real app, 'info' can contain request context, e.g., for auth
        # For this example, we'll just return a string.
        return 'Hello, World!'

schema = graphene.Schema(query=Query)

app = Flask(__name__)
app.add_url_rule(
    '/graphql',
    view_func=GraphQLView.as_view(
        'graphql',
        schema=schema,
        graphiql=True # Enable the GraphiQL IDE for testing
    )
)

# Optional: for adding batch query support (used in Apollo-Client)
app.add_url_rule(
    '/graphql/batch',
    view_func=GraphQLView.as_view(
        'graphql_batch',
        schema=schema,
        batch=True
    )
)

if __name__ == '__main__':
    # Use a secure way to get host/port in production
    host = os.environ.get('FLASK_HOST', '127.0.0.1')
    port = int(os.environ.get('FLASK_PORT', 5000))
    app.run(host=host, port=port)

view raw JSON →