|
| 1 | +import type { SchemaCoordinateNode } from '../language/ast.js'; |
| 2 | +import { parseSchemaCoordinate } from '../language/parser.js'; |
| 3 | +import type { Source } from '../language/source.js'; |
| 4 | + |
| 5 | +import type { |
| 6 | + GraphQLArgument, |
| 7 | + GraphQLEnumValue, |
| 8 | + GraphQLField, |
| 9 | + GraphQLInputField, |
| 10 | + GraphQLNamedType, |
| 11 | +} from '../type/definition.js'; |
| 12 | +import { |
| 13 | + isEnumType, |
| 14 | + isInputObjectType, |
| 15 | + isInterfaceType, |
| 16 | + isObjectType, |
| 17 | +} from '../type/definition.js'; |
| 18 | +import type { GraphQLDirective } from '../type/directives.js'; |
| 19 | +import type { GraphQLSchema } from '../type/schema.js'; |
| 20 | + |
| 21 | +/** |
| 22 | + * A resolved schema element may be one of the following kinds: |
| 23 | + */ |
| 24 | +export type ResolvedSchemaElement = |
| 25 | + | { |
| 26 | + readonly kind: 'NamedType'; |
| 27 | + readonly type: GraphQLNamedType; |
| 28 | + } |
| 29 | + | { |
| 30 | + readonly kind: 'Field'; |
| 31 | + readonly type: GraphQLNamedType; |
| 32 | + readonly field: GraphQLField<unknown, unknown>; |
| 33 | + } |
| 34 | + | { |
| 35 | + readonly kind: 'InputField'; |
| 36 | + readonly type: GraphQLNamedType; |
| 37 | + readonly inputField: GraphQLInputField; |
| 38 | + } |
| 39 | + | { |
| 40 | + readonly kind: 'EnumValue'; |
| 41 | + readonly type: GraphQLNamedType; |
| 42 | + readonly enumValue: GraphQLEnumValue; |
| 43 | + } |
| 44 | + | { |
| 45 | + readonly kind: 'FieldArgument'; |
| 46 | + readonly type: GraphQLNamedType; |
| 47 | + readonly field: GraphQLField<unknown, unknown>; |
| 48 | + readonly fieldArgument: GraphQLArgument; |
| 49 | + } |
| 50 | + | { |
| 51 | + readonly kind: 'Directive'; |
| 52 | + readonly directive: GraphQLDirective; |
| 53 | + } |
| 54 | + | { |
| 55 | + readonly kind: 'DirectiveArgument'; |
| 56 | + readonly directive: GraphQLDirective; |
| 57 | + readonly directiveArgument: GraphQLArgument; |
| 58 | + }; |
| 59 | + |
| 60 | +/** |
| 61 | + * A schema coordinate is resolved in the context of a GraphQL schema to |
| 62 | + * uniquely identifies a schema element. It returns undefined if the schema |
| 63 | + * coordinate does not resolve to a schema element. |
| 64 | + * |
| 65 | + * https://spec.graphql.org/draft/#sec-Schema-Coordinates.Semantics |
| 66 | + */ |
| 67 | +export function resolveSchemaCoordinate( |
| 68 | + schema: GraphQLSchema, |
| 69 | + schemaCoordinate: string | Source, |
| 70 | +): ResolvedSchemaElement | undefined { |
| 71 | + return resolveASTSchemaCoordinate( |
| 72 | + schema, |
| 73 | + parseSchemaCoordinate(schemaCoordinate), |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Resolves schema coordinate from a parsed SchemaCoordinate node. |
| 79 | + */ |
| 80 | +export function resolveASTSchemaCoordinate( |
| 81 | + schema: GraphQLSchema, |
| 82 | + schemaCoordinate: SchemaCoordinateNode, |
| 83 | +): ResolvedSchemaElement | undefined { |
| 84 | + const { ofDirective, name, memberName, argumentName } = schemaCoordinate; |
| 85 | + if (ofDirective) { |
| 86 | + // SchemaCoordinate : |
| 87 | + // - @ Name |
| 88 | + // - @ Name ( Name : ) |
| 89 | + // Let {directiveName} be the value of the first {Name}. |
| 90 | + // Let {directive} be the directive in the {schema} named {directiveName}. |
| 91 | + const directive = schema.getDirective(name.value); |
| 92 | + if (!argumentName) { |
| 93 | + // SchemaCoordinate : @ Name |
| 94 | + // Return the directive in the {schema} named {directiveName}. |
| 95 | + if (!directive) { |
| 96 | + return; |
| 97 | + } |
| 98 | + return { kind: 'Directive', directive }; |
| 99 | + } |
| 100 | + |
| 101 | + // SchemaCoordinate : @ Name ( Name : ) |
| 102 | + // Assert {directive} must exist. |
| 103 | + if (!directive) { |
| 104 | + return; |
| 105 | + } |
| 106 | + // Let {directiveArgumentName} be the value of the second {Name}. |
| 107 | + // Return the argument of {directive} named {directiveArgumentName}. |
| 108 | + const directiveArgument = directive.args.find( |
| 109 | + (arg) => arg.name === argumentName.value, |
| 110 | + ); |
| 111 | + if (!directiveArgument) { |
| 112 | + return; |
| 113 | + } |
| 114 | + return { kind: 'DirectiveArgument', directive, directiveArgument }; |
| 115 | + } |
| 116 | + |
| 117 | + // SchemaCoordinate : |
| 118 | + // - Name |
| 119 | + // - Name . Name |
| 120 | + // - Name . Name ( Name : ) |
| 121 | + // Let {typeName} be the value of the first {Name}. |
| 122 | + // Let {type} be the type in the {schema} named {typeName}. |
| 123 | + const type = schema.getType(name.value); |
| 124 | + if (!memberName) { |
| 125 | + // SchemaCoordinate : Name |
| 126 | + // Return the type in the {schema} named {typeName}. |
| 127 | + if (!type) { |
| 128 | + return; |
| 129 | + } |
| 130 | + return { kind: 'NamedType', type }; |
| 131 | + } |
| 132 | + |
| 133 | + if (!argumentName) { |
| 134 | + // SchemaCoordinate : Name . Name |
| 135 | + // If {type} is an Enum type: |
| 136 | + if (isEnumType(type)) { |
| 137 | + // Let {enumValueName} be the value of the second {Name}. |
| 138 | + // Return the enum value of {type} named {enumValueName}. |
| 139 | + const enumValue = type.getValue(memberName.value); |
| 140 | + if (!enumValue) { |
| 141 | + return; |
| 142 | + } |
| 143 | + return { kind: 'EnumValue', type, enumValue }; |
| 144 | + } |
| 145 | + // Otherwise if {type} is an Input Object type: |
| 146 | + if (isInputObjectType(type)) { |
| 147 | + // Let {inputFieldName} be the value of the second {Name}. |
| 148 | + // Return the input field of {type} named {inputFieldName}. |
| 149 | + const inputField = type.getFields()[memberName.value]; |
| 150 | + if (!inputField) { |
| 151 | + return; |
| 152 | + } |
| 153 | + return { kind: 'InputField', type, inputField }; |
| 154 | + } |
| 155 | + // Otherwise: |
| 156 | + // Assert {type} must be an Object or Interface type. |
| 157 | + if (!isObjectType(type) && !isInterfaceType(type)) { |
| 158 | + return; |
| 159 | + } |
| 160 | + // Let {fieldName} be the value of the second {Name}. |
| 161 | + // Return the field of {type} named {fieldName}. |
| 162 | + const field = type.getFields()[memberName.value]; |
| 163 | + if (!field) { |
| 164 | + return; |
| 165 | + } |
| 166 | + return { kind: 'Field', type, field }; |
| 167 | + } |
| 168 | + |
| 169 | + // SchemaCoordinate : Name . Name ( Name : ) |
| 170 | + // Assert {type} must be an Object or Interface type. |
| 171 | + if (!isObjectType(type) && !isInterfaceType(type)) { |
| 172 | + return; |
| 173 | + } |
| 174 | + // Let {fieldName} be the value of the second {Name}. |
| 175 | + // Let {field} be the field of {type} named {fieldName}. |
| 176 | + const field = type.getFields()[memberName.value]; |
| 177 | + // Assert {field} must exist. |
| 178 | + if (!field) { |
| 179 | + return; |
| 180 | + } |
| 181 | + // Let {fieldArgumentName} be the value of the third {Name}. |
| 182 | + // Return the argument of {field} named {fieldArgumentName}. |
| 183 | + const fieldArgument = field.args.find( |
| 184 | + (arg) => arg.name === argumentName.value, |
| 185 | + ); |
| 186 | + if (!fieldArgument) { |
| 187 | + return; |
| 188 | + } |
| 189 | + return { kind: 'FieldArgument', type, field, fieldArgument }; |
| 190 | +} |
0 commit comments