Gatsby: [gatsby-source-contentful] Why can't I query an entry by id?

Created on 13 Feb 2018  路  4Comments  路  Source: gatsbyjs/gatsby

Description

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.

Environment

Gatsby version: ^1.9.158
Node.js version: 9.4.0
Operating System: macOS 10.13.3

File contents (if changed):

gatsby-config.js:

module.exports = {
  plugins: [{
    resolve: 'gatsby-source-contentful',
    options: {
      spaceId: `...`,
      accessToken: `...`,
    },
  }, 'gatsby-plugin-react-helmet', 'gatsby-plugin-react-next', 'gatsby-plugin-sass'],
}

Actual result

As a response, I receive:

{
  "data": {
    "contentfulNavigation": null
  }
}

Expected behavior

Receive the entry instead.

Steps to reproduce

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
    }
  }
}

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?

All 4 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timbrandin picture timbrandin  路  3Comments

rossPatton picture rossPatton  路  3Comments

3CordGuy picture 3CordGuy  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments