Schema Directives implementation for graphene
Project description
Graphene Directives
Schema Directives implementation for graphene
Example
Using @directive
import graphene
from graphql import (
DirectiveLocation,
GraphQLArgument,
GraphQLDirective,
GraphQLInt,
GraphQLNonNull,
GraphQLString,
)
from graphene_directives import build_schema, directive
CacheDirective = GraphQLDirective(
name="cache",
locations=[DirectiveLocation.FIELD, DirectiveLocation.OBJECT],
args={
"maxAge": 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 @build_decorator_from_directive
import graphene
from graphql import (
DirectiveLocation,
GraphQLArgument,
GraphQLDirective,
GraphQLInt,
GraphQLNonNull,
GraphQLString,
)
from graphene_directives import build_decorator_from_directive, build_schema
CacheDirective = GraphQLDirective(
name="cache",
locations=[DirectiveLocation.FIELD, DirectiveLocation.OBJECT],
args={
"maxAge": 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 = build_decorator_from_directive(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]
)
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
Built Distribution
Close
Hashes for graphene_directives-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a18a29334c3f31fd39f09579c0b3fd46f15af802ab47e2e200d1d210d13ea29 |
|
MD5 | fa771da15cd532f04f9d856a9004a0a2 |
|
BLAKE2b-256 | 9c30d618ac04ea2bad06ddd2cd0cdfbf9787ed2ad927f7045b2a3ee5ccc35cf8 |
Close
Hashes for graphene_directives-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81371000535b8c1c6111060d38664a309bf1d36792672af11532a2ed45c6ff34 |
|
MD5 | 04c25b9bd9d06571f17c9d24fd39c745 |
|
BLAKE2b-256 | da1413a777d4ca47936764b5be14f1edf2dad0ac95927ead4a3639f7388d4c57 |