Connect Gatsby with Postgres












3














I would like to pull data from Postgres to Gatsby using graphql. I have written node.js server, but i cannot find way to use it in gatsby.
(https://github.com/gstuczynski/graphql-postgres-test)
Have you any ideas?










share|improve this question



























    3














    I would like to pull data from Postgres to Gatsby using graphql. I have written node.js server, but i cannot find way to use it in gatsby.
    (https://github.com/gstuczynski/graphql-postgres-test)
    Have you any ideas?










    share|improve this question

























      3












      3








      3


      1





      I would like to pull data from Postgres to Gatsby using graphql. I have written node.js server, but i cannot find way to use it in gatsby.
      (https://github.com/gstuczynski/graphql-postgres-test)
      Have you any ideas?










      share|improve this question













      I would like to pull data from Postgres to Gatsby using graphql. I have written node.js server, but i cannot find way to use it in gatsby.
      (https://github.com/gstuczynski/graphql-postgres-test)
      Have you any ideas?







      postgresql graphql gatsby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 10 '18 at 20:03









      tyskocz

      614




      614
























          3 Answers
          3






          active

          oldest

          votes


















          2














          What you need to do is implement a source plugin as seen here https://www.gatsbyjs.org/docs/create-source-plugin/.



          There are many examples within the gatsby repository that implement the source api. See those for inspiration! Basically you need to translate the contents of your Postgres db into a format gatsby understands. Gatsby calls this format “nodes”.



          You could implement a plugin which interfaces with your db directly or with whatever api your node server exposes (graphql, REST etc.).






          share|improve this answer





























            1














            The gatsby-source-pg module connects directly to your database and adds the tables/views/functions/etc to Gatsby's GraphQL API. To use it, install the module:



            yarn add gatsby-source-pg


            then add to to the plugin list in gatsby-config.js:



            module.exports = {
            plugins: [
            /* ... */
            {
            resolve: "gatsby-source-pg",
            options: {
            connectionString: "postgres://localhost/my_db",
            },
            },
            ],
            };


            The connection string can also include username/password, host, port and SSL if you need to connect to remote database; e.g.: postgres://pg_user:pg_pass@pg_host:5432/pg_db?ssl=1



            You can query it in your components using the root postgres field, e.g.:



            {
            postgres {
            allPosts {
            nodes {
            id
            title
            authorId
            userByAuthorId {
            id
            username
            }
            }
            }
            }
            }





            share|improve this answer





























              1














              Gatsby now supports an arbitrary GraphQL endpoint as a source which will help: https://www.gatsbyjs.org/packages/gatsby-source-graphql/





              You can also use Hasura to give you an instant GraphQL API on Postgres and then query that from your Gatsby app. You can follow the tutorial here.



              Step1: Deploy Hasura against your existing Postgres database: https://docs.hasura.io/1.0/graphql/manual/getting-started/using-existing-database.html



              Step 2: Install the gatsby-source-graphql plugin for gatsby: https://www.gatsbyjs.org/packages/gatsby-source-graphql/



              Step 3: Configure the plugin



              {
              plugins: [
              {
              resolve: 'gatsby-source-graphql', // <- Configure plugin
              options: {
              typeName: 'HASURA',
              fieldName: 'hasura', // <- fieldName under which schema will be stitched
              createLink: () =>
              createHttpLink({
              uri: `https://my-graphql.herokuapp.com/v1alpha1/graphql`, // <- Configure connection GraphQL url
              headers: {},
              fetch,
              }),
              refetchInterval: 10, // Refresh every 10 seconds for new data
              },
              },
              ]
              }


              Step 4: Make the GraphQL query in your component



              const Index = ({ data }) => (
              <div>
              <h1>My Authors </h1>
              <AuthorList authors={data.hasura.author} />
              </div>
              )
              export const query = graphql`
              query AuthorQuery {
              hasura { # <- fieldName as configured in the gatsby-config
              author { # Normal GraphQL query
              id
              name
              }
              }
              }


              Other links:




              • Boilerplate/tutorial:
                https://github.com/hasura/graphql-engine/tree/master/community/boilerplates/gatsby-postgres-graphql

              • Blogpost:
                https://blog.hasura.io/create-gatsby-sites-using-graphql-on-postgres-603b5dd1e516


              Note: I work at Hasura.






              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%2f48725049%2fconnect-gatsby-with-postgres%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                What you need to do is implement a source plugin as seen here https://www.gatsbyjs.org/docs/create-source-plugin/.



                There are many examples within the gatsby repository that implement the source api. See those for inspiration! Basically you need to translate the contents of your Postgres db into a format gatsby understands. Gatsby calls this format “nodes”.



                You could implement a plugin which interfaces with your db directly or with whatever api your node server exposes (graphql, REST etc.).






                share|improve this answer


























                  2














                  What you need to do is implement a source plugin as seen here https://www.gatsbyjs.org/docs/create-source-plugin/.



                  There are many examples within the gatsby repository that implement the source api. See those for inspiration! Basically you need to translate the contents of your Postgres db into a format gatsby understands. Gatsby calls this format “nodes”.



                  You could implement a plugin which interfaces with your db directly or with whatever api your node server exposes (graphql, REST etc.).






                  share|improve this answer
























                    2












                    2








                    2






                    What you need to do is implement a source plugin as seen here https://www.gatsbyjs.org/docs/create-source-plugin/.



                    There are many examples within the gatsby repository that implement the source api. See those for inspiration! Basically you need to translate the contents of your Postgres db into a format gatsby understands. Gatsby calls this format “nodes”.



                    You could implement a plugin which interfaces with your db directly or with whatever api your node server exposes (graphql, REST etc.).






                    share|improve this answer












                    What you need to do is implement a source plugin as seen here https://www.gatsbyjs.org/docs/create-source-plugin/.



                    There are many examples within the gatsby repository that implement the source api. See those for inspiration! Basically you need to translate the contents of your Postgres db into a format gatsby understands. Gatsby calls this format “nodes”.



                    You could implement a plugin which interfaces with your db directly or with whatever api your node server exposes (graphql, REST etc.).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 11 '18 at 2:56









                    dkweave

                    362




                    362

























                        1














                        The gatsby-source-pg module connects directly to your database and adds the tables/views/functions/etc to Gatsby's GraphQL API. To use it, install the module:



                        yarn add gatsby-source-pg


                        then add to to the plugin list in gatsby-config.js:



                        module.exports = {
                        plugins: [
                        /* ... */
                        {
                        resolve: "gatsby-source-pg",
                        options: {
                        connectionString: "postgres://localhost/my_db",
                        },
                        },
                        ],
                        };


                        The connection string can also include username/password, host, port and SSL if you need to connect to remote database; e.g.: postgres://pg_user:pg_pass@pg_host:5432/pg_db?ssl=1



                        You can query it in your components using the root postgres field, e.g.:



                        {
                        postgres {
                        allPosts {
                        nodes {
                        id
                        title
                        authorId
                        userByAuthorId {
                        id
                        username
                        }
                        }
                        }
                        }
                        }





                        share|improve this answer


























                          1














                          The gatsby-source-pg module connects directly to your database and adds the tables/views/functions/etc to Gatsby's GraphQL API. To use it, install the module:



                          yarn add gatsby-source-pg


                          then add to to the plugin list in gatsby-config.js:



                          module.exports = {
                          plugins: [
                          /* ... */
                          {
                          resolve: "gatsby-source-pg",
                          options: {
                          connectionString: "postgres://localhost/my_db",
                          },
                          },
                          ],
                          };


                          The connection string can also include username/password, host, port and SSL if you need to connect to remote database; e.g.: postgres://pg_user:pg_pass@pg_host:5432/pg_db?ssl=1



                          You can query it in your components using the root postgres field, e.g.:



                          {
                          postgres {
                          allPosts {
                          nodes {
                          id
                          title
                          authorId
                          userByAuthorId {
                          id
                          username
                          }
                          }
                          }
                          }
                          }





                          share|improve this answer
























                            1












                            1








                            1






                            The gatsby-source-pg module connects directly to your database and adds the tables/views/functions/etc to Gatsby's GraphQL API. To use it, install the module:



                            yarn add gatsby-source-pg


                            then add to to the plugin list in gatsby-config.js:



                            module.exports = {
                            plugins: [
                            /* ... */
                            {
                            resolve: "gatsby-source-pg",
                            options: {
                            connectionString: "postgres://localhost/my_db",
                            },
                            },
                            ],
                            };


                            The connection string can also include username/password, host, port and SSL if you need to connect to remote database; e.g.: postgres://pg_user:pg_pass@pg_host:5432/pg_db?ssl=1



                            You can query it in your components using the root postgres field, e.g.:



                            {
                            postgres {
                            allPosts {
                            nodes {
                            id
                            title
                            authorId
                            userByAuthorId {
                            id
                            username
                            }
                            }
                            }
                            }
                            }





                            share|improve this answer












                            The gatsby-source-pg module connects directly to your database and adds the tables/views/functions/etc to Gatsby's GraphQL API. To use it, install the module:



                            yarn add gatsby-source-pg


                            then add to to the plugin list in gatsby-config.js:



                            module.exports = {
                            plugins: [
                            /* ... */
                            {
                            resolve: "gatsby-source-pg",
                            options: {
                            connectionString: "postgres://localhost/my_db",
                            },
                            },
                            ],
                            };


                            The connection string can also include username/password, host, port and SSL if you need to connect to remote database; e.g.: postgres://pg_user:pg_pass@pg_host:5432/pg_db?ssl=1



                            You can query it in your components using the root postgres field, e.g.:



                            {
                            postgres {
                            allPosts {
                            nodes {
                            id
                            title
                            authorId
                            userByAuthorId {
                            id
                            username
                            }
                            }
                            }
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 2 '18 at 11:51









                            Benjie

                            5,27512139




                            5,27512139























                                1














                                Gatsby now supports an arbitrary GraphQL endpoint as a source which will help: https://www.gatsbyjs.org/packages/gatsby-source-graphql/





                                You can also use Hasura to give you an instant GraphQL API on Postgres and then query that from your Gatsby app. You can follow the tutorial here.



                                Step1: Deploy Hasura against your existing Postgres database: https://docs.hasura.io/1.0/graphql/manual/getting-started/using-existing-database.html



                                Step 2: Install the gatsby-source-graphql plugin for gatsby: https://www.gatsbyjs.org/packages/gatsby-source-graphql/



                                Step 3: Configure the plugin



                                {
                                plugins: [
                                {
                                resolve: 'gatsby-source-graphql', // <- Configure plugin
                                options: {
                                typeName: 'HASURA',
                                fieldName: 'hasura', // <- fieldName under which schema will be stitched
                                createLink: () =>
                                createHttpLink({
                                uri: `https://my-graphql.herokuapp.com/v1alpha1/graphql`, // <- Configure connection GraphQL url
                                headers: {},
                                fetch,
                                }),
                                refetchInterval: 10, // Refresh every 10 seconds for new data
                                },
                                },
                                ]
                                }


                                Step 4: Make the GraphQL query in your component



                                const Index = ({ data }) => (
                                <div>
                                <h1>My Authors </h1>
                                <AuthorList authors={data.hasura.author} />
                                </div>
                                )
                                export const query = graphql`
                                query AuthorQuery {
                                hasura { # <- fieldName as configured in the gatsby-config
                                author { # Normal GraphQL query
                                id
                                name
                                }
                                }
                                }


                                Other links:




                                • Boilerplate/tutorial:
                                  https://github.com/hasura/graphql-engine/tree/master/community/boilerplates/gatsby-postgres-graphql

                                • Blogpost:
                                  https://blog.hasura.io/create-gatsby-sites-using-graphql-on-postgres-603b5dd1e516


                                Note: I work at Hasura.






                                share|improve this answer




























                                  1














                                  Gatsby now supports an arbitrary GraphQL endpoint as a source which will help: https://www.gatsbyjs.org/packages/gatsby-source-graphql/





                                  You can also use Hasura to give you an instant GraphQL API on Postgres and then query that from your Gatsby app. You can follow the tutorial here.



                                  Step1: Deploy Hasura against your existing Postgres database: https://docs.hasura.io/1.0/graphql/manual/getting-started/using-existing-database.html



                                  Step 2: Install the gatsby-source-graphql plugin for gatsby: https://www.gatsbyjs.org/packages/gatsby-source-graphql/



                                  Step 3: Configure the plugin



                                  {
                                  plugins: [
                                  {
                                  resolve: 'gatsby-source-graphql', // <- Configure plugin
                                  options: {
                                  typeName: 'HASURA',
                                  fieldName: 'hasura', // <- fieldName under which schema will be stitched
                                  createLink: () =>
                                  createHttpLink({
                                  uri: `https://my-graphql.herokuapp.com/v1alpha1/graphql`, // <- Configure connection GraphQL url
                                  headers: {},
                                  fetch,
                                  }),
                                  refetchInterval: 10, // Refresh every 10 seconds for new data
                                  },
                                  },
                                  ]
                                  }


                                  Step 4: Make the GraphQL query in your component



                                  const Index = ({ data }) => (
                                  <div>
                                  <h1>My Authors </h1>
                                  <AuthorList authors={data.hasura.author} />
                                  </div>
                                  )
                                  export const query = graphql`
                                  query AuthorQuery {
                                  hasura { # <- fieldName as configured in the gatsby-config
                                  author { # Normal GraphQL query
                                  id
                                  name
                                  }
                                  }
                                  }


                                  Other links:




                                  • Boilerplate/tutorial:
                                    https://github.com/hasura/graphql-engine/tree/master/community/boilerplates/gatsby-postgres-graphql

                                  • Blogpost:
                                    https://blog.hasura.io/create-gatsby-sites-using-graphql-on-postgres-603b5dd1e516


                                  Note: I work at Hasura.






                                  share|improve this answer


























                                    1












                                    1








                                    1






                                    Gatsby now supports an arbitrary GraphQL endpoint as a source which will help: https://www.gatsbyjs.org/packages/gatsby-source-graphql/





                                    You can also use Hasura to give you an instant GraphQL API on Postgres and then query that from your Gatsby app. You can follow the tutorial here.



                                    Step1: Deploy Hasura against your existing Postgres database: https://docs.hasura.io/1.0/graphql/manual/getting-started/using-existing-database.html



                                    Step 2: Install the gatsby-source-graphql plugin for gatsby: https://www.gatsbyjs.org/packages/gatsby-source-graphql/



                                    Step 3: Configure the plugin



                                    {
                                    plugins: [
                                    {
                                    resolve: 'gatsby-source-graphql', // <- Configure plugin
                                    options: {
                                    typeName: 'HASURA',
                                    fieldName: 'hasura', // <- fieldName under which schema will be stitched
                                    createLink: () =>
                                    createHttpLink({
                                    uri: `https://my-graphql.herokuapp.com/v1alpha1/graphql`, // <- Configure connection GraphQL url
                                    headers: {},
                                    fetch,
                                    }),
                                    refetchInterval: 10, // Refresh every 10 seconds for new data
                                    },
                                    },
                                    ]
                                    }


                                    Step 4: Make the GraphQL query in your component



                                    const Index = ({ data }) => (
                                    <div>
                                    <h1>My Authors </h1>
                                    <AuthorList authors={data.hasura.author} />
                                    </div>
                                    )
                                    export const query = graphql`
                                    query AuthorQuery {
                                    hasura { # <- fieldName as configured in the gatsby-config
                                    author { # Normal GraphQL query
                                    id
                                    name
                                    }
                                    }
                                    }


                                    Other links:




                                    • Boilerplate/tutorial:
                                      https://github.com/hasura/graphql-engine/tree/master/community/boilerplates/gatsby-postgres-graphql

                                    • Blogpost:
                                      https://blog.hasura.io/create-gatsby-sites-using-graphql-on-postgres-603b5dd1e516


                                    Note: I work at Hasura.






                                    share|improve this answer














                                    Gatsby now supports an arbitrary GraphQL endpoint as a source which will help: https://www.gatsbyjs.org/packages/gatsby-source-graphql/





                                    You can also use Hasura to give you an instant GraphQL API on Postgres and then query that from your Gatsby app. You can follow the tutorial here.



                                    Step1: Deploy Hasura against your existing Postgres database: https://docs.hasura.io/1.0/graphql/manual/getting-started/using-existing-database.html



                                    Step 2: Install the gatsby-source-graphql plugin for gatsby: https://www.gatsbyjs.org/packages/gatsby-source-graphql/



                                    Step 3: Configure the plugin



                                    {
                                    plugins: [
                                    {
                                    resolve: 'gatsby-source-graphql', // <- Configure plugin
                                    options: {
                                    typeName: 'HASURA',
                                    fieldName: 'hasura', // <- fieldName under which schema will be stitched
                                    createLink: () =>
                                    createHttpLink({
                                    uri: `https://my-graphql.herokuapp.com/v1alpha1/graphql`, // <- Configure connection GraphQL url
                                    headers: {},
                                    fetch,
                                    }),
                                    refetchInterval: 10, // Refresh every 10 seconds for new data
                                    },
                                    },
                                    ]
                                    }


                                    Step 4: Make the GraphQL query in your component



                                    const Index = ({ data }) => (
                                    <div>
                                    <h1>My Authors </h1>
                                    <AuthorList authors={data.hasura.author} />
                                    </div>
                                    )
                                    export const query = graphql`
                                    query AuthorQuery {
                                    hasura { # <- fieldName as configured in the gatsby-config
                                    author { # Normal GraphQL query
                                    id
                                    name
                                    }
                                    }
                                    }


                                    Other links:




                                    • Boilerplate/tutorial:
                                      https://github.com/hasura/graphql-engine/tree/master/community/boilerplates/gatsby-postgres-graphql

                                    • Blogpost:
                                      https://blog.hasura.io/create-gatsby-sites-using-graphql-on-postgres-603b5dd1e516


                                    Note: I work at Hasura.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 20 '18 at 22:45

























                                    answered Oct 10 '18 at 18:45









                                    iamnat

                                    2,367926




                                    2,367926






























                                        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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2f48725049%2fconnect-gatsby-with-postgres%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

                                        Create new schema in PostgreSQL using DBeaver

                                        Deepest pit of an array with Javascript: test on Codility

                                        Costa Masnaga