Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(total: number, childNode: FieldNode | FragmentSpreadNode | InlineFragmentNode) => {
let nodeComplexity = 0;
switch (childNode.kind) {
case Kind.FIELD: {
const field = fields[childNode.name.value];
// Invalid field, should be caught by other validation rules
if (!field) {
break;
}
const fieldType = getNamedType(field.type);
// Get arguments
let args: {[key: string]: any};
try {
args = getArgumentValues(field, childNode, this.options.variables || {});
} catch (e) {
return this.context.reportError(e);
}
// Check if we have child complexity
let childComplexity = 0;
if (isCompositeType(fieldType)) {
childComplexity = this.nodeComplexity(childNode, fieldType);
}
// Run estimators one after another and return first valid complexity
}
// Get one level deeper in the query nesting
const field = getField(parentType, definition);
if (field !== null) {
const fieldType = getNamedType(field.type);
// Make this abstract type concrete if possible
if (
fieldType instanceof GraphQLInterfaceType &&
fieldType.resolveType instanceof Function
) {
// We currenlty only allow resolveType to return a GraphQLObjectType
// and we pass in the wrong values as we don't need this feature currently
// @ts-ignore
return getNamedType(fieldType.resolveType(instance));
} else {
return fieldType;
}
}
return null;
}
}
SelectionSet(node: SelectionSetNode) {
const type = typeInfo.getType();
if (!type) {
throw new Error(`Failed to determine type of SelectionSet node`);
}
if (!isAbstractType(getNamedType(type))) {
// only need __typename for abstract types
return undefined;
}
// this does not check if __typename is aliased in a fragment spread. That would cause a GraphQL error.
// but seriously... nobody would do that. This saves the performance impact of traversing all fragment spreads
const typenameRequest = node.selections.filter(sel => sel.kind == 'Field' && getAliasOrName(sel) == '__typename')[0] as FieldNode | undefined;
if (typenameRequest) {
// make sure nothing else is requested as __typename
if (typenameRequest.name.value != '__typename') {
throw new Error(`Fields must not be aliased to __typename because this is a reserved field.`);
}
// __typename is requested, so no change needed
return undefined;
}
Object.keys(fields).forEach(key => {
const field = fields[key];
const type = getNamedType(field.type);
collectTypes(type);
});
}
parentType: GraphQLCompositeType,
possibleTypes: GraphQLObjectType[],
visitedFragments: Set
): Selection | null {
switch (selectionNode.kind) {
case Kind.FIELD: {
const name = selectionNode.name.value;
const alias = selectionNode.alias ? selectionNode.alias.value : undefined;
const fieldDef = getFieldDef(this.schema, parentType, selectionNode);
if (!fieldDef) {
throw new GraphQLError(`Cannot query field "${name}" on type "${String(parentType)}"`, [selectionNode]);
}
const fieldType = fieldDef.type;
const unmodifiedFieldType = getNamedType(fieldType);
this.addTypeUsed(unmodifiedFieldType);
const { description, isDeprecated, deprecationReason } = fieldDef;
const responseKey = alias || name;
const args =
selectionNode.arguments && selectionNode.arguments.length > 0
? selectionNode.arguments.map(arg => {
const name = arg.name.value;
const argDef = fieldDef.args.find(argDef => argDef.name === arg.name.value);
return {
name,
value: valueFromValueNode(arg.value),
type: (argDef && argDef.type) || undefined,
case 'InlineFragment':
case 'FragmentDefinition':
if (state.type) {
info.type = schema.getType(state.type);
}
break;
case 'Field':
case 'AliasedField':
info.fieldDef =
info.type && state.name
? getFieldDef(schema, info.parentType, state.name)
: null;
info.type = info.fieldDef && info.fieldDef.type;
break;
case 'SelectionSet':
info.parentType = getNamedType(info.type);
break;
case 'Directive':
info.directiveDef = state.name && schema.getDirective(state.name);
break;
case 'Arguments':
const parentDef =
state.prevState.kind === 'Field'
? info.fieldDef
: state.prevState.kind === 'Directive'
? info.directiveDef
: state.prevState.kind === 'AliasedField'
? state.prevState.name &&
getFieldDef(schema, info.parentType, state.prevState.name)
: null;
info.argDefs = parentDef && parentDef.args;
break;
function getRawType(type: GraphQLType): GraphQLNamedType {
return nullthrows(getNamedType(type));
}
function isScalarFieldType(type: GraphQLOutputType): boolean {
const namedType = getNamedType(type);
return (
namedType instanceof GraphQLScalarType ||
namedType instanceof GraphQLEnumType
);
}
export function isGraphQLArgumentType(
type: GraphQLType,
argument: GraphQLArgument,
): boolean {
return getNamedType(argument.type).name === getNamedType(type).name
}