Skip to content

A dockerized graphQL api created with .NET (Hot Chocolate) with mongoDB

Notifications You must be signed in to change notification settings

ScandiumSG/gqlBackend

Repository files navigation

GraphQL API

This repository contains some brief implementation of a graphQL based API.

Built with

C# .Net

GraphQL implemented using HotChocolate.

How to run

(Requires Docker dekstop) Down the project and navigate to the project root, then run the command

docker compose down; docker compose build --no-cache; docker compose up -d

This Docker compose command will build and run the project backend aswell as a mongodb database.

The server will then be available at the address: localhost:8080/graphql

Data

The data is not connected to anything at the moment, its just 3 movies with 3 actors to test out queries.

Data details
Movies
Movie 1 { name: "Movie A" description: "Something happens!" year: 1980, actors: [actor1, actor2] }
Movie 2 { name: "Movie B" description: "Something happens?" year: 2000, actors: [actor2, actor3] }
Movie 3 { name: "Movie C" description: "Something happens." year: 2020, actors: [actor1, actor3] }
Actors
Actor 1 { name: "Actor A" age: 42 }
Actor 2 { name: "Actor B" age: 62 }
Actor 3 { name: "Actor C" age: 22 }

Query examples

GraphQL fetches data differently than REST APIs, here are some examples:

Retrieve all movies, return name and description for each

Query
{
  movie {
    name
    description
  }
}
Response
{
  "data": {
    "movie": [
      {
        "name": "Movie A",
        "description": "Something happens!"
      },
      {
        "name": "Movie B",
        "description": "Something happens?"
      },
      {
        "name": "Movie C",
        "description": "Something happens."
      }
    ]
  }
}

Retrieve all movies with name, and include all actors by name associated with the movie.

Query
{
  movie {
    name
    description
  }
}
Response
{
  "data": {
    "movie": [
      {
        "name": "Movie A",
        "actors": [
          {
            "name": "Actor A"
          },
          {
            "name": "Actor B"
          }
        ]
      },
      {
        "name": "Movie B",
        "actors": [
          {
            "name": "Actor B"
          },
          {
            "name": "Actor C"
          }
        ]
      },
      {
        "name": "Movie C",
        "actors": [
          {
            "name": "Actor A"
          },
          {
            "name": "Actor C"
          }
        ]
      }
    ]
  }
}

Sort movie based on release year descending

Query
{
  movie (order: {year: DESC}){
    name
    year
  }
}
Response
{
  "data": {
    "movie": [
      {
        "name": "Movie C",
        "year": 2020
      },
      {
        "name": "Movie B",
        "year": 2000
      },
      {
        "name": "Movie A",
        "year": 1980
      }
    ]
  }
}

Filter based on movie year, less (earlier) than 1990 or greater (later) than 2010

Query
{
    movie (
        where: {or: [{year: {lt: 1990}}, {year: {gt: 2010}}]}
        order: [{year: ASC}]
    ) {
        name
        year
        actors (order: [{age: DESC}]) {
            name
            age
        }
    }
}
Response
{
  "data": {
    "movie": [
      {
        "name": "Movie A",
        "year": 1980,
        "actors": [
          {
            "name": "Actor B",
            "age": 62
          },
          {
            "name": "Actor A",
            "age": 42
          }
        ]
      },
      {
        "name": "Movie C",
        "year": 2020,
        "actors": [
          {
            "name": "Actor A",
            "age": 42
          },
          {
            "name": "Actor C",
            "age": 22
          }
        ]
      }
    ]
  }
}

Mutation examples

Changing data in graphQL is done using mutations, here are some examples:

Creating new movie, return all information regarding the movie added

Mutation
mutation ($movie_name: String!, $year: Int!) {
  createMovie (
    input: {movie: 
      {name: $movie_name, description: "Nothing happened", year: $year, 
        actors: [{name: "Actor A", age: 25}, {name: "Actor B", age: 30}, {name: "Actor C", age: 35}]
      }
    }) {
    movie {
      id
      name
      description
      year
      actors {
        name
        age
      }
    }
  }
}
Variables example
{
  "movie_name": "Movie about something",
  "year": 2222
}
Response
{
  "data": {
    "createMovie": {
      "movie": {
        "id": "6628bba84810ce245f23db4b",
        "name": "Movie about something",
        "description": "Nothing happened",
        "year": 2222,
        "actors": [
          {
            "name": "Actor A",
            "age": 25
          },
          {
            "name": "Actor B",
            "age": 30
          },
          {
            "name": "Actor C",
            "age": 35
          }
        ]
      }
    }
  }
}

About

A dockerized graphQL api created with .NET (Hot Chocolate) with mongoDB

Topics

Resources

Stars

Watchers

Forks