Skip to content
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

Graphql codegen typescript effect #701

Open
wants to merge 6 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
5 changes: 5 additions & 0 deletions .changeset/eight-ants-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-effect': minor
---

Introduces a graphql-codegen plugin for generating a client SDK using Effect
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"packages/plugins/other/*",
"packages/presets/*"
],
"packageManager": "yarn@1.22.22",
"scripts": {
"build": "bob build",
"ci:lint": "eslint --ext .ts . --output-file eslint_report.json --format json",
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/typescript/effect/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../../../jest.project')({ dirname: __dirname });
67 changes: 67 additions & 0 deletions packages/plugins/typescript/effect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "@graphql-codegen/typescript-effect",
"version": "0.0.1",
"type": "module",
"description": "GraphQL Code Generator plugin for generating Effect Platform HTTP Client requests for GraphQL operations",
"repository": {
"type": "git",
"url": "https://github.com/dotansimha/graphql-code-generator-community.git",
"directory": "packages/plugins/typescript/effect"
},
"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",
"test": "jest --no-watchman --config ../../../../jest.config.js"
},
"peerDependencies": {
"@effect/platform": "~0.48.12",
"effect": "^2.4.9",
"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",
"graphql-tag": "^2.0.0"
},
"dependencies": {
"@graphql-codegen/plugin-helpers": "^3.0.0",
"@graphql-codegen/visitor-plugin-common": "2.13.1",
"auto-bind": "~4.0.0",
"tslib": "~2.6.0"
},
"devDependencies": {
"@effect/platform": "~0.48.12",
"@effect/platform-node": "~0.45.14",
"@effect/schema": "^0.64.8",
"@graphql-codegen/testing": "1.18.0",
"@graphql-tools/schema": "10.0.3",
"effect": "2.4.9",
"fast-check": "^3.16.0"
},
"publishConfig": {
"directory": "dist",
"access": "public"
},
"typescript": {
"definition": "dist/typings/index.d.ts"
}
}
44 changes: 44 additions & 0 deletions packages/plugins/typescript/effect/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common';

/**
* @description This plugin generates a fully-typed, ready-to-use SDK written with [`effect`](https://effect.website).
*/

export interface RawEffectPluginConfig extends RawClientSideBasePluginConfig {
/**
* @description Allows you to override the output mode of the `typescript-effect` codegen plugin. To generate the GraphQL client in a separate file, use the `client-only` and `operations-only` modes, respectively.
* @default 'mixed'
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/client.ts': {
* plugins: ['typescript-effect'],
* config: {
* mode: 'client-only'
* },
* },
* 'path/to/sdk.ts': {
* plugins: ['typescript', 'typescript-operations', 'typescript-effect'],
* config: {
* mode: 'operations-only',
* relativeClientImportPath: './client.js',
* },
* },
* },
* };
* export default config;
* ```
*/
mode?: 'client-only' | 'operations-only' | 'mixed';

/**
* @description Specifies the relative import path of the graphql client file. A required config when the mode is set to `operations-only`, ignored in all other cases.
* @default undefined
*/
relativeClientImportPath?: string;
}
60 changes: 60 additions & 0 deletions packages/plugins/typescript/effect/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { concatAST, type FragmentDefinitionNode, type GraphQLSchema, Kind } from 'graphql';
import {
oldVisit,
type PluginFunction,
type PluginValidateFn,
type Types,
} from '@graphql-codegen/plugin-helpers';
import type { LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
import type { RawEffectPluginConfig } from './config.js';
import { EffectVisitor } from './visitor.js';

export const plugin: PluginFunction<RawEffectPluginConfig> = (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: RawEffectPluginConfig,
) => {
if (config.mode === 'client-only') return EffectVisitor.clientContent();

const allAst = concatAST(documents.map(v => v.document));
const allFragments: LoadedFragment[] = [
...(
allAst.definitions.filter(
d => d.kind === Kind.FRAGMENT_DEFINITION,
) as FragmentDefinitionNode[]
).map(fragmentDef => ({
node: fragmentDef,
name: fragmentDef.name.value,
onType: fragmentDef.typeCondition.name.value,
isExternal: false,
})),
...(config.externalFragments || []),
];
const visitor = new EffectVisitor(schema, allFragments, config);
const visitorResult = oldVisit(allAst, { leave: visitor });

return {
prepend: visitor.getImports(),
content: [
visitor.fragments,
...visitorResult.definitions.filter(t => typeof t === 'string'),
visitor.sdkContent,
].join('\n'),
};
};

export const validate: PluginValidateFn<any> = async (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: RawEffectPluginConfig,
outputFile: string,
) => {
if (
config.mode === 'operations-only' &&
(!config.relativeClientImportPath || config.relativeClientImportPath.length === 0)
) {
throw new Error(
`Plugin "typescript-effect" requires the "relativeClientImportPath" configuration option to be a non-empty string when "mode" is set to "operations-only"!`,
);
}
};
Loading