|
1 |
| -import { getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers'; |
2 |
| -import { GraphQLObjectType, GraphQLSchema, visit } from 'graphql'; |
| 1 | +import { transformComment } from '@graphql-codegen/visitor-plugin-common'; |
| 2 | +import { |
| 3 | + GraphQLSchema, |
| 4 | + ASTNode, |
| 5 | + FieldDefinitionNode, |
| 6 | + Kind, |
| 7 | + ObjectTypeDefinitionNode, |
| 8 | + TypeNode, |
| 9 | + InputObjectTypeDefinitionNode, |
| 10 | + InputValueDefinitionNode, |
| 11 | +} from 'graphql'; |
3 | 12 | import { Config } from './config.js';
|
4 |
| -import { createTypeInfoVisitor } from './visitor.js'; |
| 13 | + |
| 14 | +// The fork of https://github.com/dotansimha/graphql-code-generator/blob/e1dc75f3c598bf7f83138ca533619716fc73f823/packages/plugins/typescript/resolvers/src/visitor.ts#L85-L91 |
| 15 | +function clearOptional(str: string): string { |
| 16 | + if (str.startsWith('Maybe')) { |
| 17 | + return str.replace(/Maybe<(.*?)>$/u, '$1'); |
| 18 | + } |
| 19 | + return str; |
| 20 | +} |
| 21 | + |
| 22 | +// The fork of https://github.com/dotansimha/graphql-code-generator/blob/ba84a3a2758d94dac27fcfbb1bafdf3ed7c32929/packages/plugins/other/visitor-plugin-common/src/base-visitor.ts#L422 |
| 23 | +function convertName(node: ASTNode | string, config: Config): string { |
| 24 | + let convertedName = ''; |
| 25 | + convertedName += config.typesPrefix; |
| 26 | + convertedName += config.convert(node); |
| 27 | + convertedName += config.typesSuffix; |
| 28 | + return convertedName; |
| 29 | +} |
| 30 | + |
| 31 | +function isTypeBasedOnUserDefinedType(node: TypeNode, userDefinedTypeNames: string[]): boolean { |
| 32 | + if (node.kind === Kind.NON_NULL_TYPE) { |
| 33 | + return isTypeBasedOnUserDefinedType(node.type, userDefinedTypeNames); |
| 34 | + } else if (node.kind === Kind.LIST_TYPE) { |
| 35 | + return isTypeBasedOnUserDefinedType(node.type, userDefinedTypeNames); |
| 36 | + } else { |
| 37 | + return userDefinedTypeNames.includes(node.name.value); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function parseTypeNode(node: TypeNode, config: Config): string { |
| 42 | + if (node.kind === Kind.NON_NULL_TYPE) { |
| 43 | + return clearOptional(parseTypeNode(node.type, config)); |
| 44 | + } else if (node.kind === Kind.LIST_TYPE) { |
| 45 | + return `Maybe<${parseTypeNode(node.type, config)}[]>`; |
| 46 | + } else { |
| 47 | + return `Maybe<Optional${convertName(node.name.value, config)}>`; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function parseFieldOrInputValueDefinition( |
| 52 | + node: FieldDefinitionNode | InputValueDefinitionNode, |
| 53 | + objectTypeName: string, |
| 54 | + config: Config, |
| 55 | + userDefinedTypeNames: string[], |
| 56 | +): { typeString: string; comment: string | undefined } { |
| 57 | + const comment = node.description ? transformComment(node.description) : undefined; |
| 58 | + if (isTypeBasedOnUserDefinedType(node.type, userDefinedTypeNames)) { |
| 59 | + return { typeString: `${parseTypeNode(node.type, config)} | undefined`, comment }; |
| 60 | + } else { |
| 61 | + return { typeString: `${objectTypeName}['${node.name.value}'] | undefined`, comment }; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function parseObjectTypeOrInputObjectTypeDefinition( |
| 66 | + node: ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode, |
| 67 | + config: Config, |
| 68 | + userDefinedTypeNames: string[], |
| 69 | +): TypeInfo { |
| 70 | + const objectTypeName = convertName(node.name.value, config); |
| 71 | + const comment = node.description ? transformComment(node.description) : undefined; |
| 72 | + return { |
| 73 | + name: objectTypeName, |
| 74 | + fields: [ |
| 75 | + // TODO: support __is<AbstractType> (__is<InterfaceType>, __is<UnionType>) |
| 76 | + ...(!config.skipTypename ? [{ name: '__typename', typeString: `'${objectTypeName}'` }] : []), |
| 77 | + ...(node.fields ?? []).map((field) => ({ |
| 78 | + name: field.name.value, |
| 79 | + ...parseFieldOrInputValueDefinition(field, objectTypeName, config, userDefinedTypeNames), |
| 80 | + })), |
| 81 | + ], |
| 82 | + comment, |
| 83 | + }; |
| 84 | +} |
5 | 85 |
|
6 | 86 | type FieldInfo = { name: string; typeString: string; comment?: string | undefined };
|
7 | 87 | export type TypeInfo = { name: string; fields: FieldInfo[]; comment?: string | undefined };
|
8 | 88 |
|
9 | 89 | export function getTypeInfos(config: Config, schema: GraphQLSchema): TypeInfo[] {
|
10 |
| - const userDefinedTypeNames = Object.values(schema.getTypeMap()) |
11 |
| - // Ignore introspectionTypes |
12 |
| - // ref: https://github.com/graphql/graphql-js/blob/b12dcffe83098922dcc6c0ec94eb6fc032bd9772/src/type/introspection.ts#L552-L559 |
13 |
| - .filter((type) => type instanceof GraphQLObjectType && !type.name.startsWith('__')) |
14 |
| - .map((type) => type.name); |
| 90 | + const types = Object.values(schema.getTypeMap()); |
| 91 | + |
| 92 | + const objectTypeOrInputObjectTypeDefinitions = types |
| 93 | + .map((type) => type.astNode) |
| 94 | + .filter((node): node is ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode => { |
| 95 | + if (!node) return false; |
| 96 | + return node.kind === Kind.OBJECT_TYPE_DEFINITION || node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION; |
| 97 | + }); |
15 | 98 |
|
16 |
| - const visitor = createTypeInfoVisitor(config, userDefinedTypeNames); |
17 |
| - const ast = getCachedDocumentNodeFromSchema(schema); |
| 99 | + const userDefinedTypeNames = objectTypeOrInputObjectTypeDefinitions.map((type) => type.name.value); |
18 | 100 |
|
19 |
| - visit(ast, visitor); |
| 101 | + const typeInfos = objectTypeOrInputObjectTypeDefinitions.map((node) => |
| 102 | + parseObjectTypeOrInputObjectTypeDefinition(node, config, userDefinedTypeNames), |
| 103 | + ); |
20 | 104 |
|
21 |
| - return visitor.getTypeInfos(); |
| 105 | + return typeInfos; |
22 | 106 | }
|
0 commit comments