Skip to content

fixed #394 #395

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

Merged
merged 1 commit into from
Jun 10, 2023
Merged
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
6 changes: 6 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DefinitionNode,
DocumentNode,
GraphQLSchema,
isSpecifiedScalarType,
ListTypeNode,
NamedTypeNode,
NameNode,
Expand Down Expand Up @@ -169,3 +170,8 @@ export const topsort = (g: Graph): string[] => {

return results.reverse();
};

export const isGeneratedByIntrospection = (schema: GraphQLSchema): boolean =>
Object.entries(schema.getTypeMap())
.filter(([name, type]) => !name.startsWith('__') && !isSpecifiedScalarType(type))
.every(([, type]) => type.astNode === undefined);
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
import { transformSchemaAST } from '@graphql-codegen/schema-ast';
import { GraphQLSchema, visit } from 'graphql';
import { buildSchema, GraphQLSchema, printSchema, visit } from 'graphql';

import { ValidationSchemaPluginConfig } from './config';
import { topologicalSortAST } from './graphql';
import { isGeneratedByIntrospection, topologicalSortAST } from './graphql';
import { MyZodSchemaVisitor } from './myzod/index';
import { SchemaVisitor } from './types';
import { YupSchemaVisitor } from './yup/index';
Expand Down Expand Up @@ -40,16 +40,20 @@ const schemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConf

const _transformSchemaAST = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
const { schema: _schema, ast } = transformSchemaAST(schema, config);

// See: https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/issues/394
const __schema = isGeneratedByIntrospection(_schema) ? buildSchema(printSchema(_schema)) : _schema;

// This affects the performance of code generation, so it is
// enabled only when this option is selected.
if (config.validationSchemaExportType === 'const') {
return {
schema: _schema,
ast: topologicalSortAST(_schema, ast),
schema: __schema,
ast: topologicalSortAST(__schema, ast),
};
}
return {
schema: _schema,
schema: __schema,
ast,
};
};
62 changes: 60 additions & 2 deletions tests/graphql.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { Graph } from 'graphlib';
import { buildSchema, Kind, ObjectTypeDefinitionNode, parse, print } from 'graphql';
import {
buildClientSchema,
buildSchema,
introspectionFromSchema,
Kind,
ObjectTypeDefinitionNode,
parse,
print,
} from 'graphql';
import dedent from 'ts-dedent';

import { ObjectTypeDefinitionBuilder, topologicalSortAST, topsort } from '../src/graphql';
import { isGeneratedByIntrospection, ObjectTypeDefinitionBuilder, topologicalSortAST, topsort } from '../src/graphql';

describe('graphql', () => {
describe('ObjectTypeDefinitionBuilder', () => {
Expand Down Expand Up @@ -238,3 +246,53 @@ describe('topologicalSortAST', () => {
expect(sortedSchema).toBe(expectedSortedSchema);
});
});

describe('isGeneratedByIntrospection function', () => {
const schemaDefinition = /* GraphQL */ `
scalar CustomScalar

interface Node {
id: ID!
}

type UserType implements Node {
id: ID!
name: String!
email: String!
}

union SearchResult = UserType

enum Role {
ADMIN
USER
}

input UserInput {
name: String!
email: String!
role: Role!
}

type Query {
user(id: ID!): UserType!
search(text: String!): [SearchResult]
}

type Mutation {
createUser(input: UserInput!): UserType!
}
`;

test('returns false for a schema not generated by introspection', () => {
const schema = buildSchema(schemaDefinition);
expect(isGeneratedByIntrospection(schema)).toBe(false);
});

test('returns true for a schema generated by introspection', () => {
const schema = buildSchema(schemaDefinition);
const query = introspectionFromSchema(schema);
const clientSchema = buildClientSchema(query);
expect(isGeneratedByIntrospection(clientSchema)).toBe(true);
});
});
39 changes: 38 additions & 1 deletion tests/myzod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildSchema } from 'graphql';
import { buildClientSchema, buildSchema, introspectionFromSchema } from 'graphql';
import dedent from 'ts-dedent';

import { plugin } from '../src/index';
Expand Down Expand Up @@ -922,4 +922,41 @@ describe('myzod', () => {
expect(result.content).not.toContain(wantNotContain);
}
});

it('issue #394', async () => {
const schema = buildSchema(/* GraphQL */ `
enum Test {
A
B
}

type Query {
_dummy: Test
}

input QueryInput {
_dummy: Test
}
`);
const query = introspectionFromSchema(schema);
const clientSchema = buildClientSchema(query);
const result = await plugin(
clientSchema,
[],
{
schema: 'myzod',
scalars: {
ID: 'string',
},
},
{}
);
const wantContain = dedent`
export function QueryInputSchema(): myzod.Type<QueryInput> {
return myzod.object({
_dummy: TestSchema.optional().nullable()
})
}`;
expect(result.content).toContain(wantContain);
});
});
39 changes: 38 additions & 1 deletion tests/yup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildSchema } from 'graphql';
import { buildClientSchema, buildSchema, introspectionFromSchema } from 'graphql';
import dedent from 'ts-dedent';

import { plugin } from '../src/index';
Expand Down Expand Up @@ -836,4 +836,41 @@ describe('yup', () => {
expect(result.content).not.toContain(wantNotContain);
}
});

it('issue #394', async () => {
const schema = buildSchema(/* GraphQL */ `
enum Test {
A
B
}

type Query {
_dummy: Test
}

input QueryInput {
_dummy: Test
}
`);
const query = introspectionFromSchema(schema);
const clientSchema = buildClientSchema(query);
const result = await plugin(
clientSchema,
[],
{
schema: 'yup',
scalars: {
ID: 'string',
},
},
{}
);
const wantContain = dedent`
export function QueryInputSchema(): yup.ObjectSchema<QueryInput> {
return yup.object({
_dummy: TestSchema.nullable().optional()
})
}`;
expect(result.content).toContain(wantContain);
});
});
40 changes: 39 additions & 1 deletion tests/zod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildSchema } from 'graphql';
import { getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
import { buildClientSchema, buildSchema, introspectionFromSchema, isSpecifiedScalarType } from 'graphql';
import { dedent } from 'ts-dedent';

import { plugin } from '../src/index';
Expand Down Expand Up @@ -989,4 +990,41 @@ describe('zod', () => {
expect(result.content).not.toContain(wantNotContain);
}
});

it('issue #394', async () => {
const schema = buildSchema(/* GraphQL */ `
enum Test {
A
B
}

type Query {
_dummy: Test
}

input QueryInput {
_dummy: Test
}
`);
const query = introspectionFromSchema(schema);
const clientSchema = buildClientSchema(query);
const result = await plugin(
clientSchema,
[],
{
schema: 'zod',
scalars: {
ID: 'string',
},
},
{}
);
const wantContain = dedent`
export function QueryInputSchema(): z.ZodObject<Properties<QueryInput>> {
return z.object({
_dummy: TestSchema.nullish()
})
}`;
expect(result.content).toContain(wantContain);
});
});