Building a GraphQL API with Apollo Server in a Docker Image in 5 minutes
Introduction: In today’s world of web development, building APIs has become a necessity. GraphQL has become a popular choice for building APIs because of its flexibility and efficiency. Apollo Server is a popular library for building GraphQL APIs in Node.js. We will learn how to build a GraphQL API using Apollo Server and Docker.
What is Apollo Server? Apollo Server is an open-source GraphQL server that can be used with any GraphQL client, including Apollo Client, Relay, and more. It is built on top of the popular Express.js web framework and provides a simple way to create a GraphQL API.
What is GraphQL? GraphQL is a query language for APIs that was developed by Facebook. It provides a more efficient, powerful, and flexible alternative to traditional REST APIs. GraphQL allows clients to specify exactly what data they need, and the server responds with only that data.
Building a GraphQL API with Apollo Server in a Docker Image: Let’s build a simple GraphQL API that allows users to create and retrieve comments on a blog post. We will use Apollo Server and Docker to build and deploy the API.
1.- Set up the project Create a new directory for the project and navigate to it in the terminal. Run the following command to initialize a new Node.js project:
npm init -y
Install the required dependencies:
npm install apollo-server graphql
2.- Define the schema and resolvers Create a new file called schema.js
and define the schema for the API:
const { gql } = require('apollo-server');
const typeDefs = gql`
type Comment {
id: ID!
text: String!
createdAt: String!
}
type Query {
comments: [Comment]!
}
type Mutation {
createComment(text: String!): Comment!
}
`;
module.exports = typeDefs;
Create a new file called resolvers.js
and define the resolvers for the schema:
const comments = [];
const resolvers = {
Query: {
comments: () => comments,
},
Mutation: {
createComment: (parent, args) => {
const comment = {
id: comments.length + 1,
text: args.text,
createdAt: new Date().toISOString(),
};
comments.push(comment);
return comment;
},
},
};
module.exports = resolvers;
Create the server Create a new file called server.js
and create an instance of Apollo Server:
const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
3.- Build the Docker image Create a new file called Dockerfile
and add the following code:
FROM node:14
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD [ "npm", "start" ]
This Dockerfile defines a Docker image that is based on the official Node.js 14 image. It sets the working directory to /app
, installs the dependencies, copies the project files, exposes port 4000, and runs the npm start
command.
Build the Docker image:
docker build -t my-app .
Run the Docker container Run the Docker container:
docker run -p 4000:4000 my-app
This command runs the Docker container and exposes port 4000 on the host machine, which allows us to access the API at http://localhost:4000/graphql
.
4.- Test the API Open a web browser and navigate to http://localhost:4000/graphql
. This will open the GraphQL Playground, which allows us to test the API.
First, let’s create a new comment:
mutation {
createComment(text: "This is a comment") {
id
text
createdAt
}
}
This should return a JSON object with the new comment’s ID, text, and creation date.
Next, let’s retrieve all comments:
query {
comments {
id
text
createdAt
}
}
This should return a JSON object with an array of all comments.
Conclusion: We learned how to build a GraphQL API using Apollo Server and Docker. We defined a simple schema and resolvers for a blog post comment system and built a Docker image that can be easily deployed to any environment. With Apollo Server and Docker, building and deploying GraphQL APIs has never been easier.