Feed result of one graphql query into another?












1















Why I need to do this: I'm using graphql + gatsby + contentful. I'm using the rich text field in Contentful which allows to embed object references inside of markdown. I'd like to display the embedded entry.



My query looks like this:



export const pageQuery = graphql`
query BlogPostBySlug($slug: String!) {
richTextField {
content {
nodeType
data {
target {
sys {
id
}
}
}
}
}
}
}
`


The richTextField.content comes back looking like this:



{
"data": {
"target": {
"sys": {
"id": "5wVh2a6XvySacEioOEMyqQ",
}
}
},
}


The id, "5wVh2a6XvySacEioOEMyqQ", is a reference to another database entry. I can see two potential ways in how this might work:




  1. Some way of telling graphql that the id in the query refers to the other database model and have it autopopulate fields. Is this possible?


  2. Get the results of this query, collect all the ids and construct another query where it searches for entries with those ids. It seems like apollo might be a solution, but it doesn't work well with gatsby. Is this possible?



What is the best way to do this?



Thank you! :)










share|improve this question





























    1















    Why I need to do this: I'm using graphql + gatsby + contentful. I'm using the rich text field in Contentful which allows to embed object references inside of markdown. I'd like to display the embedded entry.



    My query looks like this:



    export const pageQuery = graphql`
    query BlogPostBySlug($slug: String!) {
    richTextField {
    content {
    nodeType
    data {
    target {
    sys {
    id
    }
    }
    }
    }
    }
    }
    }
    `


    The richTextField.content comes back looking like this:



    {
    "data": {
    "target": {
    "sys": {
    "id": "5wVh2a6XvySacEioOEMyqQ",
    }
    }
    },
    }


    The id, "5wVh2a6XvySacEioOEMyqQ", is a reference to another database entry. I can see two potential ways in how this might work:




    1. Some way of telling graphql that the id in the query refers to the other database model and have it autopopulate fields. Is this possible?


    2. Get the results of this query, collect all the ids and construct another query where it searches for entries with those ids. It seems like apollo might be a solution, but it doesn't work well with gatsby. Is this possible?



    What is the best way to do this?



    Thank you! :)










    share|improve this question



























      1












      1








      1








      Why I need to do this: I'm using graphql + gatsby + contentful. I'm using the rich text field in Contentful which allows to embed object references inside of markdown. I'd like to display the embedded entry.



      My query looks like this:



      export const pageQuery = graphql`
      query BlogPostBySlug($slug: String!) {
      richTextField {
      content {
      nodeType
      data {
      target {
      sys {
      id
      }
      }
      }
      }
      }
      }
      }
      `


      The richTextField.content comes back looking like this:



      {
      "data": {
      "target": {
      "sys": {
      "id": "5wVh2a6XvySacEioOEMyqQ",
      }
      }
      },
      }


      The id, "5wVh2a6XvySacEioOEMyqQ", is a reference to another database entry. I can see two potential ways in how this might work:




      1. Some way of telling graphql that the id in the query refers to the other database model and have it autopopulate fields. Is this possible?


      2. Get the results of this query, collect all the ids and construct another query where it searches for entries with those ids. It seems like apollo might be a solution, but it doesn't work well with gatsby. Is this possible?



      What is the best way to do this?



      Thank you! :)










      share|improve this question
















      Why I need to do this: I'm using graphql + gatsby + contentful. I'm using the rich text field in Contentful which allows to embed object references inside of markdown. I'd like to display the embedded entry.



      My query looks like this:



      export const pageQuery = graphql`
      query BlogPostBySlug($slug: String!) {
      richTextField {
      content {
      nodeType
      data {
      target {
      sys {
      id
      }
      }
      }
      }
      }
      }
      }
      `


      The richTextField.content comes back looking like this:



      {
      "data": {
      "target": {
      "sys": {
      "id": "5wVh2a6XvySacEioOEMyqQ",
      }
      }
      },
      }


      The id, "5wVh2a6XvySacEioOEMyqQ", is a reference to another database entry. I can see two potential ways in how this might work:




      1. Some way of telling graphql that the id in the query refers to the other database model and have it autopopulate fields. Is this possible?


      2. Get the results of this query, collect all the ids and construct another query where it searches for entries with those ids. It seems like apollo might be a solution, but it doesn't work well with gatsby. Is this possible?



      What is the best way to do this?



      Thank you! :)







      graphql gatsby contentful






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 19:45







      Ekaterina

















      asked Nov 15 '18 at 17:14









      EkaterinaEkaterina

      7918




      7918
























          2 Answers
          2






          active

          oldest

          votes


















          0














          The richtext field is fairly new and it looks like this is not resolved yet. Might be good to give the Gatsby folks more information here so that they can progress.






          share|improve this answer































            0














            You could try using foreign keys to associate the nodes.



            In your gatsby-node.js file you'll need to add a key appended with ___NODE with the id of the sys node to link:



            exports.sourceNodes = async ({ getNodes, actions, createContentDigest }) => {
            // find all the sys nodes - you'll need to alter this filter to get the right one
            const sysNodes = getNodes().filter(node => node.internal.type === 'File');

            // go through the posts to map the sys nodes
            // these nodes may already exist, so you could filter `getNodes` like above
            const postData = howeverYouGetDataFromContentful();

            postData.forEach(post => {
            const sys = sysNodes.find(embedded => embedded.id === post.data.target.embedded.id);

            // This will create a brand-new node.
            // If your posts already exist, then use `createNodeField` mentioned below
            const node = {
            id: post.id,
            children: ,
            internal: {
            type: 'post',
            mediaType: 'text/json',
            contentDigest: createContentDigest(JSON.stringify(post))
            },
            sys___NODE: sys.id
            };

            actions.createNode(node);
            });
            }


            If the nodes already exist by the time sourceNodes is called (which they probably will be with Contentful), then you can filter them using getNodes. You'll also probably need to use createNodeField instead of createNode, as mentioned here:
            https://github.com/gatsbyjs/gatsby/issues/3129#issuecomment-360927436 to avoid recreating nodes.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324711%2ffeed-result-of-one-graphql-query-into-another%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              The richtext field is fairly new and it looks like this is not resolved yet. Might be good to give the Gatsby folks more information here so that they can progress.






              share|improve this answer




























                0














                The richtext field is fairly new and it looks like this is not resolved yet. Might be good to give the Gatsby folks more information here so that they can progress.






                share|improve this answer


























                  0












                  0








                  0







                  The richtext field is fairly new and it looks like this is not resolved yet. Might be good to give the Gatsby folks more information here so that they can progress.






                  share|improve this answer













                  The richtext field is fairly new and it looks like this is not resolved yet. Might be good to give the Gatsby folks more information here so that they can progress.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 10:13









                  stefan judisstefan judis

                  31928




                  31928

























                      0














                      You could try using foreign keys to associate the nodes.



                      In your gatsby-node.js file you'll need to add a key appended with ___NODE with the id of the sys node to link:



                      exports.sourceNodes = async ({ getNodes, actions, createContentDigest }) => {
                      // find all the sys nodes - you'll need to alter this filter to get the right one
                      const sysNodes = getNodes().filter(node => node.internal.type === 'File');

                      // go through the posts to map the sys nodes
                      // these nodes may already exist, so you could filter `getNodes` like above
                      const postData = howeverYouGetDataFromContentful();

                      postData.forEach(post => {
                      const sys = sysNodes.find(embedded => embedded.id === post.data.target.embedded.id);

                      // This will create a brand-new node.
                      // If your posts already exist, then use `createNodeField` mentioned below
                      const node = {
                      id: post.id,
                      children: ,
                      internal: {
                      type: 'post',
                      mediaType: 'text/json',
                      contentDigest: createContentDigest(JSON.stringify(post))
                      },
                      sys___NODE: sys.id
                      };

                      actions.createNode(node);
                      });
                      }


                      If the nodes already exist by the time sourceNodes is called (which they probably will be with Contentful), then you can filter them using getNodes. You'll also probably need to use createNodeField instead of createNode, as mentioned here:
                      https://github.com/gatsbyjs/gatsby/issues/3129#issuecomment-360927436 to avoid recreating nodes.






                      share|improve this answer




























                        0














                        You could try using foreign keys to associate the nodes.



                        In your gatsby-node.js file you'll need to add a key appended with ___NODE with the id of the sys node to link:



                        exports.sourceNodes = async ({ getNodes, actions, createContentDigest }) => {
                        // find all the sys nodes - you'll need to alter this filter to get the right one
                        const sysNodes = getNodes().filter(node => node.internal.type === 'File');

                        // go through the posts to map the sys nodes
                        // these nodes may already exist, so you could filter `getNodes` like above
                        const postData = howeverYouGetDataFromContentful();

                        postData.forEach(post => {
                        const sys = sysNodes.find(embedded => embedded.id === post.data.target.embedded.id);

                        // This will create a brand-new node.
                        // If your posts already exist, then use `createNodeField` mentioned below
                        const node = {
                        id: post.id,
                        children: ,
                        internal: {
                        type: 'post',
                        mediaType: 'text/json',
                        contentDigest: createContentDigest(JSON.stringify(post))
                        },
                        sys___NODE: sys.id
                        };

                        actions.createNode(node);
                        });
                        }


                        If the nodes already exist by the time sourceNodes is called (which they probably will be with Contentful), then you can filter them using getNodes. You'll also probably need to use createNodeField instead of createNode, as mentioned here:
                        https://github.com/gatsbyjs/gatsby/issues/3129#issuecomment-360927436 to avoid recreating nodes.






                        share|improve this answer


























                          0












                          0








                          0







                          You could try using foreign keys to associate the nodes.



                          In your gatsby-node.js file you'll need to add a key appended with ___NODE with the id of the sys node to link:



                          exports.sourceNodes = async ({ getNodes, actions, createContentDigest }) => {
                          // find all the sys nodes - you'll need to alter this filter to get the right one
                          const sysNodes = getNodes().filter(node => node.internal.type === 'File');

                          // go through the posts to map the sys nodes
                          // these nodes may already exist, so you could filter `getNodes` like above
                          const postData = howeverYouGetDataFromContentful();

                          postData.forEach(post => {
                          const sys = sysNodes.find(embedded => embedded.id === post.data.target.embedded.id);

                          // This will create a brand-new node.
                          // If your posts already exist, then use `createNodeField` mentioned below
                          const node = {
                          id: post.id,
                          children: ,
                          internal: {
                          type: 'post',
                          mediaType: 'text/json',
                          contentDigest: createContentDigest(JSON.stringify(post))
                          },
                          sys___NODE: sys.id
                          };

                          actions.createNode(node);
                          });
                          }


                          If the nodes already exist by the time sourceNodes is called (which they probably will be with Contentful), then you can filter them using getNodes. You'll also probably need to use createNodeField instead of createNode, as mentioned here:
                          https://github.com/gatsbyjs/gatsby/issues/3129#issuecomment-360927436 to avoid recreating nodes.






                          share|improve this answer













                          You could try using foreign keys to associate the nodes.



                          In your gatsby-node.js file you'll need to add a key appended with ___NODE with the id of the sys node to link:



                          exports.sourceNodes = async ({ getNodes, actions, createContentDigest }) => {
                          // find all the sys nodes - you'll need to alter this filter to get the right one
                          const sysNodes = getNodes().filter(node => node.internal.type === 'File');

                          // go through the posts to map the sys nodes
                          // these nodes may already exist, so you could filter `getNodes` like above
                          const postData = howeverYouGetDataFromContentful();

                          postData.forEach(post => {
                          const sys = sysNodes.find(embedded => embedded.id === post.data.target.embedded.id);

                          // This will create a brand-new node.
                          // If your posts already exist, then use `createNodeField` mentioned below
                          const node = {
                          id: post.id,
                          children: ,
                          internal: {
                          type: 'post',
                          mediaType: 'text/json',
                          contentDigest: createContentDigest(JSON.stringify(post))
                          },
                          sys___NODE: sys.id
                          };

                          actions.createNode(node);
                          });
                          }


                          If the nodes already exist by the time sourceNodes is called (which they probably will be with Contentful), then you can filter them using getNodes. You'll also probably need to use createNodeField instead of createNode, as mentioned here:
                          https://github.com/gatsbyjs/gatsby/issues/3129#issuecomment-360927436 to avoid recreating nodes.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 '18 at 11:40









                          whostolemyhatwhostolemyhat

                          2,87332748




                          2,87332748






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324711%2ffeed-result-of-one-graphql-query-into-another%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues