Node.js has become a popular choice for building server-side applications due to its efficiency and scalability. Apollo Server, on the other hand, is a flexible GraphQL server that integrates seamlessly with Node.js. In this tutorial, we will walk through the process of setting up a basic Node.js application and integrating Apollo Server to create a GraphQL API.
Prerequisites
Before we begin, make sure you have the following installed on your system:
- Node.js and npm (Node Package Manager)
- Basic understanding of JavaScript and Node.js concepts
Step 1: Setting Up the Project
First, let’s create a new directory for our project and navigate into it:
mkdir node-apollo-server-tutorial
cd node-apollo-server-tutorial
Now, initialize a new Node.js project by running:
npm init -y
Step 2: Installing Dependencies
We need to install the necessary dependencies for our project, including apollo-server-express
and express
:
npm install apollo-server-express express graphql
apollo-server-express
: This package provides integration between Apollo Server and Express.js, allowing us to create a GraphQL server with Express.express
: Express.js is a popular web framework for Node.js, which we’ll use to handle HTTP requests.
Step 3: Creating the Apollo Server
Now, let’s create a new file named server.js
in the project directory and set up our Apollo Server:
// server.js
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
// Define GraphQL schema
const typeDefs = gql`
type Query {
hello: String
}
`;
// Define resolvers for GraphQL queries
const resolvers = {
Query: {
hello: () => 'Hello, world!'
}
};
// Create an instance of ApolloServer
const server = new ApolloServer({ typeDefs, resolvers });
// Create an instance of Express
const app = express();
// Apply middleware to integrate ApolloServer with Express
server.applyMiddleware({ app });
// Start the server
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}${server.graphqlPath}`);
});
In this code:
- We import
express
andApolloServer
from their respective packages. - We define our GraphQL schema using the
gql
tag provided by Apollo Server. - We define resolvers to handle GraphQL queries.
- We create an instance of
ApolloServer
, passing in the schema and resolvers. - We create an instance of
Express
. - We use
applyMiddleware
to integrate Apollo Server with Express. - Finally, we start the server and listen for incoming requests on port 4000.
Step 4: Running the Server
To run the server, execute the following command in your terminal:
node server.js
You should see a message indicating that the server is running at http://localhost:4000/graphql
.
Step 5: Testing the GraphQL API
Open your web browser or use a tool like GraphQL Playground or Insomnia to interact with the GraphQL API. Navigate to http://localhost:4000/graphql
in your browser or set up a request to that endpoint in your API client.
Try running the following GraphQL query:
query {
hello
}
You should receive the following response:
{
"data": {
"hello": "Hello, world!"
}
}
Conclusion
In this tutorial, we learned how to create a GraphQL server using Apollo Server and Node.js. We covered setting up the project, installing dependencies, defining a GraphQL schema, creating resolvers, integrating Apollo Server with Express, and testing the GraphQL API. With this foundation, you can now explore more advanced features of GraphQL and Apollo Server to build powerful and scalable APIs for your applications.