UNPKG

@graphql-mesh/transport-rest

Version:
73 lines (72 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDirectiveAnnotations = getDirectiveAnnotations; exports.serializeArgumentsForInterpolation = serializeArgumentsForInterpolation; const graphql_1 = require("graphql"); const utils_1 = require("@graphql-tools/utils"); function getDirectiveAnnotations(directableObj) { const directiveAnnotations = []; const directiveExtensions = directableObj.extensions?.directives; if (directiveExtensions) { for (const directiveName in directiveExtensions) { const args = directiveExtensions[directiveName]; directiveAnnotations.push({ name: directiveName, args, }); } } const directiveAstNodes = directableObj.astNode?.directives; if (directiveAstNodes) { for (const directiveNode of directiveAstNodes) { const args = {}; for (const argNode of directiveNode.arguments || []) { const argValue = (0, graphql_1.valueFromASTUntyped)(argNode.value); args[argNode.name.value] = argValue; directiveAnnotations.push({ name: directiveNode.name.value, args, }); } } } return directiveAnnotations; } function normalizeArgValueForInterpolation(argValue) { if (argValue instanceof Date) { return argValue.toJSON(); } return argValue; } function serializeArgumentsForInterpolation(argValue, argType) { if (argValue == null) { return argValue; } if ((0, graphql_1.isScalarType)(argType)) { try { return normalizeArgValueForInterpolation(argType.serialize(argValue)); } catch (e) { return normalizeArgValueForInterpolation(argValue); } } else if ((0, graphql_1.isListType)(argType)) { return (0, utils_1.asArray)(argValue).map(item => serializeArgumentsForInterpolation(item, argType.ofType)); } else if ((0, graphql_1.isInputObjectType)(argType)) { const serializedObj = {}; const argTypeFields = argType.getFields(); for (const fieldName in argValue) { const fieldValue = argValue[fieldName]; const fieldType = argTypeFields[fieldName]?.type; if (fieldType) { serializedObj[fieldName] = serializeArgumentsForInterpolation(fieldValue, fieldType); } } return serializedObj; } else if ((0, graphql_1.isNonNullType)(argType)) { return serializeArgumentsForInterpolation(argValue, argType.ofType); } return normalizeArgValueForInterpolation(argValue); }