Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created a new precomposition rule to detect fed2 schemas #1723

Merged
merged 5 commits into from
Apr 13, 2022
Merged
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
2 changes: 1 addition & 1 deletion federation-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This CHANGELOG pertains only to Apollo Federation packages in the `0.x` range. T

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.

- _Nothing yet! Stay tuned._
- Composition will detect and error if it finds fed2 subgraphs [PR #1723](https://github.com/apollographql/federation/pull/1723).

## v0.36.0
- Support for Node 17 [PR #1648](https://github.com/apollographql/federation/pull/1648).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { noFed2Subgraphs as validateNoFed2Subgraphs } from '../';
import {
gql,
graphqlErrorSerializer,
} from 'apollo-federation-integration-testsuite';

expect.addSnapshotSerializer(graphqlErrorSerializer);

describe('noFed2Subgraphs', () => {
it('does not compose when fed2 semantics are detected. fed2.0', () => {
const serviceA = {
typeDefs: gql`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.0",
import: [ "@key" ]
)
type Product @key(fields: "color { id value }") {
sku: String!
upc: String!
color: Color!
}

type Color {
id: ID!
value: String!
}
`,
name: 'serviceA',
};

const errors = validateNoFed2Subgraphs(serviceA);
expect(errors).toMatchInlineSnapshot(`
Array [
Object {
"code": "NO_FED2_SUBGRAPHS",
"locations": Array [],
"message": "[serviceA] Schema contains a Federation 2 subgraph. Only federation 1 subgraphs can be composed with the fed1 composer.",
},
]
`);
});

it('does not compose when fed2 semantics are detected. link 1.0', () => {
const serviceA = {
typeDefs: gql`
extend schema
@link(
url: "https://specs.apollo.dev/link/v1.0"
)
type Product @key(fields: "color { id value }") {
sku: String!
upc: String!
color: Color!
}

type Color {
id: ID!
value: String!
}
`,
name: 'serviceA',
};

const errors = validateNoFed2Subgraphs(serviceA);
expect(errors).toMatchInlineSnapshot(`
Array [
Object {
"code": "NO_FED2_SUBGRAPHS",
"locations": Array [],
"message": "[serviceA] Schema contains a Federation 2 subgraph. Only federation 1 subgraphs can be composed with the fed1 composer.",
},
]
`);
});


it('composes just fine when versions are sufficiently old', () => {
const serviceA = {
typeDefs: gql`
extend schema
@link(
url: "https://specs.apollo.dev/link/v0.2"
)
@link(
url: "https://specs.apollo.dev/federation/v1.0"
)
type Product @key(fields: "color { id value }") {
sku: String!
upc: String!
color: Color!
}

type Color {
id: ID!
value: String!
}
`,
name: 'serviceA',
};

const errors = validateNoFed2Subgraphs(serviceA);
expect(errors).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { keyFieldsMissingExternal } from './keyFieldsMissingExternal';
export { reservedFieldUsed } from './reservedFieldUsed';
export { duplicateEnumOrScalar } from './duplicateEnumOrScalar';
export { duplicateEnumValue } from './duplicateEnumValue';
export { noFed2Subgraphs } from './noFed2Subgraphs';
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { visit, GraphQLError, Kind } from 'graphql';
import { ServiceDefinition } from '../../types';

import { errorWithCode, findDirectivesOnNode } from '../../utils';

/**
* - There are no subgraphs that @link to a version that is unsupported by 1.0 composition.
*/
export const noFed2Subgraphs = ({
name: serviceName,
typeDefs,
}: ServiceDefinition) => {
const errors: GraphQLError[] = [];
visit(typeDefs, {
SchemaExtension(schemaExtensionNode) {
const directives = findDirectivesOnNode(schemaExtensionNode, 'link').filter(directive => {
if (directive.name.value === 'link') {
const argNode = directive.arguments?.find(
arg => arg.name.value === 'url',
);
if (argNode) {
if (argNode.value.kind === Kind.STRING) {
const url = argNode.value.value;
const [,spec, versionRaw] = url.match(
/(federation|link)\/v(\d*\.\d*)/,
)!;

const version = parseFloat(versionRaw);
return (
(spec === 'federation' && version >= 2.0) ||
(spec === 'link' && version >= 1.0)
);
}
}
}
return false;
});
if (directives.length > 0) {
errors.push(
errorWithCode(
'NO_FED2_SUBGRAPHS',
`[${serviceName}] Schema contains a Federation 2 subgraph. Only federation 1 subgraphs can be composed with the fed1 composer.`,
)
);
}
},
});

return errors;
};