{"id":34108517,"url":"https://github.com/strollby/graphene-directives","last_synced_at":"2026-04-02T02:18:36.756Z","repository":{"id":210729877,"uuid":"727061390","full_name":"strollby/graphene-directives","owner":"strollby","description":"Schema Directives implementation for graphene","archived":false,"fork":false,"pushed_at":"2025-10-09T06:43:24.000Z","size":182,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-17T01:36:18.265Z","etag":null,"topics":["graphql","graphql-python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/strollby.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-12-04T05:17:51.000Z","updated_at":"2025-10-09T06:40:31.000Z","dependencies_parsed_at":"2024-01-15T19:58:49.118Z","dependency_job_id":null,"html_url":"https://github.com/strollby/graphene-directives","commit_stats":null,"previous_names":["strollby/graphene-directives"],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/strollby/graphene-directives","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strollby%2Fgraphene-directives","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strollby%2Fgraphene-directives/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strollby%2Fgraphene-directives/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strollby%2Fgraphene-directives/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/strollby","download_url":"https://codeload.github.com/strollby/graphene-directives/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strollby%2Fgraphene-directives/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31294527,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-02T01:43:37.129Z","status":"online","status_checked_at":"2026-04-02T02:00:08.535Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","graphql-python"],"created_at":"2025-12-14T18:19:00.411Z","updated_at":"2026-04-02T02:18:36.748Z","avatar_url":"https://github.com/strollby.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Graphene Directives\nSchema Directives implementation for graphene\n\n[![PyPI version][pypi-image]][pypi-url]\n[![PyPI pyversions][pypi-version-image]][pypi-version-url]\n[![Downloads][pypi-downloads-image]][pypi-downloads-url]\n[![Test Status][tests-image]][tests-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n\n[pypi-image]: https://badge.fury.io/py/graphene-directives.svg\n[pypi-url]: https://pypi.org/project/graphene-directives/\n[pypi-version-image]: https://img.shields.io/pypi/pyversions/graphene-directives.svg\n[pypi-version-url]: https://pypi.python.org/pypi/graphene-directives/\n[pypi-downloads-image]: https://pepy.tech/badge/graphene-directives\n[pypi-downloads-url]: https://pepy.tech/project/graphene-directives\n[tests-image]: https://github.com/strollby/graphene-directives/actions/workflows/test.yml/badge.svg?branch=main\n[tests-url]: https://github.com/strollby/graphene-directives/actions/workflows/test.yml\n[coveralls-image]: https://coveralls.io/repos/github/strollby/graphene-directives/badge.svg?branch=main\n[coveralls-url]: https://coveralls.io/github/strollby/graphene-directives?branch=main\n\n------------------------\n\n## Directive Locations Supported\n\n- [x] DirectiveLocation.SCHEMA\n- [x] DirectiveLocation.OBJECT\n- [x] DirectiveLocation.ENUM\n- [x] DirectiveLocation.INTERFACE\n- [x] DirectiveLocation.UNION\n- [x] DirectiveLocation.SCALAR\n- [x] DirectiveLocation.FIELD_DEFINITION\n- [x] DirectiveLocation.INPUT_FIELD_DEFINITION\n- [x] DirectiveLocation.INPUT_OBJECT\n- [x] DirectiveLocation.ENUM_VALUE\n- [x] DirectiveLocation.ARGUMENT_DEFINITION,\n\n------------------------\n\n## Example\n\n### Using `@directive`\n\n```python\nimport graphene\nfrom graphql import (\n    GraphQLArgument,\n    GraphQLInt,\n    GraphQLNonNull,\n    GraphQLString,\n)\n\nfrom graphene_directives import CustomDirective, DirectiveLocation, build_schema, directive\n\nCacheDirective = CustomDirective(\n    name=\"cache\",\n    locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],\n    args={\n        \"max_age\": GraphQLArgument(\n            GraphQLNonNull(GraphQLInt),\n            description=\"Specifies the maximum age for cache in seconds.\",\n        ),\n        \"swr\": GraphQLArgument(\n            GraphQLInt, description=\"Stale-while-revalidate value in seconds. Optional.\"\n        ),\n        \"scope\": GraphQLArgument(\n            GraphQLString, description=\"Scope of the cache. Optional.\"\n        ),\n    },\n    description=\"Caching directive to control cache behavior of fields or fragments.\",\n)\n\n\n@directive(CacheDirective, max_age=200)\nclass SomeType(graphene.ObjectType):\n    field_1 = directive(CacheDirective, field=graphene.String(), max_age=300)\n    field_2 = directive(CacheDirective, field=graphene.String(), max_age=300, swr=2)\n    field_3 = graphene.String()\n\n\nclass Query(graphene.ObjectType):\n    some_query = graphene.Field(SomeType)\n\n\nschema = build_schema(\n    query=Query, directives=[CacheDirective]\n) \n```\n\n\n### Using `directive_decorator`\n\n```python\nimport graphene\nfrom graphql import (\n    GraphQLArgument,\n    GraphQLInt,\n    GraphQLNonNull,\n    GraphQLString,\n)\n\nfrom graphene_directives import CustomDirective, DirectiveLocation, build_schema, directive_decorator\n\nCacheDirective = CustomDirective(\n    name=\"cache\",\n    locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],\n    args={\n        \"max_age\": GraphQLArgument(\n            GraphQLNonNull(GraphQLInt),\n            description=\"Specifies the maximum age for cache in seconds.\",\n        ),\n        \"swr\": GraphQLArgument(\n            GraphQLInt, description=\"Stale-while-revalidate value in seconds. Optional.\"\n        ),\n        \"scope\": GraphQLArgument(\n            GraphQLString, description=\"Scope of the cache. Optional.\"\n        ),\n    },\n    description=\"Caching directive to control cache behavior of fields or fragments.\",\n)\n\n# This returns a partial of directive function\ncache = directive_decorator(target_directive=CacheDirective)\n\n\n@cache(max_age=200)\nclass SomeType(graphene.ObjectType):\n    field_1 = cache(field=graphene.String(), max_age=300)\n    field_2 = cache(field=graphene.String(), max_age=300, swr=2)\n    field_3 = graphene.String()\n\n\nclass Query(graphene.ObjectType):\n    some_query = graphene.Field(SomeType)\n\n\nschema = build_schema(\n    query=Query, directives=[CacheDirective]\n)\n```\n\n### Custom Input Validation\n\n```python\nfrom typing import Any\n\nimport graphene\nfrom graphql import (\n    GraphQLArgument,\n    GraphQLInt,\n    GraphQLNonNull,\n    GraphQLString,\n)\n\nfrom graphene_directives import CustomDirective, DirectiveLocation, Schema, build_schema, directive_decorator\n\n\ndef input_transform(inputs: dict, _schema: Schema) -\u003e dict:\n    \"\"\"\n    def input_transform (inputs: Any, schema: Schema) -\u003e dict,\n    \"\"\"\n    if inputs.get(\"max_age\") \u003e 200:\n        inputs[\"swr\"] = 30\n    return inputs\n\n\ndef validate_non_field_input(_type: Any, inputs: dict, _schema: Schema) -\u003e bool:\n    \"\"\"\n    def validator (type_: graphene type, inputs: Any, schema: Schema) -\u003e bool,\n    if validator returns False, library raises DirectiveCustomValidationError\n    \"\"\"\n    if inputs.get(\"max_age\") \u003e 2500:\n        return False\n    return True\n\n\ndef validate_field_input(\n        _parent_type: Any, _field_type: Any, inputs: dict, _schema: Schema\n) -\u003e bool:\n    \"\"\"\n    def validator (parent_type_: graphene_type, field_type_: graphene type, inputs: Any, schema: Schema) -\u003e bool,\n    if validator returns False, library raises DirectiveCustomValidationError\n    \"\"\"\n    if inputs.get(\"max_age\") \u003e 2500:\n        return False\n    return True\n\n\nCacheDirective = CustomDirective(\n    name=\"cache\",\n    locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT],\n    args={\n        \"max_age\": GraphQLArgument(\n            GraphQLNonNull(GraphQLInt),\n            description=\"Specifies the maximum age for cache in seconds.\",\n        ),\n        \"swr\": GraphQLArgument(\n            GraphQLInt, description=\"Stale-while-revalidate value in seconds. Optional.\"\n        ),\n        \"scope\": GraphQLArgument(\n            GraphQLString, description=\"Scope of the cache. Optional.\"\n        ),\n    },\n    description=\"Caching directive to control cache behavior of fields or fragments.\",\n    non_field_validator=validate_non_field_input,\n    field_validator=validate_field_input,\n    input_transform=input_transform,\n)\n\n# This returns a partial of directive function\ncache = directive_decorator(target_directive=CacheDirective)\n\n\n@cache(max_age=200)\nclass SomeType(graphene.ObjectType):\n    field_1 = cache(field=graphene.String(), max_age=300)\n    field_2 = cache(field=graphene.String(), max_age=300, swr=2)\n    field_3 = graphene.String()\n\n\nclass Query(graphene.ObjectType):\n    some_query = graphene.Field(SomeType)\n\n\nschema = build_schema(\n    query=Query, directives=[CacheDirective]\n)\n```\n\n\n### Complex Use Cases\n\nRefer [`Code`](./example/complex_uses.py) and [`Graphql Output`](./example/complex_uses.graphql)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrollby%2Fgraphene-directives","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrollby%2Fgraphene-directives","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrollby%2Fgraphene-directives/lists"}