API#

Anything documented here is part of the public API that Flask-Magql provides, unless otherwise indicated. Anything not documented here is considered internal or private and may change at any time.

Extension#

class flask_magql.MagqlExtension(schema, *, decorators=None)#

Serve a Magql Schema to provide a GraphQL API.

The following views are registered. The blueprint’s name is magql by default, which is the prefix for each endpoint name.

URL

Endpoint

Description

/graphql

.graphql

The GraphQL API view. Supports the multipart spec and batched operations.

/schema.graphql

.schema

The GraphQL schema document served as a text file. Useful if a tool does not use the introspection query to discover the API.

/graphiql

.graphiql

The GraphiQL UI for exploring the schema and building queries.

Parameters:
  • schema (magql.Schema) – The schema to serve.

  • decorators (list[t.Callable[[t.Callable[..., ResponseReturnValue]], t.Callable[..., ResponseReturnValue]]] | None) – View decorators applied to each view function. This can be used to apply authentication, CORS, etc.

schema#

The Magql schema to serve.

blueprint: Blueprint#

The Flask blueprint that will hold these GraphQL routes and be registered on the app. The blueprint can be modified, for example to add a URL prefix: me.blueprint.url_prefix = "/api".

decorators#

View decorators to apply to each view function. This can be used to apply authentication, CORS, etc.

init_app(app)#

Register the GraphQL API on the given Flask app.

Applies decorators to each view function and registers it on blueprint, then registers the blueprint on the app.

Parameters:

app (Flask) – The app to register on.

Return type:

None

context_provider(f)#

Decorate a function that should be called by execute() to provide a value for info.context in resolvers.

Parameters:

f (Callable[[], Any]) –

Return type:

Callable[[], Any]

execute(source, variables=None, operation=None)#

Execute a GraphQL operation (query or mutation). The SQLAlchemy session is passed as the context.

Parameters:
  • source (str) – The operation (query or mutation) written in GraphQL language to execute on the schema.

  • variables (dict[str, Any] | None) – Maps placeholder names in the source to input values passed along with the request.

  • operation (str | None) – The name of the operation if the source defines multiple.

Return type:

ExecutionResult