|
| 1 | +import { |
| 2 | + ComposeNamedOutputType, |
| 3 | + ComposeOutputType, |
| 4 | + isTypeComposer, |
| 5 | + ObjectTypeComposer, |
| 6 | + SchemaComposer, |
| 7 | + unwrapOutputTC, |
| 8 | + upperFirst, |
| 9 | +} from 'graphql-compose'; |
| 10 | +import { |
| 11 | + AstDirNode, |
| 12 | + AstFileNode, |
| 13 | + AstRootNode, |
| 14 | + AstRootTypeNode, |
| 15 | + RootTypeNames, |
| 16 | +} from './directoryToAst'; |
| 17 | +import { FieldConfig } from './typeDefs'; |
| 18 | + |
| 19 | +interface VisitInfoData<TContext = any> { |
| 20 | + node: AstDirNode | AstFileNode | AstRootTypeNode; |
| 21 | + nodeParent: AstDirNode | AstRootTypeNode | AstRootNode; |
| 22 | + operation: RootTypeNames; |
| 23 | + fieldName: string; |
| 24 | + fieldPath: string[]; |
| 25 | + schemaComposer: SchemaComposer<TContext>; |
| 26 | +} |
| 27 | + |
| 28 | +export class VisitInfo<TContext = any> { |
| 29 | + node: AstDirNode | AstFileNode | AstRootTypeNode; |
| 30 | + /** Parent AST node from directoryToAst */ |
| 31 | + nodeParent: AstDirNode | AstRootTypeNode | AstRootNode; |
| 32 | + /** Brunch of schema under which is working visitor. Can be: query, mutation, subscription */ |
| 33 | + operation: RootTypeNames; |
| 34 | + /** Name of field for current FieldConfig */ |
| 35 | + fieldName: string; |
| 36 | + /** List of parent names starting from root */ |
| 37 | + fieldPath: string[]; |
| 38 | + /** Type registry */ |
| 39 | + schemaComposer: SchemaComposer<TContext>; |
| 40 | + |
| 41 | + constructor(data: VisitInfoData<TContext>) { |
| 42 | + this.node = data.node; |
| 43 | + this.operation = data.operation; |
| 44 | + this.nodeParent = data.nodeParent; |
| 45 | + this.fieldName = data.fieldName; |
| 46 | + this.fieldPath = data.fieldPath; |
| 47 | + this.schemaComposer = data.schemaComposer; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Check that this entrypoint belongs to Query |
| 52 | + */ |
| 53 | + isQuery(): boolean { |
| 54 | + return this.operation === 'query'; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Check that this entrypoint belongs to Mutation |
| 59 | + */ |
| 60 | + isMutation(): boolean { |
| 61 | + return this.operation === 'mutation'; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Check that this entrypoint belongs to Subscription |
| 66 | + */ |
| 67 | + isSubscription(): boolean { |
| 68 | + return this.operation === 'subscription'; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Return array of fieldNames. |
| 73 | + * Dotted names will be automatically splitted. |
| 74 | + * |
| 75 | + * @example |
| 76 | + * Assume: |
| 77 | + * name: 'ping' |
| 78 | + * path: ['query.storage', 'viewer', 'utils.debug'] |
| 79 | + * For empty options will be returned: |
| 80 | + * ['storage', 'viewer', 'utils', 'debug', 'ping'] |
| 81 | + * For `{ includeOperation: true }` will be returned: |
| 82 | + * ['query', 'storage', 'viewer', 'utils', 'debug', 'ping'] |
| 83 | + */ |
| 84 | + getFieldPathArray(opts?: { includeOperation?: boolean; omitFieldName?: boolean }): string[] { |
| 85 | + const res = [] as string[]; |
| 86 | + this.fieldPath.forEach((e) => { |
| 87 | + if (e.indexOf('.')) { |
| 88 | + res.push(...e.split('.').filter(Boolean)); |
| 89 | + } else { |
| 90 | + res.push(e); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + if (!opts?.omitFieldName) { |
| 95 | + res.push(this.fieldName); |
| 96 | + } |
| 97 | + |
| 98 | + return opts?.includeOperation ? res : res.slice(1); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Return dotted path for current field |
| 103 | + */ |
| 104 | + getFieldPathDotted(opts?: { includeOperation?: boolean; omitFieldName?: boolean }): string { |
| 105 | + return this.getFieldPathArray(opts).join('.'); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Return path as CamelCase string. |
| 110 | + * |
| 111 | + * Useful for getting type name according to path |
| 112 | + */ |
| 113 | + getFieldPathCamelCase(opts?: { includeOperation?: boolean; omitFieldName?: boolean }): string { |
| 114 | + return this.getFieldPathArray(opts) |
| 115 | + .map((s) => upperFirst(s)) |
| 116 | + .join(''); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get FieldConfig for file or dir. |
| 121 | + * This is mutable object and is shared between all calls. |
| 122 | + */ |
| 123 | + get fieldConfig(): FieldConfig { |
| 124 | + if (this.node.kind === 'file') { |
| 125 | + return this.node.code?.default as FieldConfig; |
| 126 | + } else if (this.node.kind === 'dir' || this.node.kind === 'rootType') { |
| 127 | + return this.node.namespaceConfig?.code?.default as FieldConfig; |
| 128 | + } |
| 129 | + throw new Error( |
| 130 | + `Cannot get fieldConfig. Node has some strange kind: ${(this.node as any).kind}` |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get TypeComposer instance for output type (object, scalar, enum, interface, union). |
| 136 | + * It's mutable object. |
| 137 | + */ |
| 138 | + getOutputAnyTC(): ComposeOutputType<TContext> { |
| 139 | + const fc = this.fieldConfig; |
| 140 | + const outputType = fc.type; |
| 141 | + if (!outputType) { |
| 142 | + throw new Error(`FieldConfig ${this.getFieldPathDotted()} does not have 'type' property`); |
| 143 | + } |
| 144 | + |
| 145 | + // if the type is of any kind of TypeComposer |
| 146 | + // then return it directly |
| 147 | + // or try to convert it to TypeComposer and save in FieldConfig as prepared type |
| 148 | + if (isTypeComposer(outputType)) { |
| 149 | + return outputType; |
| 150 | + } else { |
| 151 | + const outputTC = this.schemaComposer.typeMapper.convertOutputTypeDefinition( |
| 152 | + outputType, |
| 153 | + this.fieldName, |
| 154 | + this.nodeParent?.name |
| 155 | + ); |
| 156 | + |
| 157 | + if (!outputTC) { |
| 158 | + throw new Error( |
| 159 | + `FieldConfig ${this.getFieldPathDotted()} contains some strange value as output type` |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + fc.type = outputTC; |
| 164 | + return outputTC; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Check that output type is an object |
| 170 | + */ |
| 171 | + isOutputTypeIsObject(): boolean { |
| 172 | + return this.getOutputAnyTC() instanceof ObjectTypeComposer; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Get TypeComposer instance for output type (object, scalar, enum, interface, union). |
| 177 | + * It's mutable object. |
| 178 | + */ |
| 179 | + getOutputUnwrappedTC(): ComposeNamedOutputType<TContext> { |
| 180 | + return unwrapOutputTC(this.getOutputAnyTC()); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Get TypeComposer instance for output type (object, scalar, enum, interface, union). |
| 185 | + * It's mutable object. |
| 186 | + */ |
| 187 | + getOutputUnwrappedOTC(): ObjectTypeComposer { |
| 188 | + const tc = unwrapOutputTC(this.getOutputAnyTC()); |
| 189 | + |
| 190 | + if (!(tc instanceof ObjectTypeComposer)) { |
| 191 | + throw new Error( |
| 192 | + `FieldConfig ${this.getFieldPathDotted()} has non-Object output type. Use 'isOutputTypeIsObject()' before for avoiding this error.` |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + return tc; |
| 197 | + } |
| 198 | + |
| 199 | + toString(): string { |
| 200 | + return `VisitInfo(${this.getFieldPathDotted({ includeOperation: true })})`; |
| 201 | + } |
| 202 | + |
| 203 | + toJSON(): string { |
| 204 | + return this.toString(); |
| 205 | + } |
| 206 | +} |
0 commit comments