GraphQL over HTTP is a powerful specification that allows developers to create a compliant server, client, and audit suite without complicated dependencies. In this blog post, we’ll guide you through the process of getting started with graphql-http and creating a simple server. By the end, you’ll be able to serve GraphQL requests efficiently.
Getting Started
- First, install the necessary package using the package manager:
shell
yarn add graphql-http
js
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql';
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: GraphQLString,
resolve: () => 'world',
},
},
}),
});
Starting the Server
Now that we have a schema ready, it’s time to create a server. There are multiple ways to use GraphQL over HTTP. Below are examples using http, Express, Fastify, and Koa.
Using HTTP
js
import http from 'http';
import createHandler from 'graphql-http/lib/use/http';
import schema from './previous-step';
const handler = createHandler({ schema });
const server = http.createServer((req, res) => {
if (req.url.startsWith('/graphql')) {
handler(req, res);
} else {
res.writeHead(404).end();
}
});
server.listen(4000);
console.log('Listening on port 4000');
Using Express
js
import express from 'express';
import createHandler from 'graphql-http/lib/use/express';
import schema from './previous-step';
const app = express();
app.all('/graphql', createHandler({ schema }));
app.listen(4000);
console.log('Listening on port 4000');
An Analogy to Understand the Setup
Imagine creating a restaurant to serve a specific dish – let’s say spaghetti. The recipe represents your GraphQL schema, detailing how to make it and what ingredients you need. The restaurant itself is the server, where customers (your clients) come to place their orders. The specific types of pasta or sauce action as your endpoints in the GraphQL world.
Using Express or Fastify is akin to equipping your restaurant with a friendly waiter who takes orders and serves dishes efficiently, ensuring customers get their food on time, whereas raw HTTP gives you the bare essentials with no frills.
Troubleshooting
If you encounter issues while setting up the server, consider the following steps:
- Ensure that the port 4000 is not blocked by another application.
- Check that your GraphQL endpoint is correctly defined (e.g., ‘/graphql’).
- Verify your schema for any syntax errors or missing resolvers.
- If you receive a ‘404 Not Found’, ensure the request URL matches your defined endpoint.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Final Thoughts
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Now that you know how to set up a GraphQL over HTTP server, feel free to experiment further with subscriptions and other advanced functionality to enhance your application!

