I want to query an entry by id via GraphQL from contentful. I used a different field (title) to query the entry and retrieve its id, but I want to query it by id in the future to avoid naming conflicts, as title does not need to be unique. I am testing this in GraphiQL, but get back null.
Gatsby version: ^1.9.158
Node.js version: 9.4.0
Operating System: macOS 10.13.3
gatsby-config.js:
module.exports = {
plugins: [{
resolve: 'gatsby-source-contentful',
options: {
spaceId: `...`,
accessToken: `...`,
},
}, 'gatsby-plugin-react-helmet', 'gatsby-plugin-react-next', 'gatsby-plugin-sass'],
}
As a response, I receive:
{
"data": {
"contentfulNavigation": null
}
}
Receive the entry instead.
1. Query an entry from contentful including the id field.
2. Try to query this exact entry again by using the previous value for id as a variable as in:
query HomepageQuery($id: String!) {
contentfulNavigation(id: {eq: $id}) {
links {
id
}
}
}
The problem is that contentful ids are left-padded with a c if and only if they start with a number, because GraphQL does not allow names starting with numbers (I am not sure if this is still actually the case).
This change was introduced in 9cfc87355965aec9b2a5326f4bc56025b4cb598a and dff3b2915770057014a5c24dfee9046e6a4326fc.
This leads to inconsistencies in ids returned in a response and the id needed to query for the same object.
Yeah, this is a fun limitation GraphQL gives us. We add contentful_id to nodes for this reason https://github.com/gatsbyjs/gatsby/blob/224af0fe76cd84e2e5e4d98742f3b58c521bc59d/packages/gatsby-source-contentful/src/normalize.js#L49
Hello I'm struggling to get it working.
Contentful graphQL usage is not very documented.
How I'm I suppose to retrieve a single asset using the ID ?
For now I'm doing:
{
contentfulAsset(contentful_id: "47ibVr7o8w8MiEEIGQyeMW") {
title
}
}
This is maddening, sad, and hilarious at the same time. All these new technologies to make our lives "so much better" and they always seem to fail at doing a basic task of the old tech. (facepalm). Does anyone know of any...any...way in gatsby to retrieve a single asset using the ID?
Most helpful comment
This is maddening, sad, and hilarious at the same time. All these new technologies to make our lives "so much better" and they always seem to fail at doing a basic task of the old tech. (facepalm). Does anyone know of any...any...way in gatsby to retrieve a single asset using the ID?