Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

GraphQL-Error: Found Duplicate Type Names: Cat

2 replies
Last updated: Jul 30, 2020
Hi, I’m trying to move a Sanity project to use GraphQL, and I encountered an issue when I tried to deploy the GraphQL-schema. It appears there’s a generated GraphQL union type that ends up having the same name as another type.
I’ve reconstructred the issue in a minimal schema.js, where I have a person document that can have an array of cats. The cats can be created as part of the person document, or as a reference.


import createSchema from "part:@sanity/base/schema-creator";
import schemaTypes from "all:part:@sanity/base/schema-type";

const cat = {
    title: 'Cat',
    name: 'cat',
    type: 'document',
    fields: [
        {
            title: 'Name',
            name: 'name',
            type: 'string'
        }
    ]
};

const person = {
    title: 'Person',
    name: 'person',
    type: 'document',
    fields: [
        {
            title: 'Name',
            name: 'name',
            type: 'string'
        },
        {
            title: 'Cats',
            name: 'cats',
            type: 'array',
            of: [
                {
                    type: 'reference',
                    title: 'Cat Reference',
                    to: [{ type: 'cat' }]
                },
                { type: 'cat' }
            ],
        }
    ]
};

export default createSchema({
    name: "default",
    types: schemaTypes.concat([ cat, person ])
});
This code breaks when you run
sanity graphql deploy
with the error message: “Error: Found duplicate type names: Cat”.
I have found a workaround for this issue that works, but it’s not ideal and I’m looking for a better solution.
Jul 30, 2020, 9:31 AM
As for the details of the workaround:
If you change the cat reference to refer to either a cat or some other type, the generated name will change and there’s no longer a duplicate name. In my case, it changes from Cat to CatOrWorkaround. Additionally I made the workaround document always validate to an error.

The code looks like this:


import createSchema from "part:@sanity/base/schema-creator";
import schemaTypes from "all:part:@sanity/base/schema-type";

const cat = {
    title: 'Cat',
    name: 'cat',
    type: 'document',
    fields: [
        {
            title: 'Name',
            name: 'name',
            type: 'string'
        }
    ]
};

const workaround = {
    title: 'Workaround',
    name: 'workaround',
    type: 'document',
    fields: [
        {
            title: 'Name',
            name: 'name',
            type: 'string'
        }
    ],
    validation: Rule => Rule.required().custom(() =>
        "Do not create a Workaround document. " +
        "The Workaround document fixes a naming issue when deploying the GraphQL schema."
    )
};

const person = {
    title: 'Person',
    name: 'person',
    type: 'document',
    fields: [
        {
            title: 'Name',
            name: 'name',
            type: 'string'
        },
        {
            title: 'Cats',
            name: 'cats',
            type: 'array',
            of: [
                {
                    type: 'reference',
                    title: 'Cat Reference',
                    to: [{ type: 'cat' }, { type: 'workaround' }]
                },
                { type: 'cat' }
            ],
        }
    ]
};

export default createSchema({
    name: "default",
    types: schemaTypes.concat([ cat, person, workaround ])
});
Jul 30, 2020, 9:31 AM
That’s a pretty clever workaround, Tommy 🙂 The issue is occurring because you’re using the
cat
type both as a
document
type (referencing stand-alone docs) and as an
object
type, where creating a cat inside the
person
document will not actually create a new
cat
document but an in-line cat object in the
cats
array.
Generally speaking, we do not yet have a functionality to create new documents from within a different document, in other words: creating references in place. This is very much on our radar as a feature request though.

As an alternative workaround, you could include the reference to
cat
as you already do, but then create a separate cat object type in addition to the cat document type you already have. Let’s call it
catObject
for this purpose. You should then be able to both reference cats (available globally) and create new cats locally in the document (not available globally).
Hope the above explanation is clear enough - happy to give more context!
Jul 30, 2020, 10:07 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?