GraphQL Server
Now that you have a GraphQL schema, and you understand the concept of GraphQL schema and a GraphQL query, it's time to create a real GraphQL server.
Now that you have a GraphQL schema, and you understand the concept of GraphQL schema and a GraphQL query, it’s time to create a real GraphQL server.
You are going to use the HTTP protocol to serve the GraphQL server, but note that there are other options for serving GraphQL - you can use WebSocket, SSE (Server-Sent Events) and basically any network transport protocol that you wish! (You can find a list of transport implementations at the end of this page)
Creating a GraphQL HTTP Server with Yoga
In this tutorial, we will be using GraphQL Yoga for building our Node.js GraphQL HTTP server.
You’ll need the graphql-yoga package available in your project (if you have not yet installed in
the last step), so install it using the following command:
Now, update src/main.ts to create a simple GraphQL HTTP server on port 4000:
Now, try to run your server again with
open your browser and navigate to http://localhost:4000/graphql.
Type in the following operation in the left editor section and press the Play button for executing
the operation against the GraphQL server.
Unsurprisingly, the result looks like the following:
Of course, it is also possible to send requests to the service without GraphiQL, e.g. by using
curl.
In another terminal window, run the following command in order to execute the same query operation.
Oh wonder, the result looks like the following: