Skip to content

Commit

Permalink
allow adding extra types and change default CodeGen settings (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jun 9, 2023
1 parent 74191a8 commit 157bfb4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-mice-tickle.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .changeset/mean-ladybugs-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': minor
---

Change `avoidOptionals` to false in the base type generation
1 change: 1 addition & 0 deletions packages/graphqlsp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 16 additions & 12 deletions packages/graphqlsp/src/graphql/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,37 @@ 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<string, unknown>
scalars: Record<string, unknown>,
extraImports?: string
) => {
if (!schema) return;

const config = {
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
Expand Down Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion packages/graphqlsp/src/graphql/getSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const loadSchema = (
schema: string,
logger: Logger,
baseTypesPath: string,
scalars: Record<string, unknown>
scalars: Record<string, unknown>,
extraImports?: string
): { current: GraphQLSchema | null } => {
const ref: { current: GraphQLSchema | null } = { current: null };
let url: URL | undefined;
Expand Down
4 changes: 3 additions & 1 deletion packages/graphqlsp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -42,7 +43,8 @@ function create(info: ts.server.PluginCreateInfo) {
info.config.schema,
logger,
baseTypesPath,
scalars
scalars,
extraImports
);

proxy.getSemanticDiagnostics = (filename: string): ts.Diagnostic[] => {
Expand Down

0 comments on commit 157bfb4

Please sign in to comment.