Querying a single field from a Sanity schema using groq in Node.js
50 replies
Last updated: Sep 1, 2022
T
Hello everyone! I am not sure why I am having troubles with this, but I have a schema from which I am trying to only query a single field from, it's an image. It looks like this:
I adapted this query from a different place, where all of the fields of
const filter = groq`*[_type == "siteSettings"]`; const projection = groq`{ image: gigs_image.asset->url}`; const query = [filter, projection].join(' '); const image = await client.fetch(query).catch(error => console.error(error));
siteSettingsis used. But here, I only need that one field
gigs_image. I know I can get that specific field with groq`*[_type == "siteSettings"]{gigs_image}`, but I am not sure how to get the..
.asset->urlstuff there.. I am sorry this is a really poorly phrased question, I am still super new to sanity.
Aug 29, 2022, 9:07 PM
I think I get what you're going for! Why are you defining the query and the projection separately and then joining them?
The following should return just the url you're looking for:
The following should return just the url you're looking for:
`*[_type == "siteSettings"].gigs_image.asset->.url
Aug 29, 2022, 9:13 PM
T
hm, this gives me
null
Aug 29, 2022, 9:18 PM
T
and, it returns an object
Aug 29, 2022, 9:19 PM
T
I am not sure I can answer that question
Aug 29, 2022, 9:20 PM
T
my package json says
Aug 29, 2022, 9:20 PM
T
"@sanity/client": "^2.0.0",
Aug 29, 2022, 9:21 PM
T
"groq": "^0.142.0",
Aug 29, 2022, 9:21 PM
T
does that help?
Aug 29, 2022, 9:21 PM
T
the query is done by node, not by clientside javascript
Aug 29, 2022, 9:21 PM
T
if that makes a difference
Aug 29, 2022, 9:22 PM
T
it's running as the site is being built by eleventy, via node
Aug 29, 2022, 9:22 PM
T
there is no clientside javascript running
Aug 29, 2022, 9:23 PM
T
this is happening during build
Aug 29, 2022, 9:23 PM
T
... I don't know what that question means
Aug 29, 2022, 9:25 PM
T
I added a webhook to netlify, whenever the content changes in production, netlify triggers a rebuild
Aug 29, 2022, 9:25 PM
Where are you using this package?
Aug 29, 2022, 9:27 PM
T
I have a file that requires it and configures it with env vars
Aug 29, 2022, 9:28 PM
T
but I never touched that, it was part of the eleventy starter
Aug 29, 2022, 9:29 PM
T
require('dotenv').config({ path: `.env.${process.env.NODE_ENV || 'development'}` }) const sanityClient = require("@sanity/client"); const { sanity } = require('../../client-config') /** * Set manually. Find configuration in * studio/sanity.json or on <http://manage.sanity.io|manage.sanity.io> */ /* const sanity = { projectId: 'anokeucs', dataset: 'eleventy', useCdn: true } */ module.exports = sanityClient({...sanity, useCdn: !process.env.SANITY_READ_TOKEN, token: process.env.SANITY_READ_TOKEN});
Aug 29, 2022, 9:29 PM
T
so just to make sure I expressed myself correctly, I am getting everything as I wanted, I am just trying to query a single field instead of the entire schema with all of the fields. Instead of doing
settings.gigs_imageI just wanna use
gigs_image
Aug 29, 2022, 9:30 PM
T
I understand that the query for the field is
*[_type == "siteSettings"]{gigs_image}according to doc. But I don't know how to use
.asset->urlin this context, because when I just add it to the query on top, it fails
Aug 29, 2022, 9:31 PM
T
I'm sorry that I am lacking the vocabulary to express myself correctly, I should have not relied on a starter. It's basically a black box for me with abstraction that is entirely foreign 😕
Aug 29, 2022, 9:32 PM
No worries, I understand! That first query I sent you would get you the url of the image you're looking for if you're using the right API version. It looks like your client is missing an API version , so it falls back to v1 which doesn't have support for that sort of query. If you add
If you want your data in it in the form of an object, your first message looks correct. That may have been failing because of the missing API version.
apiVersion: '2022-08-29'to the
sanityobject that contains your project id and dataset it should fix this.
If you want your data in it in the form of an object, your first message looks correct. That may have been failing because of the missing API version.
Aug 29, 2022, 9:37 PM
T
alright, I have added the
apiVersionto the sanity object for the clientConfig. But with groq`*[_type == "siteSettings"].gigs_image.asset->.url`, it still returns
null
Aug 29, 2022, 9:39 PM
T
export default { name: 'siteSettings', type: 'document', title: 'Einstellungen', __experimental_actions: ['update', /* 'create', 'delete', */ 'publish'], fields: [ { name: 'email', type: 'string', title: 'E-Mail*', validation: Rule => Rule.required() }, { name: 'image', type: 'image', title: 'SEO Bild*', description: 'Dieses Bild wird von der Suchmaschine verwendet.', validation: Rule => Rule.required() }, { name: 'sharing_image', type: 'image', title: 'Social Media Bild*', description: 'Dieses Bild wird angezeigt, wenn jemand den Link der Website auf Social Media geteilt wird.', validation: Rule => Rule.required() }, { name: 'gigs_image', type: 'image', title: 'Hero-Image: Gigs', description: 'Test test', validation: Rule => Rule.required() } ] }
Aug 29, 2022, 9:41 PM
T
yes
Aug 29, 2022, 9:41 PM
T
I can access the asset url, if I do it the other way
Aug 29, 2022, 9:42 PM
T
In this query:
const filter = groq`*[_type == "siteSettings"]`; const projection = groq`{email, "image": image.asset->url, "sharing_image": sharing_image.asset->url, "gigs_image": gigs_image.asset->url}`; const query = [filter, projection].join(' '); const sanitySettings = await client.fetch(query).catch(error => console.error(error)); const staticSettings = { domain: process.env.URL || '<http://localhost:8080>', thisYear: format(new Date(), 'yyyy'), }; const settings = {...sanitySettings[0], ...staticSettings};
Aug 29, 2022, 9:42 PM
T
but I don't want all of this other stuff from the data, I only want that one, single
gigs_image
Aug 29, 2022, 9:42 PM
*[_type == "siteSettings"]will return an array. Can you please try RD’s suggestion but slicing the first document?
*[_type == "siteSettings"][0].gigs_image.asset->.url
nullresponse. I’d at least expect an array.)
Aug 29, 2022, 10:21 PM
T
it still returns `null`:/
Aug 30, 2022, 7:43 AM
Would you mind showing the return from
*[_type == "siteSettings"]in Vision? For you to get data from
*[_type == "siteSettings"]{gigs_image}but null from ``*[_type == "siteSettings"].gigs_image` means we must be misunderstanding something in your configuration.
Aug 30, 2022, 3:32 PM
T
this is what it returns:
[ { _createdAt: '2019-03-29T10:09:19Z', _id: 'siteSettings', _rev: 'ns3tjWS9mvqkIfdQ8S1ltp', _type: 'siteSettings', _updatedAt: '2022-08-29T19:35:34Z', email: '', gigs_image: { _type: 'image', asset: [Object] }, image: { _type: 'image', asset: [Object] }, sharing_image: { _type: 'image', asset: [Object] } } ]
Aug 30, 2022, 3:57 PM
T
wait.. I am not sure what "Vision" is
Aug 30, 2022, 4:00 PM
Vision is a plugin in the Studio that lets you run GROQ queries quickly, but the above is equivalent to what you would have gotten there. 👍
Could you please post your snippet of code showing how you’re implementing the naked projection (i.e.,
Could you please post your snippet of code showing how you’re implementing the naked projection (i.e.,
*[_type == "siteSettings"][0].gigs_imageor something similar rather than using a projection) using the
filter,
query,
settings, etc. in your previous posts?
Aug 30, 2022, 4:06 PM
T
I am not sure I fully understand what you mean. Here's my entire code, that I am using right now.
This gives me what I want, but I don't understand why I can't just query do it without the
const query = groq`*[_type == "siteSettings"]{"url": gigs_image.asset->url}`; const image = await client.fetch(query).catch(error => console.error(error)); return image[0].url;
"url", it seems unnecessary
Aug 30, 2022, 4:15 PM
T
but maybe this is just how groq works? I really don't know
Aug 30, 2022, 4:15 PM
T
sorry I can't get syntax highlighting to work in slack? Idk what's going on
Aug 30, 2022, 4:16 PM
What does this give you?
const query = groq`*[_type == "siteSettings"][0].gigs_image.asset->url`; const image = await client.fetch(query).catch(error => console.error(error)); return image;
Aug 30, 2022, 6:20 PM
T
Sorry for the late reply! It gives me back this:
[ { url: '<https://cdn.sanity.io/images/gpygc6vl/production/a7f555027d8a791d9b4dd2915bf4ecbcb3019d36-1600x1600.webp>' } ]
Sep 1, 2022, 6:44 AM
Interesting. Is your client configured on
v1(or with no
apiVersionin the config) by chance?
Sep 1, 2022, 2:45 PM
T
user M
considered this a while ago and advised me to add `apiVersion: '2022-08-29'`to the client-config, which I did!Sep 1, 2022, 3:03 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.