iteam-software / gatsby-source-sharepoint-online

Exposes data to Gatsby from a sharepoint online tenant.
MIT License
6 stars 4 forks source link

Multiple lists create unexpected results in Gatsby 4 #30

Open madmonkeyworks opened 2 years ago

madmonkeyworks commented 2 years ago

Describe the bug When stating more lists in gatsby-config, the nodes created do not reflect actual fields. The first list has correct fields, the successors contain fields of the previous lists. This happens only in Gatsby 4.0, in Gatsby 2.0 is everything ok.

To Reproduce

  1. Upgrade to Gatsby 4.0
  2. Define more lists in gatsby-config
  3. Run develop
  4. Check GraphiQL

Expected behavior Lists' fields should match the state in the source. Instead, first list is ok, the second list contains fields from the first list, etc.

Desktop (please complete the following information):

Additional context I've tested it in Gatsby 2.0, it works well there. Issue appears in Gatsby 4.0.

SOLUTION

make the nodeId more unique.

Current code in resource.js: id: helpers.createNodeId(data.id),

Change to: const postNodeType = ${site.name}${normalizedListName}ListItem; id: helpers.createNodeId(${postNodeType}-${data.id}),

ADDITIONAL MODIFICATION

In resource.js, I have also bound the entry get() with the createNodes loop. Now, the createNodes fires only when get() is resolved.

remove: const entry = await request.get();

add:

await request.get().then(entry => {
        const postNodeType = `${site.name}${normalizedListName}ListItem`;
        entry.value.forEach((data) => {
          helpers.actions.createNode({
            data,
            id: helpers.createNodeId(`${postNodeType}-${data.id}`),
            parent: null,
            children: [],
            internal: {
              type: postNodeType,
              content: JSON.stringify(data),
              contentDigest: helpers.createContentDigest(data),
            },
          });
        });
      })
    }