@graphql-mesh/transport-rest
Version:
69 lines (68 loc) • 2.51 kB
JavaScript
import { isInputObjectType, isListType, isNonNullType, isScalarType, valueFromASTUntyped, } from 'graphql';
import { asArray } from '@graphql-tools/utils';
export 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 = 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;
}
export function serializeArgumentsForInterpolation(argValue, argType) {
if (argValue == null) {
return argValue;
}
if (isScalarType(argType)) {
try {
return normalizeArgValueForInterpolation(argType.serialize(argValue));
}
catch (e) {
return normalizeArgValueForInterpolation(argValue);
}
}
else if (isListType(argType)) {
return asArray(argValue).map(item => serializeArgumentsForInterpolation(item, argType.ofType));
}
else if (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 (isNonNullType(argType)) {
return serializeArgumentsForInterpolation(argValue, argType.ofType);
}
return normalizeArgValueForInterpolation(argValue);
}