Create a GraphQL Query With a REST Endpoint As a Data Source
<p>In this short article, let’s look at how we could create a simple graphQL query that fetches data from a RESTful endpoint. We’ll use Node.js and Apollo Server for this purpose.</p>
<p>In this article, we’ll look at a simple <code>Users</code> resource as an example. The aim of this write-up is just to outline the steps and configurations required to create a query that utilizes an existing REST endpoint as its data source, using the apollo graphQL server.</p>
<h2>More content at <a href="https://plainenglish.io/" rel="noopener ugc nofollow" target="_blank">PlainEnglish.io</a>.</h2>
<p><em>Sign up for our </em><a href="http://newsletter.plainenglish.io/" rel="noopener ugc nofollow" target="_blank"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ" rel="noopener ugc nofollow" target="_blank"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/" rel="noopener ugc nofollow" target="_blank"><strong><em>LinkedIn</em></strong></a><strong><em>, </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw" rel="noopener ugc nofollow" target="_blank"><strong><em>YouTube</em></strong></a><strong><em>, and</em></strong><em> </em><a href="https://discord.gg/GtDtUAvyhW" rel="noopener ugc nofollow" target="_blank"><strong><em>Discord</em></strong></a><strong><em>.</em></strong></p>
<h2>Looking to scale awareness and adoption for your tech startup? Check out <a href="https://circuit.ooo/?utm=publication-post-cta" rel="noopener ugc nofollow" target="_blank">Circuit</a>.</h2>
<p>This might come in handy if you’d want to give the consuming client more control over which fields are to be projected in the response.</p>
<p>Without further ado, let’s get started:</p>
<h1>Initialize The GraphQL Project:</h1>
<p>Let’s create a new directory named <code>gserver</code> and then change the directory by typing <code>cd gserver</code>.</p>
<p>Now let’s initiate the project with <code>yarn init -y</code></p>
<h2>Add Dependencies:</h2>
<p>Our primary dependencies are graphQL and apollo-server packages. So let’s add them to the project</p>
<p><a href="https://javascript.plainenglish.io/create-a-graphql-query-with-a-rest-endpoint-as-a-data-source-fc21ae95a441">Click Here</a></p>