Creating a top-level schema for nested objects in GraphQL

2 replies
Last updated: Feb 19, 2023
Hi everyone! I am trying to use Graphql with my Sanity context, but im using groups for a page. And when i try to export the data to Graphql its telling me:
Encountered anonymous inline object "group" for field/type "groups". To use this field with GraphQL you will need to create a top-level schema type for it.

– How should i do this? just create another file called groups? i understand groups it's only to separate by tabs and organize the content input better.. not sure how to apply create a top schema for something like this.
Thanks for your help!
AI Update

Hey! This is a common issue when using GraphQL with Sanity. The error you're seeing happens because GraphQL requires all object types to be explicitly named at the top level of your schema, but fieldset groups in Sanity are just organizational helpers for the Studio UI - they don't create actual schema types.

The good news is that groups/fieldsets are purely for Studio organization and don't affect your actual data structure. You don't need to create a schema type for the groups themselves.

Here's what's likely happening: you probably have an anonymous inline object somewhere in your schema (not the group itself, but an actual field). GraphQL can't handle anonymous objects - they need to be "lifted" to named, top-level object types.

How to fix it:

  1. Find the anonymous object - Look for fields in your schema that look like this:
{
  name: 'myField',
  type: 'object',  // Anonymous inline object
  fields: [
    // nested fields here
  ]
}
  1. Create a named object type - Move this to a top-level schema definition:
// schemas/myFieldObject.js
export default {
  name: 'myFieldObject',  // Give it a unique name
  type: 'object',
  fields: [
    // your nested fields here
  ]
}
  1. Reference it in your original schema:
{
  name: 'myField',
  type: 'myFieldObject'  // Reference the named type
}
  1. Deploy your GraphQL API again:
sanity graphql deploy

The Object Types documentation explains this in detail, and there's a helpful answer specifically about this GraphQL error that shows more examples.

Important: Your fieldset or group configuration for organizing fields in tabs stays exactly as it is - you don't need to change that at all. This is purely about converting any inline object type definitions to named types.

Hi
user E
,
This is a common wall that you’ll hit periodically with GraphQL.

Create an object schema anytime there are nested objects fields within another object. This joists it up to a top-level object in GraphQL.

When I work in GraphQL, I create a single object file called
refObjects
. I keep the entire projects reference objects in it.
Here’s an example of my
refObjects
file from a Gatsby Project

const refCTA = {

name: 'ref.CTA',

type: 'object',

fields: [

{

name: 'refCTA',

title: 'Sitewide CTA',

type: 'reference',

to: [{ type: 'cta' }],

},

]

}


const refInteractiveSpine = {

name: 'ref.InteractiveSpine',

type: 'object',

fields: [

{

name: 'refInteractiveSpine',

title: 'Interactive Spine Image',

type: 'reference',

to: [{ type: 'interactiveSpine' }],

},

]

}


const refMenu = {

name: '<http://ref.menu|ref.menu>',

type: 'object',

fields: [

{

name: 'refMenu',

title: 'Menu',

type: 'reference',

to: [{ type: 'menu' }],

},

]

}


export default[refCTA, refInteractiveSpine, refMenu]
Thank you
user C
! will try this out and let you know. Hope you are having a nice weekend

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?