I'm having a issue with deploying graphql query something about top level schema error.

11 replies
Last updated: Sep 12, 2020
I'm having a issue with deploying graphql query something about top level schema error. I have tried moving the schema type into it's own object which usually works but I still get the same error for the following code
This is the exact errot I am getting

Error: Encountered anonymous inline image "logoImage" for field/type "LogoImage". To use this field with GraphQL you will need to create a top-level schema type for it. See <https://docs.sanity.io/help/schema-lift-anonymous-object-type>
homepage.js

{
      name: "logoImageWhite",
      type: "logoImage",
      title: "Logo White Image",
    },
    {
      name: "logoImageColor",
      type: "logoImage",
      title: "Logo Color Image",
    },
logoImage.js

export default {
  title: "Logo Image",
  type: "object",
  name: "logoImage",
  fields: [
    {
      name: "logoImage",
      type: "image",
      title: "Logo Image",
      fields: [
        {
          name: "alt",
          type: "string",
          title: "Alt Tag",
          options: {
            isHighlighted: true, // <-- make this field easily accessible
          },
        },
      ],
    },
  ],
};

AI Update

I can see the issue! You're getting this error because you have a nested image type inside your logoImage object, and the image type itself has fields (the alt text). This creates what GraphQL sees as an "anonymous inline" structure that's too deeply nested.

The problem is in your logoImage.js schema - you have an object type that contains an image field with additional fields. GraphQL needs flatter structures.

Here's how to fix it:

Option 1: Simplify by using the image type directly (Recommended)

Instead of wrapping the image in an object, just use the image type with its fields directly in your homepage.js:

// In homepage.js
{
  name: "logoImageWhite",
  type: "image",
  title: "Logo White Image",
  fields: [
    {
      name: "alt",
      type: "string",
      title: "Alt Tag",
      options: {
        isHighlighted: true,
      },
    },
  ],
},
{
  name: "logoImageColor",
  type: "image",
  title: "Logo Color Image",
  fields: [
    {
      name: "alt",
      type: "string",
      title: "Alt Tag",
      options: {
        isHighlighted: true,
      },
    },
  ],
}

Option 2: Flatten your logoImage object

If you need the object wrapper for other reasons, flatten it by removing the nested image and putting the fields at the object level:

// logoImage.js
export default {
  title: "Logo Image",
  type: "object",
  name: "logoImage",
  fields: [
    {
      name: "asset",
      type: "reference",
      to: [{type: "sanity.imageAsset"}],
      title: "Image Asset",
    },
    {
      name: "alt",
      type: "string",
      title: "Alt Tag",
    },
  ],
};

The core issue is that GraphQL can't handle the nesting pattern of object β†’ image β†’ fields. As explained in the Sanity documentation on object types and this answer about GraphQL inline images, you need to avoid deeply nested anonymous structures when using GraphQL.

After making these changes, redeploy your GraphQL schema and the error should be resolved!

Show original thread
11 replies
I know why your getting the error, im trying to find the sanity doc that references this.
On second thought - your probably getting some sort of naming collision - your using logoImage as a name on the top level, and as a fieldName.
hmm I tried
export default {
  title: "Logo Image",
  type: "object",
  name: "logoImage",
  fields: [
    {
      name: "logoImageBlock",
      type: "image",
      title: "Logo Image",
      fields: [
        {
          name: "alt",
          type: "string",
          title: "Alt Tag",
          options: {
            isHighlighted: true, // <-- make this field easily accessible
          },
        },
      ],
    },
  ],
};
and got the error

Error: Encountered anonymous inline image "logoImageBlock" for field/type "LogoImage". To use this field with GraphQL you will need to create a top-level schema type for it. See <https://docs.sanity.io/help/schema-lift-anonymous-object-type>
I thought that was where the naming collision occured so no sure here. Thanks for your help so far
The error has the doc I was looking for! https://www.sanity.io/help/schema-lift-anonymous-object-type
I think what you need to do is lift the fields portion into a new object - then reference that in the
type
section so soemthing like
type: logoImageBlock
Usually doesnt throw an error until you try to deploy graphql
So something like this:
Top Level
export default {
  title: "Logo Image",
  type: "logoImageBlock",
  name: "logoImage"
}

Logo Image Block
export default {
  title: "Logo Image Block",
  type: "image",
  name: "logoImageBlock",
  fields: [
    {... your current fields content}
  ]

}
I dont know if type: image would trigger that too, but thats what I would try next, esp if you are getting that error
Or just clean up the original - instead of making it an object at top level, why not just make logoImage an type:image then add your fields in? Dont know the context though
Thanks so much man, that worked
πŸ‘πŸ‘ I just ran into this on a project when i went to deploy the graph so it was pretty fresh 😁 Sweet that your up and running now

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?