Ambiguos "Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type" error in nexus graphql

Viewed 1128

I'm getting the following error when using nexus to define a graphql schema with apollo-server.

Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type

The stacktrace doesn't give much information as to where the issue is occurring or to what the problem is. The project has 20+ models and dozens of resolvers so it's quite hard to debug.

Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type, check the docs for extending types
    at extendError (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1744:2)
    at SchemaBuilder.addType (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:603:8)
    at SchemaBuilder.missingType (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1212:5)
    at SchemaBuilder.getOrBuildType (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1540:4)
    at SchemaBuilder.getOutputType (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1471:10)
    at SchemaBuilder.buildOutputField (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1349:52)
    at /Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1307:7
    at Array.forEach (<anonymous>)
    at SchemaBuilder.buildOutputFields (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1306:7)
    at fields (/Users/username/Documents/folder/folder/graphq-nexus-prisma-api/node_modules/nexus/src/builder.ts:1009:33)

Any help appreciated.

4 Answers

I got the same error, but it was because I removed some exports. I'm not exactly sure what caused it, but I basically have a file that exports all of my graphql modules. E.g.

// graphql/modules/index.ts

export * from './file-a';
export * from './file-b';

When I removed the second export line, I started getting the error. I'm probably using some of the types defined in file-b somewhere else, and that's somehow causing the error. Anyway, adding the line back in fixed it (I had removed it by accident anyway).

UPDATE

I also go this by referencing some arg types that didn't exist (typo). For example:

args: {
  where: arg({ type: 'ConversationsWhereInput' }),
  orderBy: arg({ type: 'ConversationsOrderByInput', list: true }),
},

The s in Conversations shouldn't be there. It should be:

args: {
  where: arg({ type: 'ConversationWhereInput' }),
  orderBy: arg({ type: 'ConversationOrderByInput', list: true }),
},

It could have been much helpful if you could elaborate what did you do before this error happened.

I just came up with this error exactly as same as what you got and for me it was because I just accidentally changed the name for objectType of typeDef.

For instance, the name for the FollowUserResult was actually FollowResult and after I changed the name, the whole mutation resolvers related to this objectType became wrong.

export const FollowUserResult = objectType({
  name: "FollowUserResult", // It was originally "FollowResult"
  definition(t) {
    t.nonNull.boolean("ok");
    t.string("error");
  },
});

You may check on this again. Once you got those correct, delete the schema.graphql file and generate the new schema.graphql file.

I got this error because I was using the incorrect Node version. My project didn't have a .nvrmc file (yet) so I was using Node 10 on a project that uses Node 14. So after switching to the correct Node version this error went away

I got this error when I incorrectly specified an unknown type in the type attribute of an extension to the Query type. More specifically:

export const CoursesQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("myQuery", {
      type: InvalidType,  // <--- Changing InvalidType to the correct type fixed the error
      async resolve(_parent, _args, ctx) {
        return ...
      },
    });
  },
});

The error disappeared after I changed InvalidType to the correct type and restarted the server.

Related