Skip to content

GraphQL Operations

The code must be placed in a file named graphql.py or within a folder named graphql located in the Application directory.

graphql.py
import zmag

# Create your API (GraphQL) here.
@zmag.gql
class Graphql:
    class Meta: ...
    class Query: ...
    class Mutation: ...

Operations Tools — Reference

  • Meta
  • Query
  • Mutation

Meta (Optional)

The Meta class can be used to customize the naming and behavior of these GraphQL operations.

  • app (str | bool | None): Specifies the application name or identifier.
  • model (str | type | None): Associates the GraphQL operations with a model. This can be a string representing the model name or an actual type class.

Examples


Without Meta (default)

When the Meta class is not used:

@zmag.gql
class Graphql:
    async def detail(self) -> str:
        return "Detail"

GraphQL Field Name:

demoDetail

Explanation: The GraphQL field name is automatically generated based on the method name detail and package/application name demo.


With model

When the Meta class specifies a model:

from . import types

@zmag.gql
class Graphql:
    class Meta:
        model = types.Book

GraphQL Field Name:

demoBookDetail

Explanation: The GraphQL field name is prefixed with the model name (Book), resulting in demoBookDetail.


With app Set to None

When the Meta class specifies app as None:

@zmag.gql
class Graphql:
    class Meta:
        app = None
        ...

GraphQL Field Name:

detail

Explanation: The app value is ignored, so the GraphQL field name is based directly on the method name detail.

With custom app

@zmag.gql
class Graphql:
    class Meta:
        app = "custom"
        ...

GraphQL Field Name:

customDetail

Explanation: The GraphQL field name is automatically generated based on the method name detail and provided app name custom.

Full Example

import zmag

@zmag.gql
class Graphql:

    class Meta:
        app = True
        model = "Book"

    class Query:
        async def detail(self) -> str:
            return "Detail (Query)"

    class Mutation:
        async def create(self) -> str:
            return "Create (Mutation)"