Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function transformNode(
exeContext: ExecutionContext,
fieldNodes: FieldNode[],
type: GraphQLType
): QueryMetadata | null {
if (isNonNullType(type)) {
const nullable = transformNode(exeContext, fieldNodes, type.ofType);
if (nullable != null) {
nullable.isNullable = false;
return nullable;
}
return null;
}
if (isObjectType(type)) {
const subfields = collectSubfields(exeContext, type, fieldNodes);
const properties = Object.create(null);
for (const responseName of Object.keys(subfields)) {
const fieldType = resolveFieldDef(
exeContext,
type,
subfields[responseName]
);
if (!fieldType) {
// if field does not exist, it should be ignored for compatibility concerns.
// Usually, validation would stop it before getting here but this could be an old query
continue;
}
const property = transformNode(
exeContext,
subfields[responseName],
// runtime Object type and complete for that type.
if (isAbstractType(returnType)) {
// $FlowFixMe
return completeAbstractValue(
exeContext,
returnType,
fieldNodes,
info,
path,
result,
tracing,
);
}
// If field type is Object, execute and complete all sub-selections.
if (isObjectType(returnType)) {
// $FlowFixMe
return completeObjectValue(
exeContext,
returnType,
fieldNodes,
info,
path,
result,
tracing,
);
}
// Not reachable. All possible output types have been considered.
/* istanbul ignore next */
throw new Error(
`Cannot complete value of unexpected type "${String(
).some(namedType => {
if (!isObjectType(namedType)) return false;
// for every object type, loop over its fields and find fields
// with requires directives
return Object.values(namedType.getFields()).some(field =>
findDirectivesOnTypeOrField(field.astNode, 'requires').some(
directive => {
if (!directive.arguments) return false;
const selections =
isStringValueNode(directive.arguments[0].value) &&
parseSelections(directive.arguments[0].value.value);
if (!selections) return false;
return selectionIncludesField({
selections,
selectionSetType: namedType,
typeToFind: parentType,
fieldToFind: externalFieldName,
fieldNodes,
previousPath,
errorPath,
sourcePath
)}), null) : `;
if (isLeafType(type)) {
body += compileLeafType(
context,
type,
originPaths,
fieldNodes,
previousPath,
errorDestination
);
} else if (isObjectType(type)) {
const fieldMap = collectSubfields(context, type, fieldNodes);
body += compileObjectType(
context,
type,
fieldNodes,
originPaths,
destinationPaths,
previousPath,
errorDestination,
fieldMap,
false
);
} else if (isAbstractType(type)) {
body += compileAbstractType(
context,
parentType,
nullable: boolean
): GraphQLOutputType {
if (isNonNullType(type)) {
return unwrap(type.ofType, false)
}
if (nullable) {
addModifier('Nullable')
}
if (isListType(type)) {
addModifier('ReadonlyArray')
return unwrap(type.ofType, true)
}
if (isScalarType(type) || isEnumType(type) || isObjectType(type))
return type
throw 'unhandled GraphQLOutputType in unwrap'
}
);
}
if (value === undefined) {
const typeName = nameForType(type as GraphQLLeafType);
return [
error(
keyPath,
`should be ${articleForType(
typeName,
)} ${typeName} (or null), but was undefined`,
),
];
}
if (isObjectType(type)) {
if (valueType === 'object') {
if (shallow) {
return [];
} else {
const fields = type.getFields();
return Object.keys(value).reduce((fieldErrors: Error[], key) => {
const fieldKeyPath = updateKeyPath(keyPath, key);
return fields[key] == null
? fieldErrors.concat([
error(
fieldKeyPath,
`does not exist on type ${
type.name
} (available fields: ${Object.keys(fields).join(', ')})`,
),
export function fieldNotFoundMessageForType(type: GraphQLType | null): string {
if (type === null) {
return "The type should not be null.";
}
if (isScalarType(type)) {
return "The field has a scalar type, which means it supports no nesting.";
}
if (isEnumType(type)) {
return "The field has an enum type, which means it supports no nesting.";
}
if (isObjectType(type)) {
return `The only fields found in this Object are: ${Object.keys(
type.getFields()
)}.`;
}
return "";
}
export const objectType = (type: GraphQLObjectType | GraphQLInterfaceType, ctx: RenderContext) => {
const fields = Object.keys(type.getFields()).map(fieldName => type.getFields()[fieldName])
if (!ctx.schema) throw new Error('no schema provided')
const typeNames = isObjectType(type) ? [type.name] : ctx.schema.getPossibleTypes(type).map(t => t.name)
const fieldStrings = fields
.map(f => `${fieldComment(f)}${f.name}${renderTyping(f.type, false, false)}`)
.concat([`__typename:${typeNames.length > 0 ? typeNames.map(t => `'${t}'`).join('|') : 'String'}`])
const interfaceNames = isObjectType(type) ? type.getInterfaces().map(i => i.name) : []
ctx.addCodeBlock(
`${typeComment(type)}export interface ${type.name}${
interfaceNames.length > 0 ? ` extends ${interfaceNames.join(',')}` : ''
}{${fieldStrings.join(',')}}`,
)
}
function isFederationObjectType(node: ObjectTypeDefinitionNode | GraphQLObjectType): boolean {
const name = isObjectType(node) ? node.name : node.name.value;
const directives = isObjectType(node) ? node.astNode.directives : node.directives;
const isNotRoot = !['Query', 'Mutation', 'Subscription'].includes(name);
const isNotIntrospection = !name.startsWith('__');
const hasKeyDirective = directives.some(d => d.name.value === 'key');
return isNotRoot && isNotIntrospection && hasKeyDirective;
}