Querying an empty slice using GraphQuery

Hi, I'm trying to query empty slices using GraphQuery (fetchLinks v2) in a Next.js app. However, when I query, the API throws an error:

Unable to parse fetch query Invalid fetch parsing Exception.\n\nInvalid input '}', expected NewLine (line 108, column 17):\n    non-repeat {}\n                ^\n\n4 rules mismatched at error location:\n

The query itself looks something like this:

`... on games_section {
    non-repeat {}
    repeat {}
  }`

Is querying an empty slice supported and if not, can it be supported?

Thank you

Hello, You can retrieve the slices of a document, but you need to specify the fields inside the actual Slice instead of adding empty objects.

Which fields are you using inside your Slices?

Hi @Paulina, thank you for the reply.

So it might sound weird, but I don't really need any fields. For example, if I want a divider between two sections, I would have a Divider (<hr />) slice and that would be added between two slices. My Games Section slice is basically a placeholder.

Ok, I see. Your query needs to look for all fields in a slice in the given Custom type.

For example, in this GraphQuery, I'm querying for documents of the type blog that have a Content Relationship field with the API ID of example_content_relationship with a linked document of the type post that has a Slice with the name of example_divider_slice

        const mySuperGraphQuery = `{
          blog {
            example_content_relationship {
              ...on post {
                body{
                  ...on example_divider_slice {
                    non-repeat {
                      ...non-repeatFields
                    }
                    repeat {
                      ...repeatFields
                    }
                  }
                }
              }
            }
          }
        }
        `

        const BlogPosts = await client
          .getByUID('blog', uid, { graphQuery: mySuperGraphQuery })
          .then(function (document) {
            console.log(document)
          })

Note that uid is a dynamic value in this example.

@Paulina My query looks very similar to your example, but it still throws an error when I try it. My query looks something like the following:

page {
 ...other stuff
  body {
   ... on games_section {
     non-repeat  { ...non-repeatFields }
     repeat { ...repeatFields }
   }
 }
}

Am I missing something?

@Paulina Is this something that can be looked into? I'm currently working on a project that needs this capability to fetch empty slices, but errors are thrown when I try to fetch the slice.

Hey, yes of course.

If you prefer, you can send me your Github project and the instructions to run it so I can take a closer look and we can try and resolve it together.

It's a private project, but I can send you a zip file if that's possible. Another option is to create a repro, but it will take some time. Let me know which way you prefer :smiley:.

Whatever is easier for you. I'll send you a dm

Hi @iwatakeshi. Were you able to get this resolved with @Paulina? She's off currently, so I'm taking this over. But I am unable to see any private messages between you two, so I'm not sure what the status is here.

@iwatakeshi You can disregard my previous message. I tested this and was able to retrieve an empty slice.

I think the issue in your case might be a slight syntax error. Try removing the space between ... and on:

page {
  body {
   ...on games_section {
     non-repeat { ...non-repeatFields }
   }
 }
}

Let me know if that solves it for you :slight_smile:

Hi @levi! Thank you for the reply.

So I did remove the spaces as suggested, but it still gives me an error :sweat_smile:.
I think it does have something to do with the formatting. I'll look into it further. My guess is that the backend doesn't like the extra spaces and new lines. I wished there was a tool that made writing queries easier. Also, maybe a better debugging experience would be nice :wink: . I'll let you guys know if I figure something out.

@iwatakeshi I tried it with your project code and got it to work. The issue is that it didn't like you putting the entire field on one line. I got it to work by moving ...non-repeatFields and ...repeatFields to their own lines:

...on games_section {
  non-repeat {
    ...non-repeatFields
  }
  repeat {
    ...repeatFields
  }
}

Give that a try :slight_smile:

@levi That worked! Thank you, Levi!

1 Like

@levi In case anyone else runs into this issue, I've created a helper function for it:

function emptySlice(slice: string) {
  return `...on ${slice} {
  non-repeat {
    ...non-repeatFields
  }
  repeat {
    ...repeatFields
  }
}`
}

Again, thanks!

That's awesome, thanks for sharing!!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.