This is a simple GraphQL server using Express, Mongoose, and Babel with a fairly generic data model. The server uses a MongoDB backing store.
It is tested with Jest Unit and API tests.
- Node.js
- MongoDB
- MongoDB Community with Docker is recommended.
-
Clone the repository:
git clone https://github.com/testetson22/graphql-server-example.git cd graphql-server-example -
Install dependencies (use --force to avoid upstream dep issues):
npm install --force
To start the server using Docker, follow these steps:
-
Optional: Build the GQL server:
bash ./docker-build.sh
-
Run the GQL server and database:
bash ./docker-run.sh
-
Optional: Stop and remove containers
bash ./docker-stop-rm.sh
NOTE: You may see some docker daemon errors, but if you see the following you should be all set:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c93c6a7a5be graphql-server-example-1.0.0 "docker-entrypoint.s…" About a minute ago Up 59 seconds 0.0.0.0:8080->8080/tcp graphql-server-example
6a60a39468b1 mongodb/mongodb-community-server:latest "python3 /usr/local/…" About a minute ago Up About a minute 0.0.0.0:27017->27017/tcp mongodb
NOTE: You need to have a MongoDB instance running.
To start the server, run:
npm startTo run the unit tests:
npm run unittestTo run the API tests:
NOTE: You need to have a MongoDB instance running.
npm run apitestThe GraphQL schema is defined in data/item.graphql. It includes the following types and operations:
ItemStatusLocation
getItem(id: ID): ItemgetAllItems: [Item]
createItem(input: ItemInput): ItemupdateItem(input: ItemInput): ItemdeleteItem(id: ID!): StringdeleteAllItems: String
The resolvers for the GraphQL operations are implemented in data/resolvers.js.
The MongoDB connection is handled in data/db.js. The item model is defined in data/model.js.
Unit tests for the resolvers are located in test/unit/resolvers.test.js. API tests are located in test/api/graphql.test.js. Helper functions for testing are in test/helpers/helpers.js.
- GitHub Actions for standing up the GQL server & DB and running API tests.