Schema Directives implementation for graphene
Project description
Graphene Directives
Schema Directives implementation for graphene
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
Release history Release notifications | RSS feed
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.0.tar.gz
(11.1 kB
view hashes)
Built Distribution
Close
Hashes for graphene_directives-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 799514e8da1f5e9c4fc6b712611f460d4f144c2201e82e4dab27eaf47e711bad |
|
MD5 | 2585a70163a4d71083638418eb7b976a |
|
BLAKE2b-256 | 9e920a726b7745ca15fd98ba9256411d01f68b7c3d485af651683c84d23e2264 |
Close
Hashes for graphene_directives-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4da2af34d943f24d221b27c4c3d1c63949dce95da2069e0b1e966f5e8c9817b3 |
|
MD5 | 2852a21cde09cc751828440ee7be0a7a |
|
BLAKE2b-256 | e993d84c32d5bf61568d61f7d7e8f13a85fe29b347d43f173e1b2af7abec7a1a |