Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query an external GraphQL endpoint in Gatsby JS?

I don't seen any clear way to query an outside GraphQL endpoint (i.e. https://somewebsite.com/graphql) for data. Has anyone else done this, and how did you do it?

I assume it requires you to build a custom plugin, but that seems overkill for such a simple need. I have searched the docs and this issue doesn't really ever get addressed. 🤔

like image 953
Daniel Davidson Avatar asked Feb 14 '18 06:02

Daniel Davidson


2 Answers

In Gatsby V2 you don't need to create your own plugin.

What you need is a plugin called gatsby-source-graphql

gatsby-source-graphql uses schema stitching to combine the schema of a third-party API with the Gatsby schema.

You can find more info here.

like image 88
Alfrex92 Avatar answered Nov 14 '22 05:11

Alfrex92


The answer is, as you mentioned, writing a new source plugin. This is how Gatsby gets the data into it's internal GraphQL structure to then query.

Plugins are, at their core, just additions to the gatsby-node, gatsby-browser, and gatsby-ssr files. So you could write the logic needed at the top of your gatsby-node file to avoid abstracting it out into it's own plugin.

like image 35
Mark Michon Avatar answered Nov 14 '22 06:11

Mark Michon