UNPKG

@theguild/federation-composition

Version:
108 lines (107 loc) • 4.27 kB
import { GraphQLError, Kind, specifiedScalarTypes, } from "graphql"; import { print } from "../../../graphql/printer.js"; export function ProvidedArgumentsOnDirectivesRule(context) { return { Directive: { leave(directiveNode, _k, _p, _pp, ancestors) { const directiveName = directiveNode.name.value; const directiveDefinition = context.getKnownDirectiveDefinition(directiveName); if (!directiveDefinition) { return; } const args = directiveNode.arguments; if (args && directiveDefinition.arguments) { const coordinate = context.getSchemaCoordinate(ancestors); for (const arg of args) { const argDefinition = directiveDefinition.arguments.find((a) => a.name.value === arg.name.value); if (argDefinition && arg.value && arg.value.kind !== Kind.NULL && !isValidValue(argDefinition.type, arg.value, context)) { context.reportError(new GraphQLError(`Invalid value for "@${directiveName}(${arg.name.value}:)" of type "${print(argDefinition.type)}" in application of "@${directiveName}" to "${coordinate}".`, { nodes: arg, extensions: { code: "INVALID_GRAPHQL", }, })); } } } }, }, }; } function isValidValue(type, value, context) { if (type.kind === Kind.NON_NULL_TYPE) { return value.kind !== Kind.NULL && isValidValue(type.type, value, context); } if (value.kind === Kind.NULL) { return true; } if (type.kind === Kind.LIST_TYPE) { return value.kind === Kind.LIST ? value.values.every((item) => isValidValue(type.type, item, context)) : isValidValue(type.type, value, context); } const printedType = printTypeNode(type, context); if (!printedType || printedType === "Any") { return true; } if (value.kind === Kind.LIST) { return false; } const printedValue = printValueNode(value); const typeName = type.name.value; return (printedValue === printedType || (printedValue === "Int" && typeName === "Float") || (typeName === "ID" && (printedValue === "String" || printedValue === "Int"))); } function printValueNode(valueNode) { switch (valueNode.kind) { case Kind.ENUM: return "Enum"; case Kind.NULL: return "Null"; case Kind.INT: return "Int"; case Kind.FLOAT: return "Float"; case Kind.STRING: return "String"; case Kind.BOOLEAN: return "Boolean"; case Kind.OBJECT: return "Object"; } throw new Error(`Unknown value node kind: ${valueNode}`); } function printTypeNode(typeNode, context) { if (typeNode.kind === Kind.NAMED_TYPE) { const def = context.getKnownTypeDefinition(typeNode.name.value); if (!def) { const specifiedScalar = specifiedScalarTypes.find((s) => s.name === typeNode.name.value); if (specifiedScalar) { return specifiedScalar.name; } return null; } switch (def.kind) { case Kind.SCALAR_TYPE_DEFINITION: case Kind.SCALAR_TYPE_EXTENSION: return "Any"; case Kind.ENUM_TYPE_DEFINITION: case Kind.ENUM_TYPE_EXTENSION: return "Enum"; case Kind.INPUT_OBJECT_TYPE_DEFINITION: case Kind.INPUT_OBJECT_TYPE_EXTENSION: case Kind.OBJECT_TYPE_DEFINITION: case Kind.OBJECT_TYPE_EXTENSION: case Kind.INTERFACE_TYPE_DEFINITION: case Kind.INTERFACE_TYPE_EXTENSION: case Kind.UNION_TYPE_DEFINITION: case Kind.UNION_TYPE_EXTENSION: return "Object"; } } return printTypeNode(typeNode.type, context); }