Skip to content

Commit dabf3a1

Browse files
committed
Feat: Added support up to federation v2.12
Added: `@cacheTag` directive
1 parent 653357c commit dabf3a1

File tree

52 files changed

+189
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+189
-49
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
directive @composeDirective(name: String!) repeatable on SCHEMA
2+
directive @extends on OBJECT | INTERFACE
3+
directive @external on OBJECT | FIELD_DEFINITION
4+
directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
5+
directive @inaccessible on
6+
| FIELD_DEFINITION
7+
| OBJECT
8+
| INTERFACE
9+
| UNION
10+
| ENUM
11+
| ENUM_VALUE
12+
| SCALAR
13+
| INPUT_OBJECT
14+
| INPUT_FIELD_DEFINITION
15+
| ARGUMENT_DEFINITION
16+
directive @interfaceObject on OBJECT
17+
directive @override(from: String!, label: String) on FIELD_DEFINITION
18+
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
19+
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
20+
directive @shareable repeatable on FIELD_DEFINITION | OBJECT
21+
directive @tag(name: String!) repeatable on
22+
| FIELD_DEFINITION
23+
| INTERFACE
24+
| OBJECT
25+
| UNION
26+
| ARGUMENT_DEFINITION
27+
| SCALAR
28+
| ENUM
29+
| ENUM_VALUE
30+
| INPUT_OBJECT
31+
| INPUT_FIELD_DEFINITION
32+
directive @authenticated on
33+
FIELD_DEFINITION
34+
| OBJECT
35+
| INTERFACE
36+
| SCALAR
37+
| ENUM
38+
directive @requiresScopes(scopes: [[federation__Scope!]!]!) on
39+
FIELD_DEFINITION
40+
| OBJECT
41+
| INTERFACE
42+
| SCALAR
43+
| ENUM
44+
directive @policy(policies: [[federation__Policy!]!]!) on
45+
| FIELD_DEFINITION
46+
| OBJECT
47+
| INTERFACE
48+
| SCALAR
49+
| ENUM
50+
directive @context(name: String!) on OBJECT | INTERFACE | UNION
51+
directive @fromContext(field: federation__ContextFieldValue) on ARGUMENT_DEFINITION
52+
directive @listSize(
53+
assumedSize: Int
54+
slicingArguments: [String!]
55+
sizedFields: [String!]
56+
requireOneSlicingArgument: Boolean = true
57+
) on FIELD_DEFINITION
58+
directive @cost(weight: Int!) on
59+
ARGUMENT_DEFINITION
60+
| ENUM
61+
| FIELD_DEFINITION
62+
| INPUT_FIELD_DEFINITION
63+
| OBJECT
64+
| SCALAR
65+
| ENUM
66+
directive @cacheTag(format: String!) repeatable on FIELD_DEFINITION | OBJECT
67+
scalar federation__Policy
68+
scalar federation__Scope
69+
scalar FieldSet
70+
scalar federation__ContextFieldValue

graphene_federation/apollo_versions/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
from .v2_9 import get_directives as get_directives_v2_9
1414
from .v2_10 import get_directives as get_directives_v2_10
1515
from .v2_11 import get_directives as get_directives_v2_11
16+
from .v2_12 import get_directives as get_directives_v2_12
1617
from .version import FederationVersion
1718

18-
LATEST_VERSION = FederationVersion.VERSION_2_11
19+
LATEST_VERSION = FederationVersion.VERSION_2_12
1920

2021
# Stable version is determined with the latest version that rover cli supports
21-
STABLE_VERSION = FederationVersion.VERSION_2_11
22+
STABLE_VERSION = FederationVersion.VERSION_2_12
2223

2324

2425
def get_directives_based_on_version(
@@ -55,8 +56,10 @@ def get_directives_based_on_version(
5556
return get_directives_v2_10()
5657
if federation_version == FederationVersion.VERSION_2_11:
5758
return get_directives_v2_11()
59+
if federation_version == FederationVersion.VERSION_2_12:
60+
return get_directives_v2_12()
5861

59-
return get_directives_v2_11()
62+
return get_directives_v2_12()
6063

6164

6265
def get_directive_from_name(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from graphene_directives import CustomDirective, DirectiveLocation
2+
from graphql import GraphQLArgument, GraphQLDirective, GraphQLNonNull, GraphQLString
3+
4+
from .v2_11 import get_directives as get_directives_v2_11
5+
6+
cache_tag_directive = CustomDirective(
7+
name="cacheTag",
8+
locations=[
9+
DirectiveLocation.FIELD_DEFINITION,
10+
DirectiveLocation.OBJECT,
11+
],
12+
args={
13+
"format": GraphQLArgument(GraphQLNonNull(GraphQLString)),
14+
},
15+
description="Federation @cacheTag directive",
16+
add_definition_to_schema=False,
17+
is_repeatable=True,
18+
)
19+
20+
21+
def get_directives() -> dict[str, GraphQLDirective]:
22+
directives = get_directives_v2_11()
23+
directives.update(
24+
{directive.name: directive for directive in [cache_tag_directive]}
25+
)
26+
return directives

graphene_federation/apollo_versions/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ class FederationVersion(Enum):
1515
VERSION_2_9 = "2.9"
1616
VERSION_2_10 = "2.10"
1717
VERSION_2_11 = "2.11"
18+
VERSION_2_12 = "2.12"

graphene_federation/directives/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .authenticated import authenticated
2+
from .cache_tag import cache_tag
23
from .context import context
34
from .cost import cost
45
from .extends import extends
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Any
2+
3+
from graphene_directives import directive_decorator
4+
from graphene_federation.apollo_versions import (
5+
FederationVersion,
6+
LATEST_VERSION,
7+
get_directive_from_name,
8+
)
9+
10+
from .utils import is_non_field
11+
12+
13+
def cache_tag(
14+
graphene_type=None,
15+
*,
16+
format: str,
17+
federation_version: FederationVersion = LATEST_VERSION,
18+
) -> Any:
19+
"""
20+
Assigns cache tags to cached data in the Apollo Router for active cache invalidation.
21+
Use cache tags to remove specific cached entries on demand when data changes,
22+
instead of waiting for time-to-live (TTL) expiration.
23+
24+
Reference:
25+
https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/reference/directives#cachetag
26+
https://www.apollographql.com/docs/graphos/routing/performance/caching/response-caching/overview
27+
"""
28+
directive = get_directive_from_name("cacheTag", federation_version)
29+
decorator = directive_decorator(directive)
30+
31+
def wrapper(field_or_type):
32+
if is_non_field(field_or_type):
33+
return decorator(field=None, format=format)(field_or_type)
34+
return decorator(field=field_or_type, format=format)
35+
36+
if graphene_type:
37+
return wrapper(graphene_type)
38+
39+
return wrapper

tests/gql/test_annotation_corner_cases/test_annotate_object_with_meta_name_1.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extend schema
2-
@link(url: "https://specs.apollo.dev/federation/v2.11", import: ["@extends", "@external", "@key"])
2+
@link(url: "https://specs.apollo.dev/federation/v2.12", import: ["@extends", "@external", "@key"])
33

44
type Query {
55
a: Banana

tests/gql/test_annotation_corner_cases/test_annotate_object_with_meta_name_2.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extend schema
2-
@link(url: "https://specs.apollo.dev/federation/v2.11", import: ["@extends", "@external", "@key"])
2+
@link(url: "https://specs.apollo.dev/federation/v2.12", import: ["@extends", "@external", "@key"])
33

44
type Query {
55
a: Banana

tests/gql/test_annotation_corner_cases/test_annotated_field_also_used_in_filter_1.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extend schema
2-
@link(url: "https://specs.apollo.dev/federation/v2.11", import: ["@extends", "@external", "@key"])
2+
@link(url: "https://specs.apollo.dev/federation/v2.12", import: ["@extends", "@external", "@key"])
33

44
type Query {
55
a: A

tests/gql/test_annotation_corner_cases/test_annotated_field_also_used_in_filter_2.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extend schema
2-
@link(url: "https://specs.apollo.dev/federation/v2.11", import: ["@extends", "@external", "@key"])
2+
@link(url: "https://specs.apollo.dev/federation/v2.12", import: ["@extends", "@external", "@key"])
33

44
type Query {
55
a: A

0 commit comments

Comments
 (0)