How to exclude the URL parameter in a Gatsby page query if the public field is false.

7 replies
Last updated: Aug 12, 2021
I have this query: query { courses: allSanityCourses { nodes { price name playlist { title url public } } } } Is it possible to not send the url parameter to the component if the public field is false? The frontend code cannot have this URL in it.
Aug 10, 2021, 2:36 PM
This is in the context of a Gatsby page query which runs at build time.
Aug 10, 2021, 2:38 PM
Sure, here’s an example with
select
:
https://groq.dev/x6jAjBKFZK7zc94QfNMpEG

*[] {
  "secretUrl": select(
    public == true => url,
    "not public"
  )
}
Or its sugar form


*[] {
  public == true => {
    "secretUrl": url
  }
}
Aug 10, 2021, 4:55 PM
ah I’ve just realized you’re doing this with Graphql in Gatsby. You can add resolver with Gatsby’s createSchemaCustomization. See the docs here:

https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization/
Aug 10, 2021, 4:58 PM
Hey
user S
, thanks for the link! I’ve tried:exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions
const typeDefs = [
schema.buildObjectType({
name: “SanityCourses”,
fields: {
playlist: {
type: “[SanityVideo]“,
url: {
type: “String”,
resolve: (source) => {
console.log(source)
return “nope”
}
},
},
},
interfaces: [“Node”],
}),
]
createTypes(typeDefs)
}

But the url does not change. Any idea how to target that nested “url” field?
Aug 10, 2021, 8:48 PM
Hey
user S
, IIRC for this you’d have to find the type of the
playlist
field (perhaps
SanityCoursePlaylist
or something like that, you can find out in graphiq), then modify that type instead of the
SanityCourses
type. Hope that make sense!
Aug 11, 2021, 2:33 PM
Nice! I usually reach for
createSchemaCustomization
first, but what
user S
says make sense. For adding a computed field like this, createResolver definitely fits the bill since it run lasts (IIRC)
Aug 12, 2021, 8:35 AM

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?