UNPKG

3.93 kBJavaScriptView Raw
1import { Imports } from './imports.js';
2import { BaseVisitor, getBaseTypeNode, buildScalars } from '@graphql-codegen/visitor-plugin-common';
3import { getBaseType } from '@graphql-codegen/plugin-helpers';
4import { JAVA_SCALARS } from '@graphql-codegen/java-common';
5import { isScalarType, isInputObjectType, Kind, isNonNullType, isListType, GraphQLObjectType, } from 'graphql';
6export const SCALAR_TO_WRITER_METHOD = {
7 ID: 'writeString',
8 String: 'writeString',
9 Int: 'writeInt',
10 Boolean: 'writeBoolean',
11 Float: 'writeDouble',
12};
13function isTypeNode(type) {
14 return type && !!type.kind;
15}
16export class BaseJavaVisitor extends BaseVisitor {
17 constructor(_schema, rawConfig, additionalConfig) {
18 super(rawConfig, {
19 ...additionalConfig,
20 scalars: buildScalars(_schema, { ID: 'String' }, JAVA_SCALARS),
21 });
22 this._schema = _schema;
23 this._imports = new Set();
24 }
25 getPackage() {
26 return '';
27 }
28 additionalContent() {
29 return '';
30 }
31 getImports() {
32 return Array.from(this._imports).map(imp => `import ${imp};`);
33 }
34 getImplementingTypes(node) {
35 const allTypesMap = this._schema.getTypeMap();
36 const implementingTypes = [];
37 for (const graphqlType of Object.values(allTypesMap)) {
38 if (graphqlType instanceof GraphQLObjectType) {
39 const allInterfaces = graphqlType.getInterfaces();
40 if (allInterfaces.find(int => int.name === node.name)) {
41 implementingTypes.push(graphqlType.name);
42 }
43 }
44 }
45 return implementingTypes;
46 }
47 transformType(type) {
48 let schemaType;
49 let isNonNull;
50 if (isTypeNode(type)) {
51 const baseTypeNode = getBaseTypeNode(type);
52 schemaType = this._schema.getType(baseTypeNode.name.value);
53 isNonNull = type.kind === Kind.NON_NULL_TYPE;
54 }
55 else {
56 schemaType = this._schema.getType(getBaseType(type).name);
57 isNonNull = isNonNullType(type);
58 }
59 const javaType = this.getJavaClass(schemaType);
60 const annotation = isNonNull ? 'Nonnull' : 'Nullable';
61 const typeToUse = isTypeNode(type)
62 ? this.getListTypeNodeWrapped(javaType, type)
63 : this.getListTypeWrapped(javaType, type);
64 return {
65 baseType: schemaType.name,
66 javaType,
67 isNonNull,
68 annotation,
69 typeToUse,
70 };
71 }
72 // Replaces a GraphQL type with a Java class
73 getJavaClass(schemaType) {
74 let typeToUse = schemaType.name;
75 if (isScalarType(schemaType)) {
76 const scalar = this.scalars[schemaType.name] || 'Object';
77 if (Imports[scalar]) {
78 this._imports.add(Imports[scalar]);
79 }
80 typeToUse = scalar;
81 }
82 else if (isInputObjectType(schemaType)) {
83 // Make sure to import it if it's in use
84 this._imports.add(`${this.config.typePackage}.${schemaType.name}`);
85 }
86 return typeToUse;
87 }
88 getListTypeWrapped(toWrap, type) {
89 if (isNonNullType(type)) {
90 return this.getListTypeWrapped(toWrap, type.ofType);
91 }
92 if (isListType(type)) {
93 const child = this.getListTypeWrapped(toWrap, type.ofType);
94 this._imports.add(Imports.List);
95 return `List<${child}>`;
96 }
97 return toWrap;
98 }
99 getListTypeNodeWrapped(toWrap, type) {
100 if (type.kind === Kind.NON_NULL_TYPE) {
101 return this.getListTypeNodeWrapped(toWrap, type.type);
102 }
103 if (type.kind === Kind.LIST_TYPE) {
104 const child = this.getListTypeNodeWrapped(toWrap, type.type);
105 this._imports.add(Imports.List);
106 return `List<${child}>`;
107 }
108 return toWrap;
109 }
110}