diff --git a/ariadne/contrib/federation/definitions/fed1_0.graphql b/ariadne/contrib/federation/definitions/fed_v1.0.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed1_0.graphql rename to ariadne/contrib/federation/definitions/fed_v1.0.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_0.graphql b/ariadne/contrib/federation/definitions/fed_v2.0.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_0.graphql rename to ariadne/contrib/federation/definitions/fed_v2.0.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_1.graphql b/ariadne/contrib/federation/definitions/fed_v2.1.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_1.graphql rename to ariadne/contrib/federation/definitions/fed_v2.1.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_2.graphql b/ariadne/contrib/federation/definitions/fed_v2.2.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_2.graphql rename to ariadne/contrib/federation/definitions/fed_v2.2.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_3.graphql b/ariadne/contrib/federation/definitions/fed_v2.3.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_3.graphql rename to ariadne/contrib/federation/definitions/fed_v2.3.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_4.graphql b/ariadne/contrib/federation/definitions/fed_v2.4.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_4.graphql rename to ariadne/contrib/federation/definitions/fed_v2.4.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_5.graphql b/ariadne/contrib/federation/definitions/fed_v2.5.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_5.graphql rename to ariadne/contrib/federation/definitions/fed_v2.5.graphql diff --git a/ariadne/contrib/federation/definitions/fed2_6.graphql b/ariadne/contrib/federation/definitions/fed_v2.6.graphql similarity index 100% rename from ariadne/contrib/federation/definitions/fed2_6.graphql rename to ariadne/contrib/federation/definitions/fed_v2.6.graphql diff --git a/ariadne/contrib/federation/schema.py b/ariadne/contrib/federation/schema.py index 03bc1fc5e..cea70d82d 100644 --- a/ariadne/contrib/federation/schema.py +++ b/ariadne/contrib/federation/schema.py @@ -76,34 +76,20 @@ 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") - ) - elif link and link.group(1) == "https://specs.apollo.dev/federation/v2.4": - definitions = load_schema_from_path( - os.path.join(dirname, "./definitions/fed2_4.graphql") - ) - elif link and link.group(1) == "https://specs.apollo.dev/federation/v2.5": - definitions = load_schema_from_path( - os.path.join(dirname, "./definitions/fed2_5.graphql") - ) - elif link and link.group(1) == "https://specs.apollo.dev/federation/v2.6": - definitions = load_schema_from_path( - os.path.join(dirname, "./definitions/fed2_6.graphql") - ) + definitions_folder = os.path.join(dirname, "definitions") + definitions = os.listdir(definitions_folder) + definitions.sort() + last_version_file = definitions[-1] + + if link: + # extract version + 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") diff --git a/tests/federation/test_schema_v2.py b/tests/federation/test_schema_v2.py index f4eb5089b..4de78d40a 100644 --- a/tests/federation/test_schema_v2.py +++ b/tests/federation/test_schema_v2.py @@ -386,3 +386,86 @@ def test_federation_2_6_version_is_detected_in_schema(): """ ) +def test_federation_version_not_supported_is_detected_in_schema(): + type_defs = """ + extend schema + @link( + url: "https://specs.apollo.dev/federation/v2.25", + import: [ + "@key", + "@shareable", + "@provides", + "@external", + "@tag", + "@extends", + "@override", + "@interfaceObject", + "@authenticated", + "@requiresScopes", + + ] + ) + + type Query { + rootField: Review + } + + type Review + @interfaceObject + @key(fields: "id") + @authenticated + @requiresScopes(scopes: [["read:review"]]) + @policy(policies: [["role:admin"]]) + { + id: ID! + } + """ + + schema = make_federated_schema(type_defs) + + result = graphql_sync( + schema, + """ + query GetServiceDetails { + _service { + sdl + } + } + """, + ) + + assert result.errors is None + assert sic(result.data["_service"]["sdl"]) == sic( + """ + extend schema + @link( + url: "https://specs.apollo.dev/federation/v2.25", + import: [ + "@key", + "@shareable", + "@provides", + "@external", + "@tag", + "@extends", + "@override", + "@interfaceObject" + "@authenticated", + "@requiresScopes", + ] + ) + + type Query { + rootField: Review + } + + type Review + @interfaceObject + @key(fields: "id") + @authenticated + @requiresScopes(scopes: [["read:review"]]) + @policy(policies: [["role:admin"]]) + { + id: ID! + } + """ + )