@apollo/federation
Version:
Apollo Federation Utilities
88 lines (83 loc) • 2.33 kB
text/typescript
import { composeServices } from '../../../compose';
import { requiresFieldsMissingOnBase as validateRequiresFieldsMissingOnBase } from '../';
import {
gql,
graphqlErrorSerializer,
} from 'apollo-federation-integration-testsuite';
expect.addSnapshotSerializer(graphqlErrorSerializer);
describe('requiresFieldsMissingOnBase', () => {
it('does not warn with proper @requires usage', () => {
const serviceA = {
typeDefs: gql`
type Product {
sku: String!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
extend type Product {
sku: String!
id: ID!
weight: Float!
}
`,
name: 'serviceB',
};
const serviceList = [serviceA, serviceB];
const { schema } = composeServices(serviceList);
const warnings = validateRequiresFieldsMissingOnBase({
schema,
serviceList,
});
expect(warnings).toEqual([]);
});
it('warns when requires selects a field not found on the base type', () => {
const serviceA = {
typeDefs: gql`
type Product {
sku: String!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
extend type Product {
id: ID!
}
`,
name: 'serviceB',
};
const serviceC = {
typeDefs: gql`
extend type Product {
id: ID!
weight: Float!
}
`,
name: 'serviceC',
};
const serviceList = [serviceA, serviceB, serviceC];
const { schema } = composeServices(serviceList);
const warnings = validateRequiresFieldsMissingOnBase({
schema,
serviceList,
});
expect(warnings).toMatchInlineSnapshot(`
Array [
Object {
"code": "REQUIRES_FIELDS_MISSING_ON_BASE",
"locations": Array [
Object {
"column": 36,
"line": 4,
},
],
"message": "[serviceC] Product.weight -> requires the field \`id\` to be @external. @external fields must exist on the base type, not an extension.",
},
]
`);
});
});