Skip to content

Commit

Permalink
add: support upto 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
mak626 committed Jan 15, 2024
1 parent 2b7f5ab commit e1d8343
Show file tree
Hide file tree
Showing 40 changed files with 538 additions and 52 deletions.
32 changes: 32 additions & 0 deletions federation_spec/federation-v2.4.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
directive @composeDirective(name: String!) repeatable on SCHEMA
directive @extends on OBJECT | INTERFACE
directive @external on OBJECT | FIELD_DEFINITION
directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @inaccessible on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| SCALAR
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @interfaceObject on OBJECT
directive @override(from: String!) on FIELD_DEFINITION
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @shareable repeatable on FIELD_DEFINITION | OBJECT
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
scalar FieldSet
45 changes: 45 additions & 0 deletions federation_spec/federation-v2.5.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
directive @composeDirective(name: String!) repeatable on SCHEMA
directive @extends on OBJECT | INTERFACE
directive @external on OBJECT | FIELD_DEFINITION
directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @inaccessible on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| SCALAR
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @interfaceObject on OBJECT
directive @override(from: String!) on FIELD_DEFINITION
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @shareable repeatable on FIELD_DEFINITION | OBJECT
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
directive @authenticated on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
directive @requiresScopes(scopes: [[Scope!]!]!) on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
scalar Scope
scalar FieldSet
52 changes: 52 additions & 0 deletions federation_spec/federation-v2.6.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
directive @composeDirective(name: String!) repeatable on SCHEMA
directive @extends on OBJECT | INTERFACE
directive @external on OBJECT | FIELD_DEFINITION
directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @inaccessible on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| SCALAR
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @interfaceObject on OBJECT
directive @override(from: String!) on FIELD_DEFINITION
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @shareable repeatable on FIELD_DEFINITION | OBJECT
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
directive @authenticated on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
directive @requiresScopes(scopes: [[Scope!]!]!) on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
directive @policy(policies: [[federation__Policy!]!]!) on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
scalar federation__Policy
scalar Scope
scalar FieldSet
6 changes: 6 additions & 0 deletions graphene_federation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

from .apollo_versions import FederationVersion, LATEST_VERSION
from .directives import (
authenticated,
extends,
external,
inaccessible,
interface_object,
key,
override,
policy,
provides,
requires,
requires_scope,
shareable,
tag,
)
Expand All @@ -24,14 +27,17 @@
"build_schema",
"FederationDirective",
"DirectiveLocation",
"authenticated",
"extends",
"external",
"inaccessible",
"interface_object",
"key",
"override",
"provides",
"policy",
"requires",
"requires_scope",
"shareable",
"tag",
"compose_directive",
Expand Down
13 changes: 11 additions & 2 deletions graphene_federation/apollo_versions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from .v2_1 import get_directives as get_directives_v2_1
from .v2_2 import get_directives as get_directives_v2_2
from .v2_3 import get_directives as get_directives_v2_3
from .v2_4 import get_directives as get_directives_v2_4
from .v2_5 import get_directives as get_directives_v2_5
from .v2_6 import get_directives as get_directives_v2_6
from .version import FederationVersion

LATEST_VERSION = FederationVersion.VERSION_2_3
LATEST_VERSION = FederationVersion.VERSION_2_6


def get_directives_based_on_version(
Expand All @@ -24,8 +27,14 @@ def get_directives_based_on_version(
return get_directives_v2_2()
case FederationVersion.VERSION_2_3:
return get_directives_v2_3()
case FederationVersion.VERSION_2_4:
return get_directives_v2_4()
case FederationVersion.VERSION_2_5:
return get_directives_v2_5()
case FederationVersion.VERSION_2_6:
return get_directives_v2_6()
case _:
return get_directives_v2_3()
return get_directives_v2_6()


def get_directive_from_name(
Expand Down
8 changes: 8 additions & 0 deletions graphene_federation/apollo_versions/v2_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from graphql import GraphQLDirective

from .v2_3 import get_directives as get_directives_v2_3


# No Change, Added Subscription Support
def get_directives() -> dict[str, GraphQLDirective]:
return get_directives_v2_3()
50 changes: 50 additions & 0 deletions graphene_federation/apollo_versions/v2_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from graphene_directives import CustomDirective, DirectiveLocation
from graphql import GraphQLArgument, GraphQLDirective, GraphQLList, GraphQLNonNull

from .v2_4 import get_directives as get_directives_v2_4
from ..scalars import Scope

authenticated_directive = CustomDirective(
name="authenticated",
locations=[
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.OBJECT,
DirectiveLocation.INTERFACE,
DirectiveLocation.SCALAR,
DirectiveLocation.ENUM,
],
description="Federation @authenticated directive",
add_definition_to_schema=False,
)

requires_scope_directive = CustomDirective(
name="requiresScopes",
locations=[
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.OBJECT,
DirectiveLocation.INTERFACE,
DirectiveLocation.SCALAR,
DirectiveLocation.ENUM,
],
args={
"scopes": GraphQLArgument(
GraphQLNonNull(
GraphQLList(GraphQLNonNull(GraphQLList(GraphQLNonNull(Scope))))
)
),
},
description="Federation @requiresScopes directive",
add_definition_to_schema=False,
)


# No Change, Added Subscription Support
def get_directives() -> dict[str, GraphQLDirective]:
directives = get_directives_v2_4()
directives.update(
{
directive.name: directive
for directive in [authenticated_directive, requires_scope_directive]
}
)
return directives
34 changes: 34 additions & 0 deletions graphene_federation/apollo_versions/v2_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from graphene_directives import CustomDirective, DirectiveLocation
from graphql import GraphQLArgument, GraphQLDirective, GraphQLList, GraphQLNonNull

from .v2_5 import get_directives as get_directives_v2_5
from ..scalars import FederationPolicy

policy_directive = CustomDirective(
name="policy",
locations=[
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.OBJECT,
DirectiveLocation.INTERFACE,
DirectiveLocation.SCALAR,
DirectiveLocation.ENUM,
],
args={
"policies": GraphQLArgument(
GraphQLNonNull(
GraphQLList(
GraphQLNonNull(GraphQLList(GraphQLNonNull(FederationPolicy)))
)
)
),
},
description="Federation @policy directive",
add_definition_to_schema=False,
)


# No Change, Added Subscription Support
def get_directives() -> dict[str, GraphQLDirective]:
directives = get_directives_v2_5()
directives.update({directive.name: directive for directive in [policy_directive]})
return directives
3 changes: 3 additions & 0 deletions graphene_federation/apollo_versions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ class FederationVersion(Enum):
VERSION_2_1 = "2.1"
VERSION_2_2 = "2.2"
VERSION_2_3 = "2.3"
VERSION_2_4 = "2.4"
VERSION_2_5 = "2.5"
VERSION_2_6 = "2.6"
3 changes: 3 additions & 0 deletions graphene_federation/directives/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from .authenticated import authenticated
from .extends import extends
from .external import external
from .inaccessible import inaccessible
from .interface_object import interface_object
from .key import key
from .override import override
from .policy import policy
from .provides import provides
from .requires import requires
from .requires_scopes import requires_scope
from .shareable import shareable
from .tag import tag
20 changes: 20 additions & 0 deletions graphene_federation/directives/authenticated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Any, Callable

from graphene_directives import directive_decorator

from .utils import is_non_field
from ..apollo_versions import FederationVersion, LATEST_VERSION, get_directive_from_name


def authenticated(
field: Any = None,
*,
federation_version: FederationVersion = LATEST_VERSION,
) -> Callable:
directive = get_directive_from_name(
"authenticated", federation_version=federation_version
)
decorator = directive_decorator(directive)
return (
decorator(field=None)(field) if is_non_field(field) else decorator(field=field)
)
21 changes: 21 additions & 0 deletions graphene_federation/directives/policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Any, Callable

from graphene_directives import directive_decorator

from .utils import is_non_field
from ..apollo_versions import FederationVersion, LATEST_VERSION, get_directive_from_name


def policy(
field: Any = None,
*,
policies: list[list[str]],
federation_version: FederationVersion = LATEST_VERSION,
) -> Callable:
directive = get_directive_from_name("policy", federation_version=federation_version)
decorator = directive_decorator(directive)
return (
decorator(field=None, policies=policies)(field)
if is_non_field(field)
else decorator(field=field, policies=policies)
)
23 changes: 23 additions & 0 deletions graphene_federation/directives/requires_scopes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Any, Callable

from graphene_directives import directive_decorator

from .utils import is_non_field
from ..apollo_versions import FederationVersion, LATEST_VERSION, get_directive_from_name


def requires_scope(
field: Any = None,
*,
scopes: list[list[str]],
federation_version: FederationVersion = LATEST_VERSION,
) -> Callable:
directive = get_directive_from_name(
"requiresScopes", federation_version=federation_version
)
decorator = directive_decorator(directive)
return (
decorator(field=None, scopes=scopes)(field)
if is_non_field(field)
else decorator(field=field, scopes=scopes)
)
2 changes: 2 additions & 0 deletions graphene_federation/scalars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from ._any import _Any
from .federation_policy import FederationPolicy
from .field_set_v1 import _FieldSet
from .field_set_v2 import FieldSet
from .link_import import link_import
from .link_purpose import link_purpose
from .scope import Scope
Loading

0 comments on commit e1d8343

Please sign in to comment.