@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
34 lines (33 loc) • 1.49 kB
JavaScript
import { GraphQLError, Kind } from 'graphql';
import { validateDirectiveAgainstOriginal } from '../../../helpers.js';
export function InterfaceObjectRules(context) {
return {
DirectiveDefinition(node) {
validateDirectiveAgainstOriginal(node, 'interfaceObject', context);
},
Directive(node) {
if (!context.isAvailableFederationDirective('interfaceObject', node)) {
return;
}
if (context.satisfiesVersionRange('< v2.3')) {
context.reportError(new GraphQLError(` is not yet supported. See https://github.com/graphql-hive/federation-composition/issues/7`, {
extensions: { code: 'UNSUPPORTED_FEATURE' },
}));
return;
}
const typeDef = context.typeNodeInfo.getTypeDef();
if (!typeDef) {
return;
}
if (typeDef.kind !== Kind.OBJECT_TYPE_DEFINITION &&
typeDef.kind !== Kind.OBJECT_TYPE_EXTENSION) {
return;
}
if (!typeDef.directives?.some(d => d.name.value === 'key')) {
context.reportError(new GraphQLError(`The directive can only be applied to entity types but type "${typeDef.name.value}" has no in this subgraph.`, {
extensions: { code: 'INTERFACE_OBJECT_USAGE_ERROR' },
}));
}
},
};
}