@commitspark/graphql-api
Version:
GraphQL API to store and manage structured data with Git
38 lines (31 loc) • 1.13 kB
text/typescript
import { GraphQLSchema, GraphQLUnionType, Kind } from 'graphql'
function checkUnionMembersConsistentUseOfEntryDirective(
schema: GraphQLSchema,
): string {
const typeMap = schema.getTypeMap()
for (const [key, type] of Object.entries(typeMap)) {
if (type.astNode?.kind !== Kind.UNION_TYPE_DEFINITION) {
continue
}
const innerTypes = (type as GraphQLUnionType).getTypes()
const numberUnionMembersWithEntryDirective = innerTypes.filter(
(innerType) =>
!!innerType.astNode &&
innerType.astNode.directives?.find(
(directive) => directive.name.value === 'Entry',
) !== undefined,
).length
if (
numberUnionMembersWithEntryDirective !== 0 &&
numberUnionMembersWithEntryDirective !== innerTypes.length
) {
return `Either all union members of "${type.name}" must have "@Entry" directive or none.`
}
}
return ''
}
export function getValidationResult(schema: GraphQLSchema): string[] {
const results = []
results.push(checkUnionMembersConsistentUseOfEntryDirective(schema))
return results.filter((result) => result !== '')
}