|
| 1 | +import { GraphQLError } from '../error/GraphQLError'; |
| 2 | + |
| 3 | +import type { NullabilityAssertionNode } from '../language/ast'; |
| 4 | +import { Kind } from '../language/kinds'; |
| 5 | +import type { ASTReducer } from '../language/visitor'; |
| 6 | +import { visit } from '../language/visitor'; |
| 7 | + |
| 8 | +import type { GraphQLOutputType } from '../type/definition'; |
| 9 | +import { |
| 10 | + assertListType, |
| 11 | + getNullableType, |
| 12 | + GraphQLList, |
| 13 | + GraphQLNonNull, |
| 14 | + isListType, |
| 15 | + isNonNullType, |
| 16 | +} from '../type/definition'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Implements the "Accounting For Client Controlled Nullability Designators" |
| 20 | + * section of the spec. In particular, this function figures out the true return |
| 21 | + * type of a field by taking into account both the nullability listed in the |
| 22 | + * schema, and the nullability providing by an operation. |
| 23 | + * |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +export function applyRequiredStatus( |
| 27 | + type: GraphQLOutputType, |
| 28 | + nullabilityNode: NullabilityAssertionNode | undefined, |
| 29 | +): GraphQLOutputType { |
| 30 | + // If the field is marked with 0 or 1 nullability designator |
| 31 | + // short-circuit |
| 32 | + if (nullabilityNode === undefined) { |
| 33 | + return type; |
| 34 | + } else if (nullabilityNode?.nullabilityAssertion === undefined) { |
| 35 | + if (nullabilityNode?.kind === Kind.NON_NULL_ASSERTION) { |
| 36 | + return new GraphQLNonNull(getNullableType(type)); |
| 37 | + } else if (nullabilityNode?.kind === Kind.ERROR_BOUNDARY) { |
| 38 | + return getNullableType(type); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const typeStack: [GraphQLOutputType] = [type]; |
| 43 | + |
| 44 | + // Load the nullable version each type in the type definition to typeStack |
| 45 | + while (isListType(getNullableType(typeStack[typeStack.length - 1]))) { |
| 46 | + const list = assertListType( |
| 47 | + getNullableType(typeStack[typeStack.length - 1]), |
| 48 | + ); |
| 49 | + const elementType = list.ofType as GraphQLOutputType; |
| 50 | + typeStack.push(elementType); |
| 51 | + } |
| 52 | + |
| 53 | + // Re-apply nullability to each level of the list from the outside in |
| 54 | + const applyStatusReducer: ASTReducer<GraphQLOutputType> = { |
| 55 | + NonNullAssertion: { |
| 56 | + leave({ nullabilityAssertion }) { |
| 57 | + if (nullabilityAssertion) { |
| 58 | + return new GraphQLNonNull(getNullableType(nullabilityAssertion)); |
| 59 | + } |
| 60 | + |
| 61 | + // We're working with the inner-most type |
| 62 | + const nextType = typeStack.pop(); |
| 63 | + |
| 64 | + // There's no way for nextType to be null if both type and nullabilityNode are valid |
| 65 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 66 | + return new GraphQLNonNull(getNullableType(nextType!)); |
| 67 | + }, |
| 68 | + }, |
| 69 | + ErrorBoundary: { |
| 70 | + leave({ nullabilityAssertion }) { |
| 71 | + if (nullabilityAssertion) { |
| 72 | + return getNullableType(nullabilityAssertion); |
| 73 | + } |
| 74 | + |
| 75 | + // We're working with the inner-most type |
| 76 | + const nextType = typeStack.pop(); |
| 77 | + |
| 78 | + // There's no way for nextType to be null if both type and nullabilityNode are valid |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 80 | + return getNullableType(nextType!); |
| 81 | + }, |
| 82 | + }, |
| 83 | + ListNullabilityOperator: { |
| 84 | + leave({ nullabilityAssertion }) { |
| 85 | + let listType = typeStack.pop(); |
| 86 | + // Skip to the inner-most list |
| 87 | + if (!isListType(getNullableType(listType))) { |
| 88 | + listType = typeStack.pop(); |
| 89 | + } |
| 90 | + |
| 91 | + if (!listType) { |
| 92 | + throw new GraphQLError( |
| 93 | + 'List nullability modifier is too deep.', |
| 94 | + { |
| 95 | + nodes: nullabilityNode |
| 96 | + }, |
| 97 | + ); |
| 98 | + } |
| 99 | + const isRequired = isNonNullType(listType); |
| 100 | + if (nullabilityAssertion) { |
| 101 | + return isRequired |
| 102 | + ? new GraphQLNonNull(new GraphQLList(nullabilityAssertion)) |
| 103 | + : new GraphQLList(nullabilityAssertion); |
| 104 | + } |
| 105 | + |
| 106 | + // We're working with the inner-most list |
| 107 | + return listType; |
| 108 | + }, |
| 109 | + }, |
| 110 | + }; |
| 111 | + |
| 112 | + const modified = visit(nullabilityNode, applyStatusReducer); |
| 113 | + // modifiers must be exactly the same depth as the field type |
| 114 | + if (typeStack.length > 0) { |
| 115 | + throw new GraphQLError( |
| 116 | + 'List nullability modifier is too shallow.', |
| 117 | + { |
| 118 | + nodes: nullabilityNode |
| 119 | + }, |
| 120 | + ); |
| 121 | + } |
| 122 | + return modified; |
| 123 | +} |
0 commit comments