UNPKG

@graphql-codegen/java-apollo-android

Version:

GraphQL Code Generator plugin for generating Java classes for Apollo-Android

115 lines (114 loc) 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseJavaVisitor = exports.SCALAR_TO_WRITER_METHOD = void 0; const graphql_1 = require("graphql"); const java_common_1 = require("@graphql-codegen/java-common"); const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers"); const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); const imports_js_1 = require("./imports.js"); exports.SCALAR_TO_WRITER_METHOD = { ID: 'writeString', String: 'writeString', Int: 'writeInt', Boolean: 'writeBoolean', Float: 'writeDouble', }; function isTypeNode(type) { return type && !!type.kind; } class BaseJavaVisitor extends visitor_plugin_common_1.BaseVisitor { constructor(_schema, rawConfig, additionalConfig) { super(rawConfig, { ...additionalConfig, scalars: (0, visitor_plugin_common_1.buildScalars)(_schema, { ID: 'String' }, java_common_1.JAVA_SCALARS), }); this._schema = _schema; this._imports = new Set(); } getPackage() { return ''; } additionalContent() { return ''; } getImports() { return Array.from(this._imports).map(imp => `import ${imp};`); } getImplementingTypes(node) { const allTypesMap = this._schema.getTypeMap(); const implementingTypes = []; for (const graphqlType of Object.values(allTypesMap)) { if (graphqlType instanceof graphql_1.GraphQLObjectType) { const allInterfaces = graphqlType.getInterfaces(); if (allInterfaces.find(int => int.name === node.name)) { implementingTypes.push(graphqlType.name); } } } return implementingTypes; } transformType(type) { let schemaType; let isNonNull; if (isTypeNode(type)) { const baseTypeNode = (0, visitor_plugin_common_1.getBaseTypeNode)(type); schemaType = this._schema.getType(baseTypeNode.name.value); isNonNull = type.kind === graphql_1.Kind.NON_NULL_TYPE; } else { schemaType = this._schema.getType((0, plugin_helpers_1.getBaseType)(type).name); isNonNull = (0, graphql_1.isNonNullType)(type); } const javaType = this.getJavaClass(schemaType); const annotation = isNonNull ? 'Nonnull' : 'Nullable'; const typeToUse = isTypeNode(type) ? this.getListTypeNodeWrapped(javaType, type) : this.getListTypeWrapped(javaType, type); return { baseType: schemaType.name, javaType, isNonNull, annotation, typeToUse, }; } // Replaces a GraphQL type with a Java class getJavaClass(schemaType) { let typeToUse = schemaType.name; if ((0, graphql_1.isScalarType)(schemaType)) { const scalar = this.scalars[schemaType.name] || 'Object'; if (imports_js_1.Imports[scalar]) { this._imports.add(imports_js_1.Imports[scalar]); } typeToUse = scalar; } else if ((0, graphql_1.isInputObjectType)(schemaType)) { // Make sure to import it if it's in use this._imports.add(`${this.config.typePackage}.${schemaType.name}`); } return typeToUse; } getListTypeWrapped(toWrap, type) { if ((0, graphql_1.isNonNullType)(type)) { return this.getListTypeWrapped(toWrap, type.ofType); } if ((0, graphql_1.isListType)(type)) { const child = this.getListTypeWrapped(toWrap, type.ofType); this._imports.add(imports_js_1.Imports.List); return `List<${child}>`; } return toWrap; } getListTypeNodeWrapped(toWrap, type) { if (type.kind === graphql_1.Kind.NON_NULL_TYPE) { return this.getListTypeNodeWrapped(toWrap, type.type); } if (type.kind === graphql_1.Kind.LIST_TYPE) { const child = this.getListTypeNodeWrapped(toWrap, type.type); this._imports.add(imports_js_1.Imports.List); return `List<${child}>`; } return toWrap; } } exports.BaseJavaVisitor = BaseJavaVisitor;