Skip to content

Error: Schema must contain uniquely named types but contains multiple types named "JSON" #371

Closed
@valerii15298

Description

@valerii15298

Describe the Bug
In project which uses ES Modules we cannot use JSONResolver from graphql-scalars package because of the error:
Error: Schema must contain uniquely named types but contains multiple types named "JSON"

To Reproduce
Here is a very small repro repository
Also check please commonjs branch on the repo where it is working using commonjs instead of esm.

Code from repo for quick look
index.ts:

import "reflect-metadata";
import { buildSchema, FieldResolver, Resolver } from "type-graphql";
import path from "path";
import { Prisma } from "@prisma/client";

import { resolvers, User } from "@generated/type-graphql";
import { JSONResolver } from "graphql-scalars";

import * as url from "url";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

@Resolver((_of) => User)
class CustomResolver {
  @FieldResolver((_type) => JSONResolver, {
    nullable: true,
  })
  async data(): Promise<Prisma.JsonValue[]> {
    return ["1", "2", "3"];
  }
}

buildSchema({
  resolvers: [...resolvers, CustomResolver],
  emitSchemaFile: path.resolve(__dirname, "./generated-schema.graphql"),
  validate: false,
});

schema.prisma:

generator client {
  provider = "prisma-client-js"
}

generator typegraphql {
  provider = "typegraphql-prisma"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model User {
  id   Int  @id
  json Json
}

Expected Behavior
Emiting schema should work the same way as it works when using commonjs(look above for branch where it works)

Logs

> ts-node --esm --transpile-only ./index.ts

C:\Users\valer\OneDrive\Desktop\projects\testrepo\node_modules\.pnpm\graphql@15.8.0\node_modules\graphql\type\schema.js:194
        throw new Error("Schema must contain uniquely named types but contains multiple types named \"".concat(typeName, "\"."));    
              ^
Error: Schema must contain uniquely named types but contains multiple types named "JSON".
    at new GraphQLSchema (C:\Users\valer\OneDrive\Desktop\projects\testrepo\node_modules\.pnpm\graphql@15.8.0\node_modules\graphql\type\schema.js:194:15)
    at Function.generateFromMetadataSync (C:\Users\valer\OneDrive\Desktop\projects\testrepo\node_modules\.pnpm\type-graphql@1.1.1_yxlhq5elwzdhphazhcbwh6wmlq\node_modules\type-graphql\dist\schema\schema-generator.js:31:32)
    at Function.generateFromMetadata (C:\Users\valer\OneDrive\Desktop\projects\testrepo\node_modules\.pnpm\type-graphql@1.1.1_yxlhq5elwzdhphazhcbwh6wmlq\node_modules\type-graphql\dist\schema\schema-generator.js:16:29)
    at buildSchema (C:\Users\valer\OneDrive\Desktop\projects\testrepo\node_modules\.pnpm\type-graphql@1.1.1_yxlhq5elwzdhphazhcbwh6wmlq\node_modules\type-graphql\dist\utils\buildSchema.js:10:61)
    at file:///C:/Users/valer/OneDrive/Desktop/projects/testrepo/index.ts:22:1
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
 ELIFECYCLE  Command failed with exit code 1.
 ELIFECYCLE  Command failed with exit code 1.

Environment (please complete the following information):

  • OS: Windows 11
  • Node 19.7.0
  • typegraphql-prisma version 0.24.4
  • Prisma version 4.11.0
  • TypeScript version 4.9.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions