-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from strollby/input-transform
feat: add support for input transformation
- Loading branch information
Showing
10 changed files
with
209 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "graphene-directives" | ||
version = "0.4.1" | ||
version = "0.4.2" | ||
packages = [{include = "graphene_directives"}] | ||
description = "Schema Directives implementation for graphene" | ||
authors = ["Strollby <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Caching directive to control cache behavior of fields or fragments.""" | ||
directive @cache( | ||
"""Specifies the maximum age for cache in seconds.""" | ||
maxAge: Int! | ||
|
||
"""Stale-while-revalidate value in seconds. Optional.""" | ||
swr: Int | ||
|
||
"""Scope of the cache. Optional.""" | ||
scope: String | ||
) on FIELD_DEFINITION | OBJECT | UNION | ||
|
||
union SearchResult @cache(maxAge: 500, swr: 30) = Human | Droid | Starship | ||
|
||
type Human @cache(maxAge: 60) { | ||
name: String | ||
bornIn: String | ||
} | ||
|
||
type Droid @cache(maxAge: 200) { | ||
name: String @cache(maxAge: 300, swr: 30) | ||
primaryFunction: String | ||
} | ||
|
||
type Starship @cache(maxAge: 200) { | ||
name: String | ||
length: Int @deprecated(reason: "Koo") @cache(maxAge: 60) | ||
} | ||
|
||
type Query { | ||
position: Position @deprecated(reason: "Koo") | ||
} | ||
|
||
type Position @cache(maxAge: 500, swr: 30) { | ||
x: Int! | ||
y: Int! @cache(maxAge: 60) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pathlib import Path | ||
|
||
import graphene | ||
from graphql import GraphQLArgument, GraphQLInt, GraphQLNonNull, GraphQLString | ||
|
||
from graphene_directives import ( | ||
CustomDirective, | ||
DirectiveLocation, | ||
Schema, | ||
build_schema, | ||
directive_decorator, | ||
) | ||
|
||
curr_dir = Path(__file__).parent | ||
|
||
|
||
def input_transform(inputs: dict, _schema: Schema) -> dict: | ||
""" | ||
def input_transform (inputs: Any, schema: Schema) -> dict, | ||
""" | ||
if inputs.get("max_age") > 200: | ||
inputs["swr"] = 30 | ||
return inputs | ||
|
||
|
||
CacheDirective = CustomDirective( | ||
name="cache", | ||
locations=[ | ||
DirectiveLocation.FIELD_DEFINITION, | ||
DirectiveLocation.OBJECT, | ||
DirectiveLocation.UNION, | ||
], | ||
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.", | ||
input_transform=input_transform, | ||
) | ||
|
||
|
||
cache = directive_decorator(target_directive=CacheDirective) | ||
|
||
|
||
@cache(max_age=500) | ||
class Position(graphene.ObjectType): | ||
x = graphene.Int(required=True) | ||
y = cache(field=graphene.Int(required=True), max_age=60) | ||
|
||
|
||
@cache(max_age=60) | ||
class Human(graphene.ObjectType): | ||
name = graphene.String() | ||
born_in = graphene.String() | ||
|
||
|
||
@cache(max_age=200) | ||
class Droid(graphene.ObjectType): | ||
name = cache(field=graphene.String(), max_age=300) | ||
primary_function = graphene.String() | ||
|
||
|
||
@cache(max_age=200) | ||
class Starship(graphene.ObjectType): | ||
name = graphene.String() | ||
length = cache(field=graphene.Int(deprecation_reason="Koo"), max_age=60) | ||
|
||
|
||
@cache(max_age=500) | ||
class SearchResult(graphene.Union): | ||
class Meta: | ||
types = (Human, Droid, Starship) | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
position = graphene.Field(Position, deprecation_reason="Koo") | ||
|
||
|
||
schema = build_schema(query=Query, types=(SearchResult,), directives=[CacheDirective]) | ||
|
||
|
||
def test_generate_schema() -> None: | ||
with open(f"{curr_dir}/schema_files/test_directive_input_transform.graphql") as f: | ||
assert str(schema) == f.read() |