-
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.
Separate handling of union validations
- Loading branch information
1 parent
561470f
commit 1f00664
Showing
6 changed files
with
200 additions
and
106 deletions.
There are no files selected for viewing
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
95 changes: 95 additions & 0 deletions
95
packages/apollo-federation/src/composition/validate/sdl/__tests__/matchingUnions.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,95 @@ | ||
import { | ||
GraphQLSchema, | ||
specifiedDirectives, | ||
Kind, | ||
DocumentNode, | ||
} from 'graphql'; | ||
import { validateSDL } from 'graphql/validation/validate'; | ||
import gql from 'graphql-tag'; | ||
import { | ||
typeSerializer, | ||
graphqlErrorSerializer, | ||
} from '../../../../snapshotSerializers'; | ||
import { UniqueUnionTypes } from '..'; | ||
import { ServiceDefinition } from '../../../types'; | ||
import { buildMapsFromServiceList } from '../../../compose'; | ||
import federationDirectives from '../../../../directives'; | ||
|
||
expect.addSnapshotSerializer(graphqlErrorSerializer); | ||
expect.addSnapshotSerializer(typeSerializer); | ||
|
||
function createDocumentsForServices( | ||
serviceList: ServiceDefinition[], | ||
): DocumentNode[] { | ||
const { definitionsMap, extensionsMap } = buildMapsFromServiceList( | ||
serviceList, | ||
); | ||
return [ | ||
{ | ||
kind: Kind.DOCUMENT, | ||
definitions: Object.values(definitionsMap).flat(), | ||
}, | ||
{ | ||
kind: Kind.DOCUMENT, | ||
definitions: Object.values(extensionsMap).flat(), | ||
}, | ||
]; | ||
} | ||
|
||
describe('MatchingUnions', () => { | ||
let schema: GraphQLSchema; | ||
|
||
// create a blank schema for each test | ||
beforeEach(() => { | ||
schema = new GraphQLSchema({ | ||
query: undefined, | ||
directives: [...specifiedDirectives, ...federationDirectives], | ||
}); | ||
}); | ||
|
||
it('enforces unique union names', () => { | ||
const [definitions] = createDocumentsForServices([ | ||
{ | ||
typeDefs: gql` | ||
union UPC = String | Int | ||
`, | ||
name: 'serviceA', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
union UPC = String | Int | Boolean | ||
`, | ||
name: 'serviceB', | ||
}, | ||
]); | ||
|
||
const errors = validateSDL(definitions, schema, [UniqueUnionTypes]); | ||
expect(errors).toHaveLength(1); | ||
expect(errors[0]).toMatchInlineSnapshot(` | ||
Object { | ||
"code": "VALUE_TYPE_UNION_TYPES_MISMATCH", | ||
"message": "The union 'UPC' is defined in multiple places, however the unioned types do not match. Union types with the same name must also consist of identical types. The type Boolean is mismatched.", | ||
} | ||
`); | ||
}); | ||
|
||
it('permits duplicate union names for identical union types', () => { | ||
const [definitions] = createDocumentsForServices([ | ||
{ | ||
typeDefs: gql` | ||
union UPC = String | Int | ||
`, | ||
name: 'serviceA', | ||
}, | ||
{ | ||
typeDefs: gql` | ||
union UPC = String | Int | ||
`, | ||
name: 'serviceB', | ||
}, | ||
]); | ||
|
||
const errors = validateSDL(definitions, schema, [UniqueUnionTypes]); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
}); |
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
5 changes: 2 additions & 3 deletions
5
packages/apollo-federation/src/composition/validate/sdl/index.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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
export { | ||
UniqueTypeNamesWithoutEnumsOrScalars, | ||
} from './uniqueTypeNamesWithoutEnumsOrScalars'; | ||
export { UniqueTypeNamesWithFields } from './uniqueTypeNamesWithFields'; | ||
export { MatchingEnums } from './matchingEnums'; | ||
export { PossibleTypeExtensions } from './possibleTypeExtensions'; | ||
export { UniqueFieldDefinitionNames } from './uniqueFieldDefinitionNames'; | ||
export { UniqueUnionTypes } from './matchingUnions'; |
Oops, something went wrong.