Skip to main content

Schema Directives implementation for graphene

Project description

Graphene Directives

Schema Directives implementation for graphene

PyPI version PyPI pyversions Downloads Test Status Coverage Status


Directive Locations Supported

  • DirectiveLocation.SCHEMA
  • DirectiveLocation.OBJECT
  • DirectiveLocation.ENUM
  • DirectiveLocation.INTERFACE
  • DirectiveLocation.UNION
  • DirectiveLocation.SCALAR
  • DirectiveLocation.FIELD_DEFINITION
  • DirectiveLocation.INPUT_FIELD_DEFINITION
  • DirectiveLocation.INPUT_OBJECT
  • DirectiveLocation.ENUM_VALUE
  • DirectiveLocation.ARGUMENT_DEFINITION,

Example

Using @directive

import graphene
from graphql import (
    GraphQLArgument,
    GraphQLInt,
    GraphQLNonNull,
    GraphQLString,
)

from graphene_directives import CustomDirective, DirectiveLocation, build_schema, directive

CacheDirective = CustomDirective(
    name="cache",
    locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],
    args={
        "max_age": GraphQLArgument(
            GraphQLNonNull(GraphQLInt),
            description="Specifies the maximum age for cache in seconds.",
        ),
        "swr": GraphQLArgument(
            GraphQLInt, description="Stale-while-revalidate value in seconds. Optional."
        ),
        "scope": GraphQLArgument(
            GraphQLString, description="Scope of the cache. Optional."
        ),
    },
    description="Caching directive to control cache behavior of fields or fragments.",
)


@directive(CacheDirective, max_age=200)
class SomeType(graphene.ObjectType):
    field_1 = directive(CacheDirective, field=graphene.String(), max_age=300)
    field_2 = directive(CacheDirective, field=graphene.String(), max_age=300, swr=2)
    field_3 = graphene.String()


class Query(graphene.ObjectType):
    some_query = graphene.Field(SomeType)


schema = build_schema(
    query=Query, directives=[CacheDirective]
) 

Using directive_decorator

import graphene
from graphql import (
    GraphQLArgument,
    GraphQLInt,
    GraphQLNonNull,
    GraphQLString,
)

from graphene_directives import CustomDirective, DirectiveLocation, build_schema, directive_decorator

CacheDirective = CustomDirective(
    name="cache",
    locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],
    args={
        "max_age": GraphQLArgument(
            GraphQLNonNull(GraphQLInt),
            description="Specifies the maximum age for cache in seconds.",
        ),
        "swr": GraphQLArgument(
            GraphQLInt, description="Stale-while-revalidate value in seconds. Optional."
        ),
        "scope": GraphQLArgument(
            GraphQLString, description="Scope of the cache. Optional."
        ),
    },
    description="Caching directive to control cache behavior of fields or fragments.",
)

# This returns a partial of directive function
cache = directive_decorator(target_directive=CacheDirective)


@cache(max_age=200)
class SomeType(graphene.ObjectType):
    field_1 = cache(field=graphene.String(), max_age=300)
    field_2 = cache(field=graphene.String(), max_age=300, swr=2)
    field_3 = graphene.String()


class Query(graphene.ObjectType):
    some_query = graphene.Field(SomeType)


schema = build_schema(
    query=Query, directives=[CacheDirective]
)

Custom Input Validation

import graphene
from graphql import (
    GraphQLArgument,
    GraphQLDirective,
    GraphQLInt,
    GraphQLNonNull,
    GraphQLString,
)

from graphene_directives import CustomDirective, DirectiveLocation, build_schema, directive_decorator


def validate_input(_directive: GraphQLDirective, inputs: dict) -> bool:
    if inputs.get("max_age") > 2500:
        return False
    return True


CacheDirective = CustomDirective(
    name="cache",
    locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],
    args={
        "max_age": GraphQLArgument(
            GraphQLNonNull(GraphQLInt),
            description="Specifies the maximum age for cache in seconds.",
        ),
        "swr": GraphQLArgument(
            GraphQLInt, description="Stale-while-revalidate value in seconds. Optional."
        ),
        "scope": GraphQLArgument(
            GraphQLString, description="Scope of the cache. Optional."
        ),
    },
    description="Caching directive to control cache behavior of fields or fragments.",
    validator=validate_input,
)

# This returns a partial of directive function
cache = directive_decorator(target_directive=CacheDirective)


@cache(max_age=200)
class SomeType(graphene.ObjectType):
    field_1 = cache(field=graphene.String(), max_age=300)
    field_2 = cache(field=graphene.String(), max_age=300, swr=2)
    field_3 = graphene.String()


class Query(graphene.ObjectType):
    some_query = graphene.Field(SomeType)


schema = build_schema(
    query=Query, directives=[CacheDirective]
)

Complex Use Cases

Refer Code and Graphql Output

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphene_directives-0.3.3.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

graphene_directives-0.3.3-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file graphene_directives-0.3.3.tar.gz.

File metadata

  • Download URL: graphene_directives-0.3.3.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure

File hashes

Hashes for graphene_directives-0.3.3.tar.gz
Algorithm Hash digest
SHA256 0e7d3d343503a4c929d1b12a6c7896ddc1457f4fecb257d2e055fc140082a84d
MD5 d74739f6e52b9ce0fd0ec4f58f57b898
BLAKE2b-256 feabdadf02ac622f82e0ff009e08353c27539942bd81aea1e1bb48f1852fb5d9

See more details on using hashes here.

File details

Details for the file graphene_directives-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_directives-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cd1f72950c3c8f14f3a2a1843a8cb9385cdde05c9294c557e22ef5cd5e3d3864
MD5 3c73a87d0f13994adb22be80dcd4b874
BLAKE2b-256 3d0514654f4ad3f5b1f3d9f9f63daffe3463e19ca793f9ef88e5e47a279d7cca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page