Skip to content

Commit

Permalink
feat: add support for federation v2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mak626 committed Mar 8, 2024
1 parent e4a69ed commit 99904a6
Show file tree
Hide file tree
Showing 63 changed files with 316 additions and 59 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ If you need to use a version compatible with `graphene` v2 I recommend using the
- [x] v2.2
- [x] v2.3
- [x] v2.4
- [x] v2.5 `STABLE_VERSION` . Rover dev supports only upto v2.5
- [x] v2.6 `LATEST_VERSION`
- [x] v2.5
- [x] v2.6 `STABLE_VERSION` . Rover dev supports only upto v2.6
- [x] v2.7 `LATEST_VERSION`

All directives could be easily integrated with the help of [graphene-directives](https://github.com/strollby/graphene-directives).
Now every directive's values are validated at run time itself by [graphene-directives](https://github.com/strollby/graphene-directives).

### Directives (v2.6)
### Directives (v2.7)

```graphql
directive @composeDirective(name: String!) repeatable on SCHEMA
Expand All @@ -62,7 +63,7 @@ directive @inaccessible on
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @interfaceObject on OBJECT
directive @override(from: String!) on FIELD_DEFINITION
directive @override(from: String!, label: 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
Expand Down Expand Up @@ -98,7 +99,6 @@ directive @policy(policies: [[federation__Policy!]!]!) on
scalar federation__Policy
scalar federation__Scope
scalar FieldSet

```

Read about directives in [official documentation](https://www.apollographql.com/docs/federation/federated-types/federated-directives)
Expand Down
52 changes: 52 additions & 0 deletions federation_spec/federation-v2.7.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!, label: 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: [[federation__Scope!]!]!) on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
directive @policy(policies: [[federation__Policy!]!]!) on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
scalar federation__Policy
scalar federation__Scope
scalar FieldSet
13 changes: 9 additions & 4 deletions graphene_federation/apollo_versions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
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 .v2_7 import get_directives as get_directives_v2_7
from .version import FederationVersion

LATEST_VERSION = FederationVersion.VERSION_2_6
STABLE_VERSION = FederationVersion.VERSION_2_5
LATEST_VERSION = FederationVersion.VERSION_2_7

# Stable version is determined with the latest version that rover cli supports
STABLE_VERSION = FederationVersion.VERSION_2_6


def get_directives_based_on_version(
Expand All @@ -20,7 +23,7 @@ def get_directives_based_on_version(
"""
Returns a dictionary of [directive_name, directive] for the specified federation version
If no match is found for the specified federation version, latest is taken
If no match is found for the specified federation version, the latest is taken
"""
if federation_version == FederationVersion.VERSION_1_0:
return get_directives_v1_0()
Expand All @@ -38,8 +41,10 @@ def get_directives_based_on_version(
return get_directives_v2_5()
if federation_version == FederationVersion.VERSION_2_6:
return get_directives_v2_6()
if federation_version == FederationVersion.VERSION_2_7:
return get_directives_v2_7()

return get_directives_v2_6()
return get_directives_v2_7()


def get_directive_from_name(
Expand Down
24 changes: 24 additions & 0 deletions graphene_federation/apollo_versions/v2_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from graphene_directives import CustomDirective, DirectiveLocation
from graphql import GraphQLArgument, GraphQLDirective, GraphQLNonNull, GraphQLString

from .v2_6 import get_directives as get_directives_v2_6

override_directive = CustomDirective(
name="override",
locations=[
DirectiveLocation.FIELD_DEFINITION,
],
args={
"from": GraphQLArgument(GraphQLNonNull(GraphQLString)),
"label": GraphQLArgument(GraphQLString),
},
description="Federation @override directive",
add_definition_to_schema=False,
)


# @override Change, Added label argument
def get_directives() -> dict[str, GraphQLDirective]:
directives = get_directives_v2_6()
directives.update({directive.name: directive for directive in [override_directive]})
return directives
1 change: 1 addition & 0 deletions graphene_federation/apollo_versions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ class FederationVersion(Enum):
VERSION_2_4 = "2.4"
VERSION_2_5 = "2.5"
VERSION_2_6 = "2.6"
VERSION_2_7 = "2.7"
3 changes: 2 additions & 1 deletion graphene_federation/directives/override.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
def override(
graphene_type,
from_: str,
label: str = None,
*,
federation_version: FederationVersion = LATEST_VERSION,
) -> Callable:
Expand Down Expand Up @@ -40,7 +41,7 @@ def wrapper(field_or_type):
]
)
)
return decorator(field=field_or_type, **{"from": from_})
return decorator(field=field_or_type, **{"from": from_, "label": label})

if graphene_type:
return wrapper(graphene_type)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key"])

type Query {
a: Banana
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key"])

type Query {
a: Banana
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key"])

type Query {
a: A
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key"])

type Query {
a: A
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key", "@requires"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key", "@requires"])

type Query {
camel: Camel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key", "@requires"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key", "@requires"])

type Query {
camel: Camel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@requires"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@requires"])

type Query {
camel: Camel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@requires"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@requires"])

type Query {
camel: Camel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key"])

schema {
query: ChatQuery
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@extends", "@external", "@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@extends", "@external", "@key"])

schema {
query: ChatQuery
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_custom_enum/test_custom_enum_1.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@inaccessible", "@shareable"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@inaccessible", "@shareable"])

type TestCustomEnum @shareable {
testShareableScalar: Episode @shareable
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_custom_enum/test_custom_enum_2.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@inaccessible", "@shareable"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@inaccessible", "@shareable"])

type TestCustomEnum @shareable {
testShareableScalar: Episode @shareable
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_inaccessible/test_inaccessible_1.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@inaccessible"])

type Position @inaccessible {
x: Int!
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_inaccessible/test_inaccessible_2.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@inaccessible"])

type Position @inaccessible {
x: Int!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@inaccessible"])

union SearchResult @inaccessible = Human | Droid | Starship

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@inaccessible"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@inaccessible"])

union SearchResult @inaccessible = Human | Droid | Starship

Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_key/test_compound_primary_key_1.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
user: User
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_key/test_compound_primary_key_2.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
user: User
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
user: User
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
user: User
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_key/test_multiple_keys_1.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
user: User
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_key/test_multiple_keys_2.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
user: User
Expand Down
28 changes: 28 additions & 0 deletions tests/gql/test_override/test_override_1.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@override"])

type Query {
product: Product
_service: _Service!
}

type Product {
sku: ID @override(from: "subgraph-1")
size: Int @override(from: "subgraph-2")
weight: Int @override(from: "subgraph-3", label: "Test label")
}

type _Service {
sdl: String
}

"""
A string-serialized scalar represents a set of fields that's passed to a federated directive, such as @key, @requires, or @provides
"""
scalar FieldSet

"""This string-serialized scalar represents a JWT scope"""
scalar federation__Scope

"""This string-serialized scalar represents an authorization policy."""
scalar federation__Policy
23 changes: 23 additions & 0 deletions tests/gql/test_override/test_override_2.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@override"])

type Query {
product: Product
}

type Product {
sku: ID @override(from: "subgraph-1")
size: Int @override(from: "subgraph-2")
weight: Int @override(from: "subgraph-3", label: "Test label")
}

"""
A string-serialized scalar represents a set of fields that's passed to a federated directive, such as @key, @requires, or @provides
"""
scalar FieldSet

"""This string-serialized scalar represents a JWT scope"""
scalar federation__Scope

"""This string-serialized scalar represents an authorization policy."""
scalar federation__Policy
2 changes: 1 addition & 1 deletion tests/gql/test_provides/test_provides_1.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@external", "@key", "@provides"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@external", "@key", "@provides"])

type Query {
inStockCount: InStockCount
Expand Down
2 changes: 1 addition & 1 deletion tests/gql/test_provides/test_provides_2.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@external", "@key", "@provides"])
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@external", "@key", "@provides"])

type Query {
inStockCount: InStockCount
Expand Down
Loading

0 comments on commit 99904a6

Please sign in to comment.