How to exclude the URL parameter in a Gatsby page query if the public field is false.
7 replies
Last updated: Aug 12, 2021
A
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
A
This is in the context of a Gatsby page query which runs at build time.
Aug 10, 2021, 2:38 PM
D
Sure, here’s an example with
https://groq.dev/x6jAjBKFZK7zc94QfNMpEG
Or its sugar form
select:
https://groq.dev/x6jAjBKFZK7zc94QfNMpEG
*[] { "secretUrl": select( public == true => url, "not public" ) }
*[] { public == true => { "secretUrl": url } }
Aug 10, 2021, 4:55 PM
D
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/
https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization/
Aug 10, 2021, 4:58 PM
A
Hey
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?
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
D
Hey
user S
, IIRC for this you’d have to find the type of the playlistfield (perhaps
SanityCoursePlaylistor something like that, you can find out in graphiq), then modify that type instead of the
SanityCoursestype. Hope that make sense!
Aug 11, 2021, 2:33 PM
A
Hey
user S
, Corey got the answer right on SO: https://stackoverflow.com/questions/68733466/how-to-create-custom-resolvers-for-gatsby-page-queries/68734000 Aug 12, 2021, 7:51 AM
D
Nice! I usually reach for
createSchemaCustomizationfirst, 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.