Issue accessing models via Sanity GraphQL API

30 replies
Last updated: Sep 9, 2021
I've created a simple sanity model with a document called model with a name, datatable, and slug, and then tried to access it via the Sanity Graphql API. I have run sanity graphql deploy, generated a token for the client, but I have been unable to get allModels to return anything but an empty list. The dataset is public. There are at least 5 models in the studio that are published. Is there a step I'm missing?EDIT: Added url:
https://drv14kqs.api.sanity.io/v1/graphql/production/default
query{
  allModels{
    name
    _id
  }
}
AI Update

I can see the issue! You're querying allModels (plural), but since your document type is called model (singular), the correct GraphQL query field should be allModel (singular).

Sanity's GraphQL API generates query fields based on your exact document type name. For a document type called model, the API creates a field called allModel, not allModels. The naming pattern is all<DocumentTypeName> where the document type name is used exactly as defined in your schema.

Try this query instead:

query {
  allModel {
    name
    _id
  }
}

This should return your 5 published models. The query you were using (allModels) doesn't exist in your GraphQL schema because your document type is model, not models.

Why this happens: The GraphQL API doesn't automatically pluralize your document type names. It uses whatever name you defined in your schema. So:

  • Document type model → query field allModel
  • Document type models → query field allModels
  • Document type product → query field allProduct

Pro tip: You can verify the exact field names available in your schema by opening your GraphQL endpoint URL (https://drv14kqs.api.sanity.io/v1/graphql/production/default) in a browser. This will load the GraphQL Playground, which has built-in schema introspection and autocomplete. Just start typing all and it will show you all available query fields based on your deployed schema.

Since you've already run sanity graphql deploy, have published documents, and a public dataset, changing from allModels to allModel should resolve your empty results issue.

For more details about working with Sanity's GraphQL API, check out the official documentation.

If you activate the playground (answer Y when running
sanity graphql deploy
) and then check the SCHEMA menu there, you can see all your options. It might be
allModel
instead of
allModels
for example?
Thanks for the tip, unfortunately I can read the schema in insomnia and it's definitely allModels. allModel doesn't exist. It looks like the query is correct, but I get nothing
Also, have you confirmed that all of your Model documents are published and not in drafts?
Yep only one dataset is available, the default production one. Yep, data is published as well. I can access it via GROQ just fine
Mind sharing the url of the GROQ result so I can compare? You can DM me if you'd prefer it to be private.
Not at all. I've never built a GROQ URL before, just use the JS Client
Not at all. I've never built a GROQ URL before, just use the JS Client
Got it! Do you have the Vision plugin installed in your studio? If so, any query you run there will give you a url right above the results.
Got it! Do you have the Vision plugin installed in your studio? If so, any query you run there will give you a url right above the results.
Interesting. The result is showing as empty on my end. Let me look up your project details.
It looks like the dataset is set to private. Is it showing as public under your Datasets tab in your project in manage.sanity.io
Sorry, I toggled that this morning to test the token with GROQ. I have switched it back to public. The GQL results above were done when it's public, although I did try setting it private and adding a token
You're right, that is strange that your GQL wants Models plural, not Model. I just created the same structure and mine wanted allModel. Can you try running
sanity graphql undeploy
then
sanity graphql deploy
?
Done, no change in the schema or output
Done, no change in the schema or output
Super weird! I'm getting more eyes on this.
It's possible that using generation 1 of the API may be causing this. Could you try 
sanity graphql deploy --generation gen2
 or 
sanity graphql deploy --generation gen3
to see if either of those versions work?
It's possible that using generation 1 of the API may be causing this. Could you try 
sanity graphql deploy --generation gen2
 or 
sanity graphql deploy --generation gen3
to see if either of those versions work?
Running either of the gens appear the same. Weirdly, I also just lost the ability to see all of my content in Sanity Studio, but the Vision plugin still returns data
Do you have a repo you can share?
Do you mind if I sign into your studio to take a look?
Do you mind if I sign into your studio to take a look?
Not at all
OK I got it! You'll be able to get your documents back in the desk structure if you change the
models
document's
name
property to
Model
. At some point, the schema must have gotten changed but not the
name
on the documents themselves. This worked for me:
{
      title: "Models",
      name: "Model",
      type: "document",
      fields: [...]
}
In your
sanity.json
file, you can remove 'vision' from the "plugins" array, since it's enabled in the development environment already.
Now, to get your stuff from GQL: I pulled your
Model
schema into it's own file so that you wouldn't run into issues with strict schemas . So, this leaves your
schema.js
file looking like this:

// First, we must import the schema creator
import createSchema from 'part:@sanity/base/schema-creator'

// Then import schema types from any plugins that might expose them
import schemaTypes from 'all:part:@sanity/base/schema-type'
import model from './model'

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({
  // We name our schema
  name: 'praxis_oem',
  // Then proceed to concatenate our document type
  // to the ones provided by any plugins that are installed
  types: schemaTypes.concat([
    model
  ])
})
And a new file called
model.js
like this:
export default {
  // This is the display name for the type
  title: "Models",
  // The identifier for this document type used in the api's
  name: "Model",
  // Documents have the type 'document'. Your schema may describe types beyond documents
  // but let's get back to that later.
  type: "document",
  // Now we proceed to list the fields of our document
  fields: [
    {
      // The display name for this field
      title: "Name",

      // The identifier for this field used in the api's
      name: "name",

      // The type of this field
      type: "string",
    },
    {
      name: 'specTable',
      type: 'table'
    },
    {
      // The display name for this field
      title: "Slug",

      // The identifier for this field used in the api's
      name: "slug",

      // The type of this field
      type: "slug",
    },
  ]
}
Since I changed the schema, I redeployed GraphQL and tested it out in the playground. Keep in mind that as you keep working on this and making changes to your schema, you'll have to redeploy GQL each time you change it.
Thank you. Can you commit the changes and I'll redeploy it. Thanks for all the help!
Just created the pull request!
Merged and everything looks good. Thanks again!
Happy to help! Glad we got it sorted out!

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?