Watch a live product demo 👀 See how Sanity powers richer commerce experiences

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

The Sanity Composable Content Cloud is the 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.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing