Skip to content

testetson22/graphql-server-example

Repository files navigation

GraphQL Server Example

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.

Setup

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/testetson22/graphql-server-example.git
    cd graphql-server-example
  2. Install dependencies (use --force to avoid upstream dep issues):

    npm install --force

Running the Server

Via Docker

To start the server using Docker, follow these steps:

  1. Optional: Build the GQL server:

    bash ./docker-build.sh
  2. Run the GQL server and database:

    bash ./docker-run.sh
  3. 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

Via npm

NOTE: You need to have a MongoDB instance running.

To start the server, run:

npm start

Running Tests

To run the unit tests:

npm run unittest

To run the API tests:

NOTE: You need to have a MongoDB instance running.

npm run apitest

GraphQL Schema

The GraphQL schema is defined in data/item.graphql. It includes the following types and operations:

Types

  • Item
  • Status
  • Location

Queries

  • getItem(id: ID): Item
  • getAllItems: [Item]

Mutations

  • createItem(input: ItemInput): Item
  • updateItem(input: ItemInput): Item
  • deleteItem(id: ID!): String
  • deleteAllItems: String

Resolvers

The resolvers for the GraphQL operations are implemented in data/resolvers.js.

Database

The MongoDB connection is handled in data/db.js. The item model is defined in data/model.js.

Testing

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.

TODO

  • GitHub Actions for standing up the GQL server & DB and running API tests.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors