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

connecting nodes from different sources #2575

Closed
brandonmp opened this issue Oct 22, 2017 · 6 comments
Closed

connecting nodes from different sources #2575

brandonmp opened this issue Oct 22, 2017 · 6 comments

Comments

@brandonmp
Copy link
Contributor

I'm sure I'm missing this in the docs/google/issues search, but is there a way to connect nodes coming from different sources?

eg something like

type Note = {
  id: ID!,
  user: User
}
type User = {
  id: ID!,
  notes: [Note]
}

where User and Note come from separate JSONs via plugin-source-filesystem and plugin-transformer-json

I see createParentChildLink, but I'm not sure that's it nor where to use it if it is.

@KyleAMathews
Copy link
Contributor

Use onCreateNode and createNodeField to create the links.

Links are made by adding fields to nodes like link___NODE: ID_OF_ANOTHER_NODE. Or in your case do notes___NODE: [ID1, ID2, ID3]. After adding the link delete the old notes key as when Gatsby creates the GraphQL schema, it'll turn notes___NODE into just notes.

@Necmttn
Copy link
Contributor

Necmttn commented Oct 28, 2017

@KyleAMathews you should add this one in docs. :)

@KyleAMathews
Copy link
Contributor

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!

@nextlevelshit
Copy link
Contributor

Use onCreateNode and createNodeField to create the links.

Links are made by adding fields to nodes like link___NODE: ID_OF_ANOTHER_NODE. Or in your case do notes___NODE: [ID1, ID2, ID3]. After adding the link delete the old notes key as when Gatsby creates the GraphQL schema, it'll turn notes___NODE into just notes.

To be honest, this is not super clear for me. Could someone provide an example, please?

@harrysolovay
Copy link

harrysolovay commented Apr 8, 2019

@KyleAMathews –– I'm digging into Gatsby & really loving it.

Running into an interesting roadblock relating to this issue. Was hoping you could weigh in.

My sourceNodes builds the connection between post and author MarkdownRemark nodes.

createNodeField({
  node,
  name: "authors",
  value: authorNodes,
})

However, the avatar frontmatter field in the author node is reverted back to string form when referenced from within a post.

The following query...

{
  allMarkdownRemark(filter: {fields: {category: {eq: "posts"}}}) {
    edges {
      node {
        fields {
          authors {
            avatar
          }
        }
      }
    }
  }
}

Yields this data:

{
  "data": {
    "allMarkdownRemark": {
      "edges": [
        {
          "node": {
            "fields": {
              "authors": [
                {
                  "avatar": "./avatar.png"
                }
              ]
            }
          }
        },
        {
          "node": {
            "fields": {
              "authors": [
                {
                  "avatar": "./avatar.png"
                }
              ]
            }
          }
        }
      ]
    }
  }
}

(the relative file path... not the transformed gatsby-remark-copy-linked-file/gatsby-remark-images node)

Do you know of a way that I could preserve the transformations made by the plugins?

Any help would be greatly appreciated. THANK YOU!

@Brettm12345
Copy link

@KyleAMathews No idea you could link an array of IDs this is a game changer. Thank you so much

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

6 participants