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

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!
Feb 18, 2023, 1:08 PM
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]
Feb 18, 2023, 5:42 PM
Thank you
user C
! will try this out and let you know. Hope you are having a nice weekend
Feb 19, 2023, 4:52 PM

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?