Skip to content

Commit

Permalink
Add support for federation versions up to 2.6 (#1158)
Browse files Browse the repository at this point in the history
  • Loading branch information
nogates authored Feb 15, 2024
1 parent f8b3560 commit 1698ed5
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 16 deletions.
63 changes: 63 additions & 0 deletions ariadne/contrib/federation/definitions/fed_v2.4.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#
# https://specs.apollo.dev/federation/v2.0/federation-v2.0.graphql
#

directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @external on OBJECT | FIELD_DEFINITION
directive @extends on OBJECT | INTERFACE
directive @override(from: String!) on FIELD_DEFINITION
directive @inaccessible on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| SCALAR
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
scalar FieldSet

#
# federation-v2.1
#

directive @composeDirective(name: String!) repeatable on SCHEMA

#
# https://specs.apollo.dev/link/v1.0/link-v1.0.graphql
#

directive @link(
url: String!,
as: String,
import: [Import])
repeatable on SCHEMA

scalar Import

#
# federation-v2.2
#

directive @shareable repeatable on FIELD_DEFINITION | OBJECT

#
# federation-v2.3
#

directive @interfaceObject on OBJECT
84 changes: 84 additions & 0 deletions ariadne/contrib/federation/definitions/fed_v2.5.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# https://specs.apollo.dev/federation/v2.0/federation-v2.0.graphql
#

directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @external on OBJECT | FIELD_DEFINITION
directive @extends on OBJECT | INTERFACE
directive @override(from: String!) on FIELD_DEFINITION
directive @inaccessible on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| SCALAR
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
scalar FieldSet

#
# federation-v2.1
#

directive @composeDirective(name: String!) repeatable on SCHEMA

#
# https://specs.apollo.dev/link/v1.0/link-v1.0.graphql
#

directive @link(
url: String!,
as: String,
import: [Import])
repeatable on SCHEMA

scalar Import

#
# federation-v2.2
#

directive @shareable repeatable on FIELD_DEFINITION | OBJECT

#
# federation-v2.3
#

directive @interfaceObject on OBJECT

#
# federation-v2.5
#

directive @authenticated on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM

scalar federation__Scope

directive @requiresScopes(scopes: [[federation__Scope!]!]!) on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM

95 changes: 95 additions & 0 deletions ariadne/contrib/federation/definitions/fed_v2.6.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# https://specs.apollo.dev/federation/v2.0/federation-v2.0.graphql
#

directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @external on OBJECT | FIELD_DEFINITION
directive @extends on OBJECT | INTERFACE
directive @override(from: String!) on FIELD_DEFINITION
directive @inaccessible on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| SCALAR
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| ARGUMENT_DEFINITION
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
scalar FieldSet

#
# federation-v2.1
#

directive @composeDirective(name: String!) repeatable on SCHEMA

#
# https://specs.apollo.dev/link/v1.0/link-v1.0.graphql
#

directive @link(
url: String!,
as: String,
import: [Import])
repeatable on SCHEMA

scalar Import

#
# federation-v2.2
#

directive @shareable repeatable on FIELD_DEFINITION | OBJECT

#
# federation-v2.3
#

directive @interfaceObject on OBJECT

#
# federation-v2.5
#

directive @authenticated on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM

scalar federation__Scope

directive @requiresScopes(scopes: [[federation__Scope!]!]!) on
FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM

#
# federation-v2.5
#

scalar federation__Policy
directive @policy(policies: [[federation__Policy!]!]!) on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
33 changes: 17 additions & 16 deletions ariadne/contrib/federation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,26 @@ def make_federated_schema(
r"(?<=@link).*?url:.*?\"(.*?)\".?[^)]+?", sdl, re.MULTILINE | re.DOTALL
) # use regex to parse if it's fed 1 or fed 2; adds dedicated typedefs per spec
dirname = os.path.dirname(__file__)
if link and link.group(1) == "https://specs.apollo.dev/federation/v2.0":
definitions = load_schema_from_path(
os.path.join(dirname, "./definitions/fed2_0.graphql")
)
elif link and link.group(1) == "https://specs.apollo.dev/federation/v2.1":
definitions = load_schema_from_path(
os.path.join(dirname, "./definitions/fed2_1.graphql")
)
elif link and link.group(1) == "https://specs.apollo.dev/federation/v2.2":
definitions = load_schema_from_path(
os.path.join(dirname, "./definitions/fed2_2.graphql")
)
elif link and link.group(1) == "https://specs.apollo.dev/federation/v2.3":
definitions = load_schema_from_path(
os.path.join(dirname, "./definitions/fed2_3.graphql")
definitions_folder = os.path.join(dirname, "definitions")
definition_files = os.listdir(definitions_folder)
definition_files.sort()
last_version_file = definition_files[-1]

if link and link.group(1).startswith("https://specs.apollo.dev/federation/"):
fed_version = link.group(1).split("/")[-1]
versioned_schema_file = os.path.join(
definitions_folder, f"fed_{fed_version}.graphql"
)

if os.path.isfile(versioned_schema_file):
definitions = load_schema_from_path(versioned_schema_file)
else:
definitions = load_schema_from_path(
os.path.join(definitions_folder, last_version_file)
)
else:
definitions = load_schema_from_path(
os.path.join(dirname, "./definitions/fed1_0.graphql")
os.path.join(dirname, "./definitions/fed_v1.0.graphql")
)

tdl.append(definitions)
Expand Down
3 changes: 3 additions & 0 deletions ariadne/contrib/federation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
"inaccessible", # Federation 2 directive.
"composeDirective", # Federation 2.1 directive.
"interfaceObject", # Federation 2.3 directive.
"authenticated", # Federation 2.5 directive.
"requiresScopes", # Federation 2.5 directive.
"policy", # Federation 2.6 directive.
]


Expand Down
Loading

0 comments on commit 1698ed5

Please sign in to comment.