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.
Yoga can also run on other platforms, such as Cloudflare Workers or Deno. We recommend checking out the integrations section in our documentation after finishing the tutorial.
v2
You'll need the @graphql-yoga/node
package available in your project, so install it using the following command:
yarn add --save-exact @graphql-yoga/node
Now, update src/main.ts
to create a simple GraphQL HTTP server on port 4000
:
import { createServer } from '@graphql-yoga/node'
import { schema } from './schema'
async function main() {
const server = createServer({ schema })
await server.start()
}
main()
Now, try to run your server again with npm run dev
(or, npm run start
).
The output should look similar to the following:
$ cross-env NODE_ENV=development ts-node-dev --exit-child --respawn src/main.ts
[INFO] 15:40:14 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.5.5)
[2022-02-25 14:40:17.810 +0000] DEBUG (13296 on DESKTOP-U72CGKK): Setting up server.
[2022-02-25 14:40:17.818 +0000] INFO (13296 on DESKTOP-U72CGKK): GraphQL Server running at http://127.0.0.1:4000/graphql.
v3
You'll need the graphql-yoga
package available in your project, so install it using
the following command:
yarn add --save-exact graphql-yoga
Now, update src/main.ts
to create a simple GraphQL HTTP server on port 4000
:
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'
import { schema } from './schema'
function main() {
const yoga = createYoga({ schema })
const server = createServer(yoga)
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})
}
main()
Now, try to run your server again with npm run dev
(or, npm run start
), 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.
query {
hello
}
Unsurprisingly, the result looks like the following:
{ "data": { "hello": "Hello World!" } }
Of course, it is also possible to send requests to the service without GraphiQL, e.g. by using curl
.
Run the following command within your terminal for executing the same query operation.
curl -X POST http://localhost:4000/graphql -H "Content-type: application/json" --data-raw '{"query": "query { hello }"}'
Oh wonder, the result looks like the following:
{ "data": { "hello": "Hello World!" } }