Package: graphene-directives
Graphene DirectivesSchema Directives implementation for grapheneDirective Locations SupportedDirectiveLocation.SCHEMADirectiveLocation.OBJECTDirectiveLocation.ENUMDirectiveLocation.INTERFACEDirectiveLocation.UNIONDirectiveLocation.SCALARDirectiveLocation.FIELD_DEFINITIONDirectiveLocation.INPUT_FIELD_DEFINITIONDirectiveLocation.INPUT_OBJECTDirectiveLocation.ENUM_VALUEDirectiveLocation.ARGUMENT_DEFINITION,ExampleUsing@directiveimportgraphenefromgraphqlimport(GraphQLArgument,GraphQLInt,GraphQLNonNull,GraphQLString,)fromgraphene_directivesimportCustomDirective,DirectiveLocation,build_schema,directiveCacheDirective=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)classSomeType(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()classQuery(graphene.ObjectType):some_query=graphene.Field(SomeType)schema=build_schema(query=Query,directives=[CacheDirective])Usingdirective_decoratorimportgraphenefromgraphqlimport(GraphQLArgument,GraphQLInt,GraphQLNonNull,GraphQLString,)fromgraphene_directivesimportCustomDirective,DirectiveLocation,build_schema,directive_decoratorCacheDirective=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 functioncache=directive_decorator(target_directive=CacheDirective)@cache(max_age=200)classSomeType(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()classQuery(graphene.ObjectType):some_query=graphene.Field(SomeType)schema=build_schema(query=Query,directives=[CacheDirective])Custom Input ValidationfromtypingimportAnyimportgraphenefromgraphqlimport(GraphQLArgument,GraphQLInt,GraphQLNonNull,GraphQLString,)fromgraphene_directivesimportCustomDirective,DirectiveLocation,Schema,build_schema,directive_decoratordefinput_transform(inputs:dict,_schema:Schema)->dict:"""def input_transform (inputs: Any, schema: Schema) -> dict,"""ifinputs.get("max_age")>200:inputs["swr"]=30returninputsdefvalidate_non_field_input(_type:Any,inputs:dict,_schema:Schema)->bool:"""def validator (type_: graphene type, inputs: Any, schema: Schema) -> bool,if validator returns False, library raises DirectiveCustomValidationError"""ifinputs.get("max_age")>2500:returnFalsereturnTruedefvalidate_field_input(_parent_type:Any,_field_type:Any,inputs:dict,_schema:Schema)->bool:"""def validator (parent_type_: graphene_type, field_type_: graphene type, inputs: Any, schema: Schema) -> bool,if validator returns False, library raises DirectiveCustomValidationError"""ifinputs.get("max_age")>2500:returnFalsereturnTrueCacheDirective=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.",non_field_validator=validate_non_field_input,field_validator=validate_field_input,input_transform=input_transform,)# This returns a partial of directive functioncache=directive_decorator(target_directive=CacheDirective)@cache(max_age=200)classSomeType(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()classQuery(graphene.ObjectType):some_query=graphene.Field(SomeType)schema=build_schema(query=Query,directives=[CacheDirective])Complex Use CasesReferCodeandGraphql Output