This repository contains some brief implementation of a graphQL based API.
GraphQL implemented using HotChocolate.
(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
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 }GraphQL fetches data differently than REST APIs, here are some examples:
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."
}
]
}
}
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"
}
]
}
]
}
}
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
}
]
}
}
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
}
]
}
]
}
}
Changing data in graphQL is done using mutations, here are some examples:
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
}
]
}
}
}
}