GraphQL is a language that enables you to provide a complete and understandable description of the data in your API. Furthermore, it gives clients the power to ask for exactly what they need and nothing more. The project’s official website can be found here.
✔️ GraphQL is declarative: Query responses are decided by the client rather than the server. A GraphQL query returns exactly what a client asks for and no more.
✔️ GraphQL is compositional: A GraphQL query itself is a hierarchical set of fields. The query is shaped just like the data it returns. It is a natural way for product engineers to describe data requirements.
✔️ GraphQL is strongly-typed: A GraphQL query can be ensured to be valid within a GraphQL-type system at development time allowing the server to make guarantees about the response. This makes it easier to build high-quality client tools
A GraphQL API has four building blocks:
It is defined at the server in the form of objects. Each object corresponds to data types such that they can be queried upon. For example:
type User { id: ID! name: String age: Int }
The schema above defines the shape of a user object with a required field id denoted by the ! sign. Other fields such as the name which is of type string and age which is of type integer are also included. This also validates the schema when querying for the data.
Let’s get started with creating GraphQL⬇️
npm init -y
npm i
graphql express express-graphql
var express = require('express'); var express_graphql = require('express-graphql'); var { buildSchema } = require('graphql'); // GraphQL schema var schema = buildSchema(` type Query { message: String } `); // Root resolver var root = { message: () => 'Hello World!' }; // Create an express server and a GraphQL endpoint var app = express(); app.use('/graphql', express_graphql.graphqlHTTP({ schema: schema, rootValue: root, graphiql: true, }),); app.listen(4000, () => console.log('Express GraphQL Server Now Running On http://localhost:4000/graphql'));
Now, let’s test our app locally by running the following command:
npm start
Next, let’s stop the server by running
Ctrl + C
Now, open the URL http://localhost:4000/graphql on the browser
Now you can run the simple query on the GraphQL playground
{ message }
Create an empty file called Dockerfile:
touch Dockerfile
Open the Dockerfile in your favorite text editor
The first thing we need to do is define what image we want to build from. Here we will use the latest LTS (long-term support) version 16 of node available from the Docker Hub:
FROM node:16
Next, we create a directory to hold the application code inside the image, this will be the working directory for your application:
# Create app directory WORKDIR /usr/src/app
This image comes with Node.js and NPM already installed so the next thing we need to do is to install your app dependencies using the npm binary. Please note that if you are using npm version 4 or earlier a package-lock.json the file will not be generated.
# Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production
Note that, rather than copying the entire working directory, we are only copying the package.json file. This allows us to take advantage of cached Docker layers. Furthermore, the npm ci command, specified in the comments, helps provide faster, more reliable, reproducible builds for production environments. You can read more about this here.
To bundle your app’s source code inside the Docker image, use the COPY instruction:
# Bundle app source COPY . .
Your app binds to port 8080 so you’ll use the EXPOSE instruction to have it mapped by the docker daemon:
EXPOSE 8080
Last but not least, define the command to run your app using CMD which defines your runtime. Here we will use node index.js to start your server:
CMD [ "node", "index.js" ]
Your Dockerfile should now look like this:
FROM node:16 # Create app directory WORKDIR /usr/src/app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 8080 CMD [ "node", "server.js" ]
Create a .dockerignore file in the same directory as your Dockerfile with the following content:
node_modules npm-debug.log
This will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.
Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it’s easier to find later using the docker images command:
docker build . -t <your username>/nodejs-graphql-docker
Your image will now be listed by Docker: $ docker images # Example REPOSITORY TAG ID CREATED node 16 3b66eb585643 5 days ago <your username>/nodejs-graphql-docker latest d64d3505b0d2 1 minute ago
Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container. Run the image you previously built:
docker run -p 49160:8080 -d <your username>/nodejs-graphql-docker
Print the output of your app:
# Get container ID $ docker ps # Print app output $ docker logs <container id> # Example Running on http://localhost:8080
If you need to go inside the container you can use the exec command:
# Enter the container $ docker exec -it <container id> /bin/bash
To test your app, get the port of your app that Docker mapped:
$ docker ps # Example ID IMAGE COMMAND ... PORTS ecce33b30ebf <your username>/nodejs-graphql-docker:latest npm start ... 49160->8080
In the example above, Docker mapped the 8080 port inside of the container to the port 49160 on your machine.
Now you can open http://localhost:4000/ on the browser
In order to shut down the app we started, we run the kill command. This uses the container’s ID, which in this example was ecce33b30ebf.
# Kill our running container $ docker kill <container id> <container id> # Confirm that the app has stopped $ curl -i localhost:49160 curl: (7) Failed to connect to localhost port 49160: Connection refused
We hope this tutorial helped you get up and running a simple Node.js application on Docker.
Building a GraphQL API with Node.js and Docker provides a powerful and scalable solution for modern web development.
By following the steps outlined in this tutorial, developers can create a GraphQL API. By leveraging the capabilities of Node.js and Docker, developers can create high-performance APIs that are capable of handling a variety of use cases and scaling to meet the needs of their users.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork