Skip to content

Added support for Scala 3 #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ body:
- '@graphql-codegen/typescript-vue-urql'
- '@graphql-codegen/import-types-preset'
- '@graphql-codegen/near-operation-file-preset'
- '@graphql-codegen/scala'
- '@graphql-codegen/scalajs'
- '@graphql-codegen/scala-common'
validations:
required: false
- type: textarea
Expand Down
2 changes: 2 additions & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
22
registry=http://localhost:4873
//localhost:4873/:_authToken="NDk2ZDQ1ZWFkODk3NWNjZmM0NDgwNmZmZTgxZGIyOWM6NjA0OGY4MmE4ODUwMGJhMWNiMGYwMmJkMjY="
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ CHANGELOG.md
.changeset/*.md
# Ignore all flow files cause it is just hard to maintain them
*.flow.js
*.scala
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ plugins:
`@graphql-codegen/java-apollo-android`
- [![npm version](https://badge.fury.io/js/%40graphql-codegen%2Ftypescript-nest.svg)](https://badge.fury.io/js/%40graphql-codegen%2Ftypescript-nest) -
`@graphql-codegen/typescript-nest`
- [![npm version](https://badge.fury.io/js/%40graphql-codegen%2Fscala.svg)](https://badge.fury.io/js/%40graphql-codegen%2Fscala) -
`@graphql-codegen/scala`
- [![npm version](https://badge.fury.io/js/%40graphql-codegen%2Fscalajs.svg)](https://badge.fury.io/js/%40graphql-codegen%2Fscalajs) -
`@graphql-codegen/scalajs`
- [![npm version](https://badge.fury.io/js/%40graphql-codegen%2Fscala-common.svg)](https://badge.fury.io/js/%40graphql-codegen%2Fscala-common) -
`@graphql-codegen/scala-common`

### Getting started with codegen

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"packages/plugins/c-sharp/*",
"packages/plugins/dart/*",
"packages/plugins/typescript/*",
"packages/plugins/scala/*",
"packages/plugins/other/*",
"packages/presets/*"
],
Expand Down Expand Up @@ -78,5 +79,8 @@
"yarn.lock": [
"npx yarn-deduplicate"
]
},
"dependencies": {
"@graphql-codegen/scala-resolvers": "^1.0.0"
}
}
1 change: 1 addition & 0 deletions packages/plugins/scala/common/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../../../jest.project')({ dirname: __dirname });
56 changes: 56 additions & 0 deletions packages/plugins/scala/common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@graphql-codegen/scala-common",
"version": "1.0.0",
"type": "module",
"description": "GraphQL Code Generator utils library for developing Scala plugins",
"repository": {
"type": "git",
"url": "https://github.com/dotansimha/graphql-code-generator-community.git",
"directory": "packages/plugins/scala/common"
},
"license": "MIT",
"engines": {
"node": ">= 16.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"require": {
"types": "./dist/typings/index.d.cts",
"default": "./dist/cjs/index.js"
},
"import": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
},
"default": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"./package.json": "./package.json"
},
"typings": "dist/typings/index.d.ts",
"scripts": {
"lint": "eslint **/*.ts"
},
"peerDependencies": {
"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"
},
"dependencies": {
"@graphql-codegen/plugin-helpers": "^3.0.0",
"@graphql-codegen/visitor-plugin-common": "2.13.8",
"auto-bind": "~4.0.0",
"min-indent": "1.0.1",
"tslib": "^2.8.1",
"unixify": "^1.0.0"
},
"publishConfig": {
"directory": "dist",
"access": "public"
},
"typescript": {
"definition": "dist/typings/index.d.ts"
}
}
113 changes: 113 additions & 0 deletions packages/plugins/scala/common/src/base-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { GraphQLSchema } from 'graphql';
import { Types } from '@graphql-codegen/plugin-helpers';
import { ScalaBaseVisitor } from './base-visitor';
import { ScalaPluginCommonRawConfig } from './config';

export type BasePluginFunction<TRawConfig extends ScalaPluginCommonRawConfig> = (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: TRawConfig,
info: { outputFile?: string },
) => Types.PluginOutput;

export const createBasePlugin = <
TRawConfig extends ScalaPluginCommonRawConfig,
TVisitor extends ScalaBaseVisitor<TRawConfig, any>,
>(
createVisitor: (
schema: GraphQLSchema,
config: TRawConfig,
outputFileInfo: { outputFile?: string },
) => TVisitor,
): BasePluginFunction<TRawConfig> => {
return (
schema: GraphQLSchema,
_: Types.DocumentFile[],
config: TRawConfig,
info: { outputFile?: string },
): Types.PluginOutput => {
if (!config.packageName && info.outputFile) {
const parts = info.outputFile.split('/');
parts.pop();

if (parts.length > 0) {
config = {
...config,
packageName: parts.join('.'),
};
}
}

const visitor = createVisitor(schema, config, { outputFile: info.outputFile });
const typeNames = Object.keys(schema.getTypeMap()).filter(
typeName => !typeName.startsWith('__'),
);

const header = [
visitor.getPackage(),
'',
visitor.getImports(),
'',
'getTypeClassDefinitions' in visitor ? (visitor as any).getTypeClassDefinitions() : '',
]
.filter(Boolean)
.join('\n');

const schemaScalarTypeNames = typeNames.filter(typeName => {
const type = schema.getTypeMap()[typeName];
return type.astNode?.kind === 'ScalarTypeDefinition';
});

const customScalarDefinitions: string[] = [];
if (config.scalars) {
Object.keys(config.scalars).forEach(scalarName => {
if (typeof config.scalars[scalarName] === 'string') {
const scalarType = config.scalars[scalarName];
customScalarDefinitions.push(`type ${scalarName} = ${scalarType}`);
}
});
}

const defaultScalars = visitor.generateCustomScalars
? visitor.generateCustomScalars([
...schemaScalarTypeNames,
...Object.keys(config.scalars || {}),
])
: [];

const contentFragments = typeNames.map(typeName => {
const type = schema.getTypeMap()[typeName];

if (
type.astNode?.kind === 'ScalarTypeDefinition' &&
config.scalars &&
typeof config.scalars[typeName] === 'string'
) {
return '';
}

const astNodeKind = type.astNode?.kind as string | undefined;
if (astNodeKind && astNodeKind in visitor) {
const handler = (visitor as any)[astNodeKind];
if (typeof handler === 'function') {
return handler.call(visitor, type.astNode);
}
}

return '';
});

const content = [
header,
...customScalarDefinitions,
...defaultScalars,
...contentFragments.filter(Boolean),
].join('\n\n');

return {
content,
prepend: [],
append: [],
};
};
};
Loading