Skip to content

Releases: strollby/graphene-directives

v0.4.6 - schema level validations for the correct locations of directives

15 Jan 18:06
Compare
Choose a tag to compare

feat: schema also validates the correct locations of directives now, in addition to run time checking by the directive decorator

Full Changelog: v0.4.5...v0.4.6

v0.4.5 - Graphql default directives are added automatically now

15 Jan 05:53
Compare
Choose a tag to compare
  • build_schema(...) accepts a new argument include_graphql_spec_directives (defaults to true) . It will add graphql spec directives @include, @skip, @deprecated, @specifiedBy

Full Changelog: v0.4.4...v0.4.5

v0.4.4 - Validation for duplicate values at same field level

14 Jan 08:00
Compare
Choose a tag to compare

v0.4.3 - Camel Casing Bug Fix

13 Jan 22:00
Compare
Choose a tag to compare
  • auto camel cased fields skipped directive decorators

Full Changelog: v0.4.2...v0.4.3

v0.4.2 - Add support for input transformation

13 Jan 21:03
c9f8491
Compare
Choose a tag to compare

What's Changed

  • feat: add support for input transformation by @mak626 in #9

Full Changelog: v0.4.1...v0.4.2

v0.4.1 - Pass schema to validators

13 Jan 19:22
Compare
Choose a tag to compare

What's Changed

  • feat: pass schema to validators by @mak626 in #8

Custom Input Validation

from typing import Any

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

from graphene_directives import CustomDirective, DirectiveLocation, Schema, build_schema, directive_decorator


def validate_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
    """
    if inputs.get("max_age") > 2500:
        return False
    return True


def validate_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
    """
    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.",
    non_field_validator=validate_non_field_input,
    field_validator=validate_field_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]
)

Full Changelog: v0.4.0...v0.4.1

v0.4.0 - Added User Argument Input Validation

13 Jan 15:39
88c6240
Compare
Choose a tag to compare

What's Changed

  • Better field level & non field validation by @mak626 in #7
  • Fix: Object fields went missing
  • Added support for providing custom non_field_validator, field_validator for user validation on directive

Full Changelog: v0.3.3...v0.4.0

v0.3.3 - Added User Argument Input Validation

12 Jan 15:51
1e98d6d
Compare
Choose a tag to compare

What's Changed

  • feat: add support for user input validations by @mak626 in #6

Full Changelog: v0.3.2...v0.3.3

v0.3.2 - Schema Directive Support

12 Jan 10:09
848d7f1
Compare
Choose a tag to compare

What's Changed

  • feat: add support for schema directive by @mak626 in #5

Full Changelog: v0.3.1...v0.3.2

[v0.3.1] Fix _auto_camelcase case extra attribute

12 Jan 07:48
de9dac2
Compare
Choose a tag to compare

What's Changed

  • fix: removed schema's _auto_camelcase attribute by @mak626 in #4

Full Changelog: v0.2.2...v0.3.1