-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add matchingEnums to sdl validation rules
- Loading branch information
1 parent
bab5b34
commit 8754e50
Showing
10 changed files
with
524 additions
and
259 deletions.
There are no files selected for viewing
353 changes: 156 additions & 197 deletions
353
packages/apollo-federation/src/composition/__tests__/compose.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
packages/apollo-federation/src/composition/validate/sdl/__tests__/matchingEnums.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import { | ||
GraphQLEnumType, | ||
Kind, | ||
DocumentNode, | ||
validate, | ||
GraphQLSchema, | ||
specifiedDirectives, | ||
} from 'graphql'; | ||
import { validateSDL } from 'graphql/validation/validate'; | ||
import gql from 'graphql-tag'; | ||
|
||
import { composeServices, buildMapsFromServiceList } from '../../../compose'; | ||
import { | ||
astSerializer, | ||
typeSerializer, | ||
selectionSetSerializer, | ||
} from '../../../../snapshotSerializers'; | ||
import { normalizeTypeDefs } from '../../../normalize'; | ||
import federationDirectives from '../../../../directives'; | ||
import { ServiceDefinition } from '../../../types'; | ||
import { matchingEnums } from '../matchingEnums'; | ||
|
||
expect.addSnapshotSerializer(astSerializer); | ||
expect.addSnapshotSerializer(typeSerializer); | ||
expect.addSnapshotSerializer(selectionSetSerializer); | ||
|
||
// simulate the first half of the composition process | ||
const createDefinitionsDocumentForServices = ( | ||
serviceList: ServiceDefinition[], | ||
): DocumentNode => { | ||
const { definitionsMap } = buildMapsFromServiceList(serviceList); | ||
return { | ||
kind: Kind.DOCUMENT, | ||
definitions: Object.values(definitionsMap).flat(), | ||
}; | ||
}; | ||
|
||
describe('matchingEnums', () => { | ||
let schema: GraphQLSchema; | ||
|
||
// create a blank schema for each test | ||
beforeEach(() => { | ||
schema = new GraphQLSchema({ | ||
query: undefined, | ||
directives: [...specifiedDirectives, ...federationDirectives], | ||
}); | ||
}); | ||
|
||
it('does not error with matching enums across services', () => { | ||
const serviceList = [ | ||
{ | ||
typeDefs: gql` | ||
enum ProductCategory { | ||
BED | ||
BATH | ||
} | ||
`, | ||
name: 'serviceA', | ||
}, | ||
|
||
{ | ||
typeDefs: gql` | ||
enum ProductCategory { | ||
BED | ||
BATH | ||
} | ||
`, | ||
name: 'serviceB', | ||
}, | ||
]; | ||
|
||
const definitionsDocument = createDefinitionsDocumentForServices( | ||
serviceList, | ||
); | ||
const errors = validateSDL(definitionsDocument, schema, [matchingEnums]); | ||
expect(errors).toMatchInlineSnapshot(`Array []`); | ||
}); | ||
|
||
it('errors when enums in separate services dont match', () => { | ||
const serviceList = [ | ||
{ | ||
typeDefs: gql` | ||
enum ProductCategory { | ||
BED | ||
BATH | ||
} | ||
`, | ||
name: 'serviceA', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
enum ProductCategory { | ||
BEYOND | ||
} | ||
`, | ||
name: 'serviceB', | ||
}, | ||
]; | ||
|
||
const definitionsDocument = createDefinitionsDocumentForServices( | ||
serviceList, | ||
); | ||
const errors = validateSDL(definitionsDocument, schema, [matchingEnums]); | ||
expect(errors).toMatchInlineSnapshot(` | ||
Array [ | ||
[GraphQLError: The \`ProductCategory\` enum does not have identical values in all services. Groups of services with identical values are: [serviceA], [serviceB]], | ||
] | ||
`); | ||
}); | ||
|
||
it('errors when enums in separate services dont match', () => { | ||
const serviceList = [ | ||
{ | ||
typeDefs: gql` | ||
type Query { | ||
products: [Product]! | ||
} | ||
type Product @key(fields: "sku") { | ||
sku: String! | ||
upc: String! | ||
type: ProductType | ||
} | ||
enum ProductType { | ||
BOOK | ||
FURNITURE | ||
} | ||
`, | ||
name: 'serviceA', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
enum ProductType { | ||
FURNITURE | ||
BOOK | ||
DIGITAL | ||
} | ||
`, | ||
name: 'serviceB', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
enum ProductType { | ||
FURNITURE | ||
BOOK | ||
DIGITAL | ||
} | ||
`, | ||
name: 'serviceC', | ||
}, | ||
]; | ||
|
||
const definitionsDocument = createDefinitionsDocumentForServices( | ||
serviceList, | ||
); | ||
const errors = validateSDL(definitionsDocument, schema, [matchingEnums]); | ||
expect(errors).toMatchInlineSnapshot(` | ||
Array [ | ||
[GraphQLError: The \`ProductType\` enum does not have identical values in all services. Groups of services with identical values are: [serviceA], [serviceB, serviceC]], | ||
] | ||
`); | ||
}); | ||
|
||
it('errors when an enum name is defined as another type in a service', () => { | ||
const serviceList = [ | ||
{ | ||
typeDefs: gql` | ||
enum ProductType { | ||
BOOK | ||
FURNITURE | ||
} | ||
`, | ||
name: 'serviceA', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
type ProductType { | ||
id: String | ||
} | ||
`, | ||
name: 'serviceB', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
enum ProductType { | ||
FURNITURE | ||
BOOK | ||
DIGITAL | ||
} | ||
`, | ||
name: 'serviceC', | ||
}, | ||
]; | ||
|
||
const definitionsDocument = createDefinitionsDocumentForServices( | ||
serviceList, | ||
); | ||
const errors = validateSDL(definitionsDocument, schema, [matchingEnums]); | ||
expect(errors).toMatchInlineSnapshot(` | ||
Array [ | ||
[GraphQLError: [serviceA] ProductType -> ProductType is an enum in [serviceA, serviceC], but not in [serviceB]], | ||
] | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.