From 157bfb44073c022c9bc510db56b3034ca5aceb99 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Fri, 9 Jun 2023 08:06:45 +0200 Subject: [PATCH] allow adding extra types and change default CodeGen settings (#58) --- .changeset/dry-mice-tickle.md | 5 ++++ .changeset/mean-ladybugs-crash.md | 5 ++++ packages/graphqlsp/README.md | 1 + .../graphqlsp/src/graphql/generateTypes.ts | 28 +++++++++++-------- packages/graphqlsp/src/graphql/getSchema.ts | 3 +- packages/graphqlsp/src/index.ts | 4 ++- 6 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 .changeset/dry-mice-tickle.md create mode 100644 .changeset/mean-ladybugs-crash.md diff --git a/.changeset/dry-mice-tickle.md b/.changeset/dry-mice-tickle.md new file mode 100644 index 00000000..2951eaa4 --- /dev/null +++ b/.changeset/dry-mice-tickle.md @@ -0,0 +1,5 @@ +--- +'@0no-co/graphqlsp': minor +--- + +Add new option named `extraTypes` which can be used to define an additional set of types to help with the `scalar` definitions diff --git a/.changeset/mean-ladybugs-crash.md b/.changeset/mean-ladybugs-crash.md new file mode 100644 index 00000000..6ce2c65c --- /dev/null +++ b/.changeset/mean-ladybugs-crash.md @@ -0,0 +1,5 @@ +--- +'@0no-co/graphqlsp': minor +--- + +Change `avoidOptionals` to false in the base type generation diff --git a/packages/graphqlsp/README.md b/packages/graphqlsp/README.md index 6b8aa0da..005dd277 100644 --- a/packages/graphqlsp/README.md +++ b/packages/graphqlsp/README.md @@ -43,6 +43,7 @@ when on a TypeScript file or adding a file like [this](https://github.com/0no-co - `schema` allows you to specify a url, `.json` or `.graphql` file as your schema - `scalars` allows you to pass an object of scalars that we'll feed into `graphql-code-generator` +- `extraTypes` allows you to specify imports or declare types to help with `scalar` definitions - `shouldCheckForColocatedFragments` when turned on (default), this will scan your imports to find unused fragments and provide a message notifying you about them diff --git a/packages/graphqlsp/src/graphql/generateTypes.ts b/packages/graphqlsp/src/graphql/generateTypes.ts index 4b31f541..0f0ea8f8 100644 --- a/packages/graphqlsp/src/graphql/generateTypes.ts +++ b/packages/graphqlsp/src/graphql/generateTypes.ts @@ -6,12 +6,12 @@ import * as typescriptPlugin from '@graphql-codegen/typescript'; import * as typescriptOperationsPlugin from '@graphql-codegen/typescript-operations'; import * as typedDocumentNodePlugin from '@graphql-codegen/typed-document-node'; import * as addPlugin from '@graphql-codegen/add'; -import { Logger } from '..'; export const generateBaseTypes = async ( schema: GraphQLSchema | null, outputFile: string, - scalars: Record + scalars: Record, + extraImports?: string ) => { if (!schema) return; @@ -19,17 +19,24 @@ export const generateBaseTypes = async ( documents: [], config: { scalars, - // nonOptionalTypename: true, - // avoidOptionals, worth looking into + avoidOptionals: false, enumsAsTypes: true, globalNamespace: true, }, filename: outputFile, schema: parse(printSchema(schema)), - plugins: [{ typescript: {} }], - pluginMap: { - typescript: typescriptPlugin, - }, + plugins: [ + { typescript: {} }, + extraImports && { add: { content: extraImports } }, + ].filter(Boolean), + pluginMap: extraImports + ? { + typescript: typescriptPlugin, + add: addPlugin, + } + : { + typescript: typescriptPlugin, + }, }; // @ts-ignore @@ -74,11 +81,8 @@ export const generateTypedDocumentNodes = async ( config: { namespacedImportName: 'Types', scalars, - // nonOptionalTypename: true, - // avoidOptionals, worth looking into + avoidOptionals: false, enumsAsTypes: true, - dedupeOperationSuffix: true, - dedupeFragments: true, }, filename: outputFile, schema: parse(printSchema(schema)), diff --git a/packages/graphqlsp/src/graphql/getSchema.ts b/packages/graphqlsp/src/graphql/getSchema.ts index dc74e2cb..89521a07 100644 --- a/packages/graphqlsp/src/graphql/getSchema.ts +++ b/packages/graphqlsp/src/graphql/getSchema.ts @@ -17,7 +17,8 @@ export const loadSchema = ( schema: string, logger: Logger, baseTypesPath: string, - scalars: Record + scalars: Record, + extraImports?: string ): { current: GraphQLSchema | null } => { const ref: { current: GraphQLSchema | null } = { current: null }; let url: URL | undefined; diff --git a/packages/graphqlsp/src/index.ts b/packages/graphqlsp/src/index.ts index 2b8cce7e..4cf16e64 100644 --- a/packages/graphqlsp/src/index.ts +++ b/packages/graphqlsp/src/index.ts @@ -31,6 +31,7 @@ function create(info: ts.server.PluginCreateInfo) { logger('Setting up the GraphQL Plugin'); const scalars = info.config.scalars || {}; + const extraImports = info.config.extraImports; const proxy = createBasicDecorator(info); @@ -42,7 +43,8 @@ function create(info: ts.server.PluginCreateInfo) { info.config.schema, logger, baseTypesPath, - scalars + scalars, + extraImports ); proxy.getSemanticDiagnostics = (filename: string): ts.Diagnostic[] => {