@apollo/federation
Version:
Apollo Federation Utilities
118 lines (106 loc) • 3.05 kB
text/typescript
import { composeServices } from '../../../compose';
import { providesFieldsMissingExternal as validateProdivesFieldsMissingExternal } from '../';
import {
gql,
graphqlErrorSerializer,
} from 'apollo-federation-integration-testsuite';
import { assertCompositionSuccess } from '../../../utils';
expect.addSnapshotSerializer(graphqlErrorSerializer);
describe('providesFieldsMissingExternal', () => {
it('does not warn with proper @provides usage', () => {
const serviceA = {
typeDefs: gql`
type Product {
sku: String!
upc: String!
id: ID!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
type User {
id: ID!
username: String
}
`,
name: 'serviceB',
};
const serviceC = {
typeDefs: gql`
type Review {
id: ID!
product: Product
author: User
}
extend type Product {
sku: String!
id: ID!
price: Int!
}
extend type User {
id: ID!
username: String
}
`,
name: 'serviceC',
};
const serviceList = [serviceA, serviceB, serviceC];
const compositionResult = composeServices(serviceList);
assertCompositionSuccess(compositionResult);
const { schema } = compositionResult;
const warnings = validateProdivesFieldsMissingExternal({
schema,
serviceList,
});
expect(warnings).toEqual([]);
});
it('warns when there is a @provides with no matching @external field', () => {
const serviceA = {
typeDefs: gql`
type Product {
sku: String!
upc: String!
id: ID!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
type Review {
id: ID!
product: Product
}
extend type Product {
sku: String!
price: Int!
}
`,
name: 'serviceB',
};
const serviceList = [serviceA, serviceB];
const compositionResult = composeServices(serviceList);
assertCompositionSuccess(compositionResult);
const { schema } = compositionResult;
const warnings = validateProdivesFieldsMissingExternal({
schema,
serviceList,
});
expect(warnings).toMatchInlineSnapshot(`
Array [
Object {
"code": "PROVIDES_FIELDS_MISSING_EXTERNAL",
"locations": Array [
Object {
"column": 3,
"line": 3,
},
],
"message": "[serviceB] Review.product -> provides the field \`id\` and requires Product.id to be marked as @external.",
},
]
`);
});
});