Skip to content

Commit 9823c2d

Browse files
NadenNaden
authored andcommitted
Added support for Scala
1 parent be2864e commit 9823c2d

21 files changed

Lines changed: 2702 additions & 1 deletion
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../../../jest.project')({ dirname: __dirname });
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "@graphql-codegen/scala-common",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"description": "GraphQL Code Generator plugin for generating Scala code based on a GraphQL schema",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/dotansimha/graphql-code-generator-community.git",
9+
"directory": "packages/plugins/scala/common"
10+
},
11+
"license": "MIT",
12+
"engines": {
13+
"node": ">= 16.0.0"
14+
},
15+
"main": "dist/index.js",
16+
"module": "dist/index.mjs",
17+
"exports": {
18+
".": {
19+
"require": "./dist/index.js",
20+
"import": "./dist/index.mjs"
21+
},
22+
"./package.json": "./package.json"
23+
},
24+
"typings": "dist/index.d.ts",
25+
"scripts": {
26+
"lint": "eslint **/*.ts",
27+
"prepack": "bob prepack"
28+
},
29+
"peerDependencies": {
30+
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
31+
},
32+
"dependencies": {
33+
"@graphql-codegen/plugin-helpers": "^5.0.0",
34+
"@graphql-codegen/visitor-plugin-common": "^5.0.0",
35+
"auto-bind": "~4.0.0",
36+
"graphql": "^16.7.1"
37+
},
38+
"devDependencies": {
39+
"@graphql-codegen/testing": "1.18.3"
40+
},
41+
"publishConfig": {
42+
"directory": "dist",
43+
"access": "public"
44+
},
45+
"typescript": {
46+
"definition": "dist/index.d.ts"
47+
}
48+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { GraphQLSchema } from 'graphql';
2+
import { Types } from '@graphql-codegen/plugin-helpers';
3+
import { ScalaBaseVisitor } from './base-visitor';
4+
import { ScalaPluginCommonRawConfig } from './config';
5+
6+
export type BasePluginFunction<TRawConfig extends ScalaPluginCommonRawConfig> = (
7+
schema: GraphQLSchema,
8+
documents: Types.DocumentFile[],
9+
config: TRawConfig,
10+
info: { outputFile?: string },
11+
) => Types.PluginOutput;
12+
13+
export const createBasePlugin = <
14+
TRawConfig extends ScalaPluginCommonRawConfig,
15+
TVisitor extends ScalaBaseVisitor<TRawConfig, any>,
16+
>(
17+
createVisitor: (
18+
schema: GraphQLSchema,
19+
config: TRawConfig,
20+
outputFileInfo: { outputFile?: string },
21+
) => TVisitor,
22+
): BasePluginFunction<TRawConfig> => {
23+
return (
24+
schema: GraphQLSchema,
25+
_: Types.DocumentFile[],
26+
config: TRawConfig,
27+
info: { outputFile?: string },
28+
): Types.PluginOutput => {
29+
if (!config.packageName && info.outputFile) {
30+
const parts = info.outputFile.split('/');
31+
parts.pop();
32+
33+
if (parts.length > 0) {
34+
config = {
35+
...config,
36+
packageName: parts.join('.'),
37+
};
38+
}
39+
}
40+
41+
const visitor = createVisitor(schema, config, { outputFile: info.outputFile });
42+
const typeNames = Object.keys(schema.getTypeMap()).filter(
43+
typeName => !typeName.startsWith('__'),
44+
);
45+
46+
const header = [
47+
visitor.getPackage(),
48+
'',
49+
visitor.getImports(),
50+
'',
51+
visitor.getTypeClassDefinitions && visitor.getTypeClassDefinitions(),
52+
]
53+
.filter(Boolean)
54+
.join('\n');
55+
56+
const schemaScalarTypeNames = typeNames.filter(typeName => {
57+
const type = schema.getTypeMap()[typeName];
58+
return type.astNode?.kind === 'ScalarTypeDefinition';
59+
});
60+
61+
const customScalarDefinitions: string[] = [];
62+
if (config.scalars) {
63+
for (const scalarName in config.scalars) {
64+
if (typeof config.scalars[scalarName] === 'string') {
65+
const scalarType = config.scalars[scalarName];
66+
customScalarDefinitions.push(`type ${scalarName} = ${scalarType}`);
67+
}
68+
}
69+
}
70+
71+
const defaultScalars = visitor.generateCustomScalars
72+
? visitor.generateCustomScalars([
73+
...schemaScalarTypeNames,
74+
...Object.keys(config.scalars || {}),
75+
])
76+
: [];
77+
78+
const contentFragments = typeNames.map(typeName => {
79+
const type = schema.getTypeMap()[typeName];
80+
81+
if (
82+
type.astNode?.kind === 'ScalarTypeDefinition' &&
83+
config.scalars &&
84+
typeof config.scalars[typeName] === 'string'
85+
) {
86+
return '';
87+
}
88+
89+
if (visitor[type.astNode?.kind]) {
90+
return visitor[type.astNode.kind](type.astNode);
91+
}
92+
return '';
93+
});
94+
95+
const content = [
96+
header,
97+
...customScalarDefinitions,
98+
...defaultScalars,
99+
...contentFragments.filter(Boolean),
100+
].join('\n\n');
101+
102+
return {
103+
content,
104+
prepend: [],
105+
append: [],
106+
};
107+
};
108+
};

0 commit comments

Comments
 (0)