Meatgear
Meatgear

Reputation: 139

Gatsby sourceNode does not work with GraphQL arguments

Currently I am trying to pull some data from a API and add to GraphQL using sourceNode and createNode APIs like that:

exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
const modules = ['data', 'stuff', 'bananas'];
const modulesResponse = Promise.all(modules.map(async (module) => getModuleData(module)));
const modulesData = await modulesResponse;

const content = Object.fromEntries(modules.map((entries, index) => [entries, modulesData[index]]));

actions.createNode({
    id: createNodeId(modules.join(',')),
    parent: null,
    children: [],
    content,
    internal: {
        type: 'myData',
        mediaType: 'text/html',
        contentDigest: createContentDigest(content)
    }
});

When I try to filter this (using GraphiQL) with arguments it does not appear to work:

{
  myData(content: {data: {elemMatch: {filterEntry: {eq: "foo"}}}}) {
    content {
      data {
        filterEntry
      }
    }
  }
}

It results in this:

{
  "data": {
    "myData": {
      "content": {
        "data": [
          {
            "filterEntry": "foo"
          },
          {
            "filterEntry": "bar"
          },
          {
            "filterEntry": "baz"
          }
        ]
      }
    }
  }
}

Upvotes: 2

Views: 184

Answers (1)

Meatgear
Meatgear

Reputation: 139

So I found a solution for this. The issue is lies is with the data to be filtered are an entry of the content object. Which means they have no NodeId and are not filterable. The solution is to add a action.createNode foreach entry coming from the API instead just putting in the bulk.

Upvotes: 1

Related Questions