Contentful logo

Contentful Community

GraphQL query running twice

I am developing a Gatsby website and am currently having an issue that when I run a graphql query to return all products in a given category, it returns each node twice, however for the second batch, all of the names are null. This gives the problem that since the IDs are identical, my gatsby-node file doesn’t know which slug to use and subsequently errors. Note that in Contentful, there is only one instance of each category

Query:

{
  allContentfulCategory {
    edges {
      node {
        slug
        name
      }
    }
  }
}

Result:

{
  "data": {
    "allContentfulCategory": {
      "edges": [
        {
          "node": {
            "slug": "extreme-temperatures",
            "name": "Extreme Temperatures"
          }
        },
        {
          "node": {
            "slug": "warning",
            "name": "Warning"
          }
        },
        {
          "node": {
            "slug": "windsock",
            "name": "Windsock"
          }
        },
        {
          "node": {
            "slug": "temp-port",
            "name": "Temp/Port"
          }
        },
        {
          "node": {
            "slug": "flood",
            "name": "Flood"
          }
        },
        {
          "node": {
            "slug": "industrial-led",
            "name": "Industrial LED"
          }
        },
        {
          "node": {
            "slug": "cleanroom",
            "name": "Cleanroom"
          }
        },
        {
          "node": {
            "slug": "street",
            "name": "Street"
          }
        },
        {
          "node": {
            "slug": "food-manufacture",
            "name": "Food Manufacture"
          }
        },
        {
          "node": {
            "slug": "high-bay",
            "name": "High Bay"
          }
        },
        {
          "node": {
            "slug": "vandal",
            "name": "Vandal"
          }
        },
        {
          "node": {
            "slug": "zone-2-22",
            "name": "Zone 2/22"
          }
        },
        {
          "node": {
            "slug": "zone-1-21",
            "name": "Zone 1/21"
          }
        },
        {
          "node": {
            "slug": "extreme-temperatures",
            "name": null
          }
        },
        {
          "node": {
            "slug": "warning",
            "name": null
          }
        },
        {
          "node": {
            "slug": "windsock",
            "name": null
          }
        },
        {
          "node": {
            "slug": "temp-port",
            "name": null
          }
        },
        {
          "node": {
            "slug": "flood",
            "name": null
          }
        },
        {
          "node": {
            "slug": "industrial-led",
            "name": null
          }
        },
        {
          "node": {
            "slug": "cleanroom",
            "name": null
          }
        },
        {
          "node": {
            "slug": "street",
            "name": null
          }
        },
        {
          "node": {
            "slug": "food-manufacture",
            "name": null
          }
        },
        {
          "node": {
            "slug": "high-bay",
            "name": null
          }
        },
        {
          "node": {
            "slug": "vandal",
            "name": null
          }
        },
        {
          "node": {
            "slug": "zone-2-22",
            "name": null
          }
        },
        {
          "node": {
            "slug": "zone-1-21",
            "name": null
          }
        }
      ]
    }
  },
  "extensions": {
    "enableRefresh": "true"
  }
}

gatsby-node.js

async function turnCategoryIntoPage({ graphql, actions }) {
  const { createPage } = actions
  const response = await graphql(`
  query {
    allContentfulCategory {
      edges {
        node {
          slug
          name
        }
      }
    }
  }
  `)
  response.data.allContentfulCategory.edges.forEach(page => {
    if (edge.node.name !== null){
      createPage({
        path: `/categories/${page.node.slug}`,
        component: path.resolve("./src/templates/category.js"),
        context: {
          category: page.node.name,
        },
      })
    }
  })
}