Gatsby: Querying reference fields from Contentful

Created on 16 Jan 2019  路  3Comments  路  Source: gatsbyjs/gatsby

I'm am using contentful to hold re-usable feature information for products. I have a content model where I have products and I have features. The products each have a reference field where you can select multiple features (which is returned as an array of strings). How do I use GraphQL to query additional information about the features that my product has? features is a reference field so I was expecting a relationship in GraphQL.

Content model

screen shot 2019-01-16 at 6 06 27 pm

Product content model with features field

screen shot 2019-01-16 at 6 08 08 pm

Product page query for products

query ($category: String!, $subCategory: String!, $product: String!, $rrp: Float!, $verdictRating: Int!) {
          product: allContentfulProduct(
    filter: {
          categories: {slug: {eq: $category}},
          subCategories: {slug: {eq: $subCategory}},
          slug: {eq: $product}
    }) {
    edges {
      node {
        productId
        brand {
          name
          slug
        }
        name
        slug
        categories {
          slug
        }
        subCategories {
          slug
        }
        features
        color
      }
    }
  }
question or discussion

Most helpful comment

馃憢
your query can be something like this

query ($category: String!, $subCategory: String!, $product: String!, $rrp: Float!, $verdictRating: Int!) {
          product: allContentfulProduct(
    filter: {
          categories: {slug: {eq: $category}},
          subCategories: {slug: {eq: $subCategory}},
          slug: {eq: $product}
    }) {
    edges {
      node {
        productId
        brand {
          name
          slug
        }
        name
        slug
        categories {
          slug
        }
        subCategories {
          slug
        }
        features {
          ...on ContentfulFeatureType { 
            name
            etc...
          }
        }
        color
      }
    }
  }

the FeatureType is the type a.k.a contentype of the feature reference. You can have multiple ...on depends on how many type you are referencing

All 3 comments

cc @Khaledgarbaya

馃憢
your query can be something like this

query ($category: String!, $subCategory: String!, $product: String!, $rrp: Float!, $verdictRating: Int!) {
          product: allContentfulProduct(
    filter: {
          categories: {slug: {eq: $category}},
          subCategories: {slug: {eq: $subCategory}},
          slug: {eq: $product}
    }) {
    edges {
      node {
        productId
        brand {
          name
          slug
        }
        name
        slug
        categories {
          slug
        }
        subCategories {
          slug
        }
        features {
          ...on ContentfulFeatureType { 
            name
            etc...
          }
        }
        color
      }
    }
  }

the FeatureType is the type a.k.a contentype of the feature reference. You can have multiple ...on depends on how many type you are referencing

Thank you @Khaledgarbaya!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andykais picture andykais  路  3Comments

totsteps picture totsteps  路  3Comments

hobochild picture hobochild  路  3Comments

3CordGuy picture 3CordGuy  路  3Comments

ferMartz picture ferMartz  路  3Comments