Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat(nuxt): graphql default scalers and prepend ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Feb 7, 2024
1 parent aa19235 commit 6f7bca1
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 67 deletions.
1 change: 0 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default antfu(
'**/templates.json',
'packages-core/pergel/nuxt.js',
'**/pergel/**',
'**/generated/**',
],
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export const schema = `scalar DateTime
// THIS FILE IS GENERATED, DO NOT EDIT!
/* eslint-disable eslint-comments/no-unlimited-disable */
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* pergel - oku-ui.com/pergel */
export const schema = `scalar DateTime
type Query {
book(id: ID!): Book!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// THIS FILE IS GENERATED, DO NOT EDIT!
/* eslint-disable eslint-comments/no-unlimited-disable */
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* pergel - oku-ui.com/pergel */
import type { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
import type { GraphqlYogaContext } from 'pergel/test/types';
export type Maybe<T> = T | null;
Expand All @@ -15,7 +21,7 @@ export type Scalars = {
Boolean: { input: boolean; output: boolean; }
Int: { input: number; output: number; }
Float: { input: number; output: number; }
DateTime: { input: any; output: any; }
DateTime: { input: Date | string; output: Date | string; }
};

export type Query = {
Expand Down Expand Up @@ -56,11 +62,10 @@ export type SearchInput = {

export type SearchResult = User | Book;

export enum RoleStatus {
User = 'user',
Admin = 'admin',
Superadmin = 'superadmin'
}
export type RoleStatus =
| 'user'
| 'admin'
| 'superadmin';

export type User = {
__typename?: 'User';
Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt/src/runtime/core/setupModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async function initModules(nuxt: Nuxt) {
projectNameCamelCase: camelCase(projectName, { normalize: true }),
projectNameCamelCaseWithPergel: camelCase(`pergel-${projectName}`, { normalize: true }),
generatorFunctionName: (text: string) => camelCase(`${projectName}-${text}`),
generatorFolderName: (moduleName, projectName) => `${camelCase(moduleName)}-${camelCase(projectName)}`,
folderName: `${camelCase(moduleName)}-${camelCase(projectName)}`,
importPath: join(projectName, moduleName),
buildDir: join(nuxt.options.buildDir, 'pergel', projectName, moduleName),
Expand Down Expand Up @@ -271,6 +272,7 @@ export async function setupModules(data: {
projectNameCamelCase: camelCase(projectName, { normalize: true }),
projectNameCamelCaseWithPergel: camelCase(`pergel-${projectName}`, { normalize: true }),
generatorFunctionName: (text: string) => camelCase(`${projectName}-${text}`),
generatorFolderName: (moduleName, projectName) => `${camelCase(moduleName)}-${camelCase(projectName)}`,
folderName: `${camelCase(moduleName)}-${camelCase(projectName)}`,
importPath: join(projectName, moduleName),
buildDir: join(data.nuxt.options.buildDir, 'pergel', projectName, moduleName),
Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt/src/runtime/core/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface ResolvedPergelModuleOptions {
*/
folderName: string

generatorFolderName: (moduleName: PergelModuleNames, projectName: string) => string

/**
* @default
* projectname: TEst
Expand Down
23 changes: 22 additions & 1 deletion packages/nuxt/src/runtime/modules/graphqlYoga/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,18 @@ export interface GraphQLYogaConfig extends PergelModuleOptions {

/**
* Codegen config
* @default undefined
* @default
scalars: {
DateTime: DateTimeResolver.extensions.codegenScalarType as any,
UUID: UUIDResolver.extensions.codegenScalarType as any,
JSON: JSONResolver.extensions.codegenScalarType as any,
JSONObject: JSONResolver.extensions.codegenScalarType as any,
NonEmptyString: NonEmptyStringResolver.extensions.codegenScalarType as any,
Currency: CurrencyResolver.extensions.codegenScalarType as any,
},
enumsAsTypes: true,
useTypeImports: true,
strictScalars: true,
*/
config?: (args: {
dir: {
Expand All @@ -160,6 +171,11 @@ export interface GraphQLYogaConfig extends PergelModuleOptions {
* .nuxt/pergel/${projectName}/graphqlYoga
*/
nuxtModule: string

/**
* #projectName/server/drizzle/schema
*/
drizzleShemas: (key: string) => string
}
}) => CodegenServerConfig
}
Expand Down Expand Up @@ -261,6 +277,11 @@ export interface ResolvedGraphQLYogaConfig extends GraphQLYogaConfig, ResolvedPe
* .nuxt/pergel/${projectName}/graphqlYoga
*/
nuxtModule: string

/**
* #projectName/server/drizzle/schema
*/
drizzleShemas: (key: string) => string
}
}) => CodegenServerConfig
}
Expand Down
71 changes: 64 additions & 7 deletions packages/nuxt/src/runtime/modules/graphqlYoga/utils/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { writeFileSync } from 'node:fs'
import type { LoadSchemaOptions, LoadTypedefsOptions, UnnormalizedTypeDefPointer } from '@graphql-tools/load'
import { loadDocumentsSync, loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
import type { Types } from '@graphql-codegen/plugin-helpers'
import type { PluginFunction, Types } from '@graphql-codegen/plugin-helpers'
import type { GraphQLSchema } from 'graphql'
import { codegen } from '@graphql-codegen/core'
import { parse, printSchema } from 'graphql'
Expand All @@ -15,7 +15,30 @@ import type { Source } from '@graphql-tools/utils'
import * as typescriptPlugin from '@graphql-codegen/typescript'
import * as typescriptResolversPlugin from '@graphql-codegen/typescript-resolvers'
import * as urqlIntrospectionPlugin from '@graphql-codegen/urql-introspection'

import consola from 'consola'
import defu from 'defu'
import {
CurrencyResolver,
DateTimeResolver,
JSONResolver,
NonEmptyStringResolver,
UUIDResolver,
} from 'graphql-scalars'

const pluginContent: PluginFunction<any> = (_schema, _documents, _config, _info) => {
return {
prepend: [
'// THIS FILE IS GENERATED, DO NOT EDIT!',
'/* eslint-disable eslint-comments/no-unlimited-disable */',
'/* tslint:disable */',
'/* eslint-disable */',
'/* prettier-ignore */',
'/* pergel - oku-ui.com/pergel */',
],
content: '',
}
}

export type CodegenClientConfig = TypeScriptPluginConfig & TypeScriptDocumentsPluginConfig & TypeScriptTypedDocumentNodesConfig

Expand Down Expand Up @@ -77,20 +100,37 @@ async function loadDocumentsFiles(pointerOrPointers: UnnormalizedTypeDefPointer

async function typescriptResolvers(
schema: GraphQLSchema,
config: CodegenServerConfig = { },
config: CodegenServerConfig,
) {
const mergeConfig = defu(config, {
scalars: {
DateTime: DateTimeResolver.extensions.codegenScalarType as any,
UUID: UUIDResolver.extensions.codegenScalarType as any,
JSON: JSONResolver.extensions.codegenScalarType as any,
JSONObject: JSONResolver.extensions.codegenScalarType as any,
NonEmptyString: NonEmptyStringResolver.extensions.codegenScalarType as any,
Currency: CurrencyResolver.extensions.codegenScalarType as any,
},
enumsAsTypes: true,
useTypeImports: true,
strictScalars: true,
} as CodegenServerConfig)
// See https://www.graphql-code-generator.com/docs/getting-started/programmatic-usage for more details
const res = await codegen({
filename: '',
schema: parse(printSchema(schema)),
// TODO: Add support for fragments
documents: [],
config,
config: mergeConfig,
plugins: [
{ pluginContent: {} },
{ typescript: {} },
{ typescriptResolvers: {} },
],
pluginMap: {
pluginContent: {
plugin: pluginContent,
},
typescript: typescriptPlugin,
typescriptResolvers: typescriptResolversPlugin,
},
Expand All @@ -104,17 +144,25 @@ async function typescriptResolvers(

async function urqlIntrospection(
schema: GraphQLSchema,
config: CodegenClientConfig = {},
config: CodegenClientConfig,
) {
const mergeConfig = defu(config, {
useTypeImports: true,
} as CodegenClientConfig)

const res = await codegen({
filename: 'a.ts',
schema: parse(printSchema(schema)),
documents: [],
config,
config: mergeConfig,
plugins: [
{ pluginContent: {} },
{ urqlIntrospection: {} },
],
pluginMap: {
pluginContent: {
plugin: pluginContent,
},
urqlIntrospection: urqlIntrospectionPlugin,
},
}).catch((e) => {
Expand All @@ -128,22 +176,31 @@ async function urqlIntrospection(
async function generateTypedDocumentNode(
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: CodegenClientConfig = {},
config: CodegenClientConfig,
) {
const mergeConfig = defu(config, {
enumsAsTypes: true,
useTypeImports: true,
} as CodegenClientConfig)

// See https://www.graphql-code-generator.com/docs/getting-started/programmatic-usage for more details
const res = await codegen({
// Filename is not used. This is because the typescript plugin returns a string instead of writing to a file.
filename: 'a.ts',
schema: parse(printSchema(schema)),
// TODO: Add support for fragments
documents,
config,
config: mergeConfig,
plugins: [
{ pluginContent: {} },
{ typescript: {} },
{ typescriptOperations: {} },
{ typedDocumentNode: {} },
],
pluginMap: {
pluginContent: {
plugin: pluginContent,
},
typescript: typescriptPlugin,
typescriptOperations: {
plugin: typescriptOperationsPlugin,
Expand Down
Loading

0 comments on commit 6f7bca1

Please sign in to comment.