1 | import { isScalarType } from 'graphql';
|
2 | import { BaseJavaVisitor } from './base-java-visitor.js';
|
3 | import { indentMultiline } from '@graphql-codegen/visitor-plugin-common';
|
4 | import { Imports } from './imports.js';
|
5 | import { JavaDeclarationBlock } from '@graphql-codegen/java-common';
|
6 | const filteredScalars = ['String', 'Float', 'Int', 'Boolean'];
|
7 | export class CustomTypeClassVisitor extends BaseJavaVisitor {
|
8 | constructor(schema, rawConfig) {
|
9 | super(schema, rawConfig, {
|
10 | typePackage: rawConfig.typePackage || 'type',
|
11 | });
|
12 | }
|
13 | extract(name) {
|
14 | const lastIndex = name.lastIndexOf('.');
|
15 | if (lastIndex === -1) {
|
16 | return {
|
17 | className: name,
|
18 | importFrom: Imports[name] || null,
|
19 | };
|
20 | }
|
21 | return {
|
22 | className: name.substring(lastIndex + 1),
|
23 | importFrom: name,
|
24 | };
|
25 | }
|
26 | additionalContent() {
|
27 | this._imports.add(Imports.ScalarType);
|
28 | this._imports.add(Imports.Class);
|
29 | this._imports.add(Imports.Override);
|
30 | this._imports.add(Imports.Generated);
|
31 | const allTypes = this._schema.getTypeMap();
|
32 | const enumValues = Object.keys(allTypes)
|
33 | .filter(t => isScalarType(allTypes[t]) && !filteredScalars.includes(t))
|
34 | .map(t => allTypes[t])
|
35 | .map(scalarType => {
|
36 | const uppercaseName = scalarType.name.toUpperCase();
|
37 | const javaType = this.extract(this.scalars[scalarType.name] || 'String');
|
38 | if (javaType.importFrom) {
|
39 | this._imports.add(javaType.importFrom);
|
40 | }
|
41 | return indentMultiline(`${uppercaseName} {
|
42 | @Override
|
43 | public String typeName() {
|
44 | return "${scalarType.name}";
|
45 | }
|
46 |
|
47 | @Override
|
48 | public Class javaType() {
|
49 | return ${javaType.className}.class;
|
50 | }
|
51 | }`);
|
52 | })
|
53 | .join(',\n\n');
|
54 | return new JavaDeclarationBlock()
|
55 | .annotate([`Generated("Apollo GraphQL")`])
|
56 | .access('public')
|
57 | .asKind('enum')
|
58 | .withName('CustomType')
|
59 | .implements(['ScalarType'])
|
60 | .withBlock(enumValues).string;
|
61 | }
|
62 | getPackage() {
|
63 | return this.config.typePackage;
|
64 | }
|
65 | }
|