Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-humans-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

tag extraction respects @external on parent
38 changes: 19 additions & 19 deletions __tests__/composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7382,25 +7382,25 @@ testImplementations((api) => {
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
)
type Note @key(fields: "id") @shareable {
id: ID!
name: String
author: User
}
type User @key(fields: "id") {
id: ID!
name: String
id: ID!
name: String
}
type PrivateNote @key(fields: "id") @shareable {
id: ID!
note: Note
}
type Query {
note: Note @shareable
privateNote: PrivateNote @shareable
type PrivateNote @key(fields: "id") @shareable {
id: ID!
note: Note
}
type Query {
note: Note @shareable
privateNote: PrivateNote @shareable
}
`),
},
Expand All @@ -7425,9 +7425,9 @@ testImplementations((api) => {
result = api.composeServices([
{
name: "foo",
typeDefs: parse(/* GraphQL */ `
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@provides", "@shareable"]
)
Expand All @@ -7442,8 +7442,8 @@ testImplementations((api) => {
type PrivateNote @key(fields: "id") @shareable {
id: ID!
note: Note @provides(fields: "name author { id }")
}
type Query {
}
type Query {
note: Note @shareable
privateNote: PrivateNote @shareable
}
Expand All @@ -7461,8 +7461,8 @@ testImplementations((api) => {
id: ID!
name: String
author: User
}
type User @key(fields: "id") {
}
type User @key(fields: "id") {
id: ID!
name: String
}
Expand Down Expand Up @@ -7490,7 +7490,7 @@ testImplementations((api) => {
type Note @key(fields: "id") @shareable {
id: ID!
tag: String
}
}
`),
},
]);
Expand All @@ -7510,7 +7510,7 @@ testImplementations((api) => {
@join__field(external: true, graph: FOO)
@join__field(graph: BAR)
tag: String @join__field(graph: BAZ)
}
}
`);
});

Expand Down
82 changes: 82 additions & 0 deletions __tests__/contracts/federation-tag-extraction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,88 @@ describe("applyTagFilterToInaccessibleTransformOnSubgraphSchema", () => {
`);
});

test("include of object type field if @external is applied to field", () => {
const filter: SchemaContractFilter = {
include: new Set(["tag1"]),
exclude: new Set(),
};
const sdl = parse(/* GraphQL */ `
schema
@link(
url: "https://specs.apollo.dev/federation/v2.0"
import: ["@tag", "@external"]
) {
query: Query
}

type Query {
field1: String! @tag(name: "tag1") @requires(fields: "field2")
field2: String! @external
}
`);

const output = applyTagFilterToInaccessibleTransformOnSubgraphSchema(
sdl,
buildSchemaCoordinateTagRegister([sdl]),
filter,
);

expect(print(output.typeDefs)).toMatchInlineSnapshot(`
"schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@tag", "@external"]) {
query: Query
}

type Query {
field1: String! @requires(fields: "field2")
field2: String! @external
}"
`);
});

test("include of object type field if @external is applied to object", () => {
const filter: SchemaContractFilter = {
include: new Set(["tag1"]),
exclude: new Set(),
};
const sdl = parse(/* GraphQL */ `
schema
@link(
url: "https://specs.apollo.dev/federation/v2.0"
import: ["@tag", "@external"]
) {
query: Query
}

extend type Query @external {
field2: String!
}

type Query {
field1: String! @tag(name: "tag1") @requires(fields: "field2")
}
`);

const output = applyTagFilterToInaccessibleTransformOnSubgraphSchema(
sdl,
buildSchemaCoordinateTagRegister([sdl]),
filter,
);

expect(print(output.typeDefs)).toMatchInlineSnapshot(`
"schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@tag", "@external"]) {
query: Query
}

extend type Query @external {
field2: String!
}

type Query {
field1: String! @requires(fields: "field2")
}"
`);
});

test("object type is excluded even if one of its fields is included", () => {
const filter: SchemaContractFilter = {
include: new Set(["tag1"]),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"typecheck": "tsc --noEmit --project tsconfig.build.json"
},
"peerDependencies": {
"graphql": "^16.0.0"
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
},
"dependencies": {
"constant-case": "^3.0.4",
Expand Down
3 changes: 2 additions & 1 deletion src/contracts/tag-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ export function applyTagFilterToInaccessibleTransformOnSubgraphSchema(
if (
fieldNode.directives?.find(
(d) => d.name.value === externalDirectiveName,
)
) ||
node.directives?.find((d) => d.name.value === externalDirectiveName)
) {
return fieldNode;
}
Expand Down
8 changes: 4 additions & 4 deletions src/subgraph/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Kind,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
OperationTypeNode,
type OperationTypeNode,
SchemaDefinitionNode,
specifiedDirectives as specifiedDirectiveTypes,
specifiedScalarTypes,
Expand Down Expand Up @@ -398,17 +398,17 @@ export function createSubgraphStateBuilder(

const expectedQueryTypeName = decideOnRootTypeName(
schemaDef,
OperationTypeNode.QUERY,
'query' as OperationTypeNode,
"Query",
);
const expectedMutationTypeName = decideOnRootTypeName(
schemaDef,
OperationTypeNode.MUTATION,
'mutation' as OperationTypeNode,
"Mutation",
);
const expectedSubscriptionTypeName = decideOnRootTypeName(
schemaDef,
OperationTypeNode.SUBSCRIPTION,
'subscription' as OperationTypeNode,
"Subscription",
);

Expand Down
8 changes: 4 additions & 4 deletions src/subgraph/validation/rules/known-directives-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DocumentNode,
GraphQLError,
Kind,
OperationTypeNode,
type OperationTypeNode,
specifiedDirectives,
} from "graphql";

Expand Down Expand Up @@ -135,11 +135,11 @@ function getDirectiveLocationForOperation(
operation: OperationTypeNode,
): DirectiveLocation {
switch (operation) {
case OperationTypeNode.QUERY:
case 'query' as OperationTypeNode.QUERY:
return DirectiveLocation.QUERY;
case OperationTypeNode.MUTATION:
case 'mutation' as OperationTypeNode.MUTATION:
return DirectiveLocation.MUTATION;
case OperationTypeNode.SUBSCRIPTION:
case 'subscription' as OperationTypeNode.SUBSCRIPTION:
return DirectiveLocation.SUBSCRIPTION;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ASTVisitor, GraphQLError, OperationTypeNode } from "graphql";
import { ASTVisitor, GraphQLError, type OperationTypeNode } from "graphql";
import type { SubgraphValidationContext } from "../validation-context.js";

export function QueryRootTypeInaccessibleRule(
Expand All @@ -10,7 +10,7 @@ export function QueryRootTypeInaccessibleRule(
SchemaDefinition(node) {
const nonQueryType = node.operationTypes?.find(
(operationType) =>
operationType.operation === OperationTypeNode.QUERY &&
operationType.operation === 'query' &&
operationType.type.name.value !== "Query",
);

Expand All @@ -21,7 +21,7 @@ export function QueryRootTypeInaccessibleRule(
SchemaExtension(node) {
const nonQueryType = node.operationTypes?.find(
(operationType) =>
operationType.operation === OperationTypeNode.QUERY &&
operationType.operation === 'query' &&
operationType.type.name.value !== "Query",
);

Expand Down
8 changes: 4 additions & 4 deletions src/subgraph/validation/rules/root-type-used-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DefinitionNode,
GraphQLError,
isTypeDefinitionNode,
OperationTypeNode,
type OperationTypeNode,
SchemaDefinitionNode,
SchemaExtensionNode,
} from "graphql";
Expand All @@ -26,13 +26,13 @@ function findDefaultRootTypes(definitions: readonly DefinitionNode[]) {
if (isTypeDefinitionNode(definition)) {
if (definition.name.value === "Query") {
foundRootTypes.query = "Query";
found.add(OperationTypeNode.QUERY);
found.add('query' as OperationTypeNode);
} else if (definition.name.value === "Mutation") {
foundRootTypes.mutation = "Mutation";
found.add(OperationTypeNode.MUTATION);
found.add('mutation' as OperationTypeNode);
} else if (definition.name.value === "Subscription") {
foundRootTypes.subscription = "Subscription";
found.add(OperationTypeNode.SUBSCRIPTION);
found.add('subscription' as OperationTypeNode);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/subgraph/validation/validate-subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Kind,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
OperationTypeNode,
parse,
SchemaDefinitionNode,
SchemaExtensionNode,
Expand Down Expand Up @@ -541,13 +540,13 @@ function cleanSubgraphTypeDefsFromSubgraphSpec(typeDefs: DocumentNode) {
(node.kind === Kind.SCHEMA_DEFINITION ||
node.kind === Kind.SCHEMA_EXTENSION) &&
node.operationTypes?.some(
(op) => op.operation === OperationTypeNode.QUERY,
(op) => op.operation === 'query',
),
) as SchemaDefinitionNode | SchemaExtensionNode | undefined;

const queryTypeName =
schemaDef?.operationTypes?.find(
(op) => op.operation === OperationTypeNode.QUERY,
(op) => op.operation === 'query',
)?.type.name.value ?? "Query";

(typeDefs.definitions as unknown as DefinitionNode[]) =
Expand Down
8 changes: 4 additions & 4 deletions src/supergraph/composition/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
NamedTypeNode,
ObjectTypeDefinitionNode,
OperationTypeDefinitionNode,
OperationTypeNode,
type OperationTypeNode,
parseConstValue,
parseType,
ScalarTypeDefinitionNode,
Expand Down Expand Up @@ -77,21 +77,21 @@ export function createSchemaNode(schema: {
schema.query
? {
kind: Kind.OPERATION_TYPE_DEFINITION,
operation: OperationTypeNode.QUERY,
operation: 'query' as OperationTypeNode,
type: createNamedTypeNode(schema.query),
}
: [],
schema.mutation
? {
kind: Kind.OPERATION_TYPE_DEFINITION,
operation: OperationTypeNode.MUTATION,
operation: 'mutation' as OperationTypeNode,
type: createNamedTypeNode(schema.mutation),
}
: [],
schema.subscription
? {
kind: Kind.OPERATION_TYPE_DEFINITION,
operation: OperationTypeNode.SUBSCRIPTION,
operation: 'subscription' as OperationTypeNode,
type: createNamedTypeNode(schema.subscription),
}
: [],
Expand Down
1 change: 0 additions & 1 deletion src/supergraph/validation/rules/satisfiablity-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
GraphQLError,
Kind,
ListValueNode,
OperationTypeNode,
print,
specifiedScalarTypes,
ValueNode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OperationTypeNode } from "graphql";
import type { OperationTypeNode } from "graphql";
import { Logger, LoggerContext } from "../../../../utils/logger.js";
import type { SupergraphState } from "../../../state.js";
import { MERGEDGRAPH_ID, SUPERGRAPH_ID } from "./constants.js";
Expand Down
6 changes: 3 additions & 3 deletions src/supergraph/validation/rules/satisfiablity/walker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OperationTypeNode } from "graphql";
import type { OperationTypeNode } from "graphql";
import type { Logger } from "../../../../utils/logger.js";
import { isAbstractEdge, isFieldEdge, type Edge } from "./edge.js";
import { LazyErrors, SatisfiabilityError } from "./errors.js";
Expand Down Expand Up @@ -119,9 +119,9 @@ export class Walker {
}

const rootNode = this.supergraph.nodeOf(
operationType === OperationTypeNode.QUERY
operationType === 'query'
? "Query"
: operationType === OperationTypeNode.MUTATION
: operationType === 'mutation'
? "Mutation"
: "Subscription",
false,
Expand Down