Create GraphQL Server on Raspberry Pi

Create GraphQL Server is a tool for building GraphQL apps with MongoDB by Tom Coleman. Tom has a good introductory post on CGS here and here. In the following post, I’ll show how to get the project working on a Raspberry Pi.

The main problem that stops Create GraphQL Server from working on the Pi is the lack of the npm mongo-prebuilt package for the Pi (armf architecture). The workaround is to install the armf mongodb package separately.

Here are the steps

Install Mongo for Raspberry Pi

Before installing Create GraphQL Server, install mongo.

sudo apt-get install -y mongodb-server

This will also start mongodb on the default 27107 port. The configuration file can be found at /etc/mongodb.conf and the logfile is at /var/log/mongodb/mongodb.log.

Install Create GraphQL Server & Yarn

Start with the standard steps:

sudo npm install -g create-graphql-server
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
sudo echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Create a Test Project

Create a test project

create-graphql-server init test
cd test

Modify the Project

You’ll have to make the following modifications to the project to work on the Pi

  • Edit the projects package.json and remove the mongodb-server line.
  • Edit the index.js file and remove the line that imports the mongodb-prebuilt package. Change the MONGO_PORT and MONGO_URL lines as follows:
MONGO_PORT = 27017,
MONGO_URL = 'mongodb://localhost:27017/database'
  • Edit the server/index.js file and change the MONGO_PORT and MONGO_URL lines as shown above.

Continue

Use npm to build the package instead of Yarn:

npm install

Run your test project with npm:

npm start

Test Server

You should now be able to bring up GraphiQL at http://localhost:3000/graphiql and perform actual GraphQL queries at http://localhost:3000/graphql.

Systemd Startup

Add a Systemd startup file for your new GraphQL service. Create the file /lib/systemd/system/cgs-tasklist.service:

[Unit]
Description=Create-Graphql-Server tasklist-demo-server
Documentation=http://localhost:3000
After=network.target

[Service]
Environment=NODE_PORT=3000
Type=simple
User=pi
ExecStart=/usr/bin/npm /home/pi/graphql/tasklist-demo-server
WorkingDirectory=/home/pi/graphql/tasklist-demo-server
Restart=on-failure

[Install]
WantedBy=multi-user.target

Reload the systemd configuration:

sudo systemctl daemon-reload
sudo systemctl start cgs-tasklist

Leave a Reply

Your email address will not be published. Required fields are marked *