Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to manually set graphql type / link to existing nodes #1583

Closed
peonmodel opened this issue Jul 21, 2017 · 4 comments
Closed

How to manually set graphql type / link to existing nodes #1583

peonmodel opened this issue Jul 21, 2017 · 4 comments

Comments

@peonmodel
Copy link

i see that infer-graphql-type.js will automatically inferObjectStructureFromNodes to create links between nodes
such that an .md file with frontmatter (image: '11.jpg') will return from graphql as frontmatter.image = { id: ${dir}/11.jpg }

is it possible to manually create the links on onCreateNode or something similar?

@KyleAMathews
Copy link
Contributor

Yes, you can can use createNodeField with the not-yet-documented ___NODE syntax to create links between nodes.

Something like.

createNodeField({
  node,
  name: `nodeLink___NODE`,
  value: `ID_OF_ANOTHER_NODE`
})

The auto-linking to file nodes is a special case that relies on relative links between file nodes. When you create links like above, you need to use the actual ID of the node you're trying to link to.

@peonmodel
Copy link
Author

i keep having this error message

UNHANDLED REJECTION Invariant Violation: Encountered an error trying to infer a GraphQL type for: "internal.fieldOwners.nodeOtherLink___NODE". There is no corresponding node with the id field matching: "default-site-plugin"

it comes from

{ contentDigest: '3d75edb4822a6bbdc0d47f3aa1b28e0b',
  type: 'MetadataJson',
  owner: 'gatsby-transformer-json',
  fieldOwners: { nodeOtherLink___NODE: 'default-site-plugin' } }

@toxsick
Copy link
Contributor

toxsick commented Dec 22, 2017

I have the same problem as @peonmodel. Trying to create a node field on a SitePage node with

createNodeField({
  node,
  name: 'source___NODE',
  value: jsFrontmatterNode.id
});

but I'm getting

Error: Invariant Violation: Encountered an error trying to infer a GraphQL type for: "internal.fieldOwners.source___NODE". There is no corresponding node with the id field   matching: "default-site-plugin"

what's wrong?

@toxsick
Copy link
Contributor

toxsick commented Dec 22, 2017

My code is something like this:

exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
  const { createNodeField } = boundActionCreators;
  if (node.internal.type === 'SitePage') {
    const jsFrontmatterNode = getNode(`${node.component} absPath of file >>> JSFrontmatter`);
    if (jsFrontmatterNode) {
      createNodeField({
        node,
        name: 'source___NODE',
        value: jsFrontmatterNode.id
      });
    }
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants