Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo
This is demo code for my conference talk GraphQL vs Traditional REST API. It consists of simple REST end points and GraphQL end point, with MongoDB in backened.
https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo
demo graphql graphql-api graphql-java graphql-server java rest rest-api
Last synced: 19 days ago
JSON representation
This is demo code for my conference talk GraphQL vs Traditional REST API. It consists of simple REST end points and GraphQL end point, with MongoDB in backened.
- Host: GitHub
- URL: https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo
- Owner: vladimir-dejanovic
- License: gpl-3.0
- Created: 2018-03-31T14:22:43.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-27T18:49:21.000Z (about 6 years ago)
- Last Synced: 2024-10-18T18:49:49.737Z (28 days ago)
- Topics: demo, graphql, graphql-api, graphql-java, graphql-server, java, rest, rest-api
- Language: JavaScript
- Homepage:
- Size: 388 KB
- Stars: 13
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL vs Traditional REST API with MongoDB - Conference talk demo
This is demo code for my conference talk GraphQL vs Traditional REST API. I had two repos which are combined and improved in this one
- [https://github.com/vladimir-dejanovic/simple-springboot-rest-mongo-conftalk-demo](https://github.com/vladimir-dejanovic/simple-springboot-rest-mongo-conftalk-demo)
- [https://github.com/vladimir-dejanovic/simple-springboot-graphql-mongo-conftalk-demo](https://github.com/vladimir-dejanovic/simple-springboot-graphql-mongo-conftalk-demo)## How to start project
You can build java code and start it as stand alone app. In this case you need to provide MongoDB instance. Other way is to use docker-compose to start all needed dependencies as docker containers.
To build code just run
```
mvn clean package
```to build code and created docker image run
```
mvn clean install
```### Stand alone Java applicatoin
```
java -jar target/graphql-vs-rest-0.0.6-SNAPSHOT.jar
```If you want to also initalize some init data run with additiaonal paramater
```
java -jar target/graphql-vs-rest-0.0.6-SNAPSHOT.jar initdata```
### Using Docker compose
After you build code and docker image is created just run this command
``` bash
docker-compose up
```This will start two docker images. One with MongoDB database, other with Java application
## REST End points
There are multipel REST end points that are exposed
### Authours end point
- [http://localhost:8080/authors](http://localhost:8080/authors)to retrive all authours run this command
```
curl http://localhost:8080/authors
```to retrieve info about specific author run this command
```
curl http://localhost:8080/authors/
```### Posts end point
- [http://localhost:8080/posts](http://localhost:8080/posts)to retrive all posts run this command
```
curl http://localhost:8080/posts
```To retrieve all post from specific author run this command
```
curl http://localhost:8080/posts?author_id=
```### Comments end point
- [http://localhost:8080/comments](http://localhost:8080/comments)to retrive all comments run this command
```
curl http://localhost:8080/comments
```To retrieve comments for specific port run this command
```
curl http://localhost:8080/comments?post_id=
```## GraphQL End point
### GraphQL schema
Check full GraphQl schema here
- https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/blob/master/src/main/resources/schema.graphqls
### Test with GraphiQL
Once application is up and running, hit [http://localhost:8080/](http://localhost:8080/) with browser and you will get GraphiQL UI.
Here you can enter queries and get back response### Basic query
Run this query```
query {
allPosts {
id
title
body
}
}
```Response should be something like this
```
{
"data": {
"allPosts": [
{
"id": "321",
"title": "Who is Radial Edward",
"body": "Edward Wong Hau Pepelu Tivrusky IV (エドワード・ウォン・ハウ・ペペル・チブルスキー4世 Edowādo Won Hau Peperu Chiburusukī 4-sei?), commonly called Ed, colloquially known as Radical Edward and born on January 1, 2058 as Françoise Appledelhi, is a child prodigy skilled in hacking originally from Earth. She is a comically-eccentric teenager around 13 years of age."
},
{
"id": "543",
"title": "Who is Son Goku",
"body": "Son Goku (孫悟空 Son Gokū), born Kakarot (カカロット Kakarotto), is a male Saiyan and the main protagonist of the Dragon Ball meta-series created by Akira Toriyama. Cheerful, tenacious, and also a bit naïve, Goku is a Saiyan originally sent to Earth as an infant with the mission to destroy it. However, an accident alters his memory, causing him to grow up pure-hearted and later become Earth's greatest defender, as well as the informal leader of the Dragon Team. Throughout his life, he trains hard and constantly strives to be the greatest warrior possible and to fight stronger opponents, which has kept the Earth and the Universe safe from destruction many times"
}
]
}
}
```### Query filtering fields in response
Great thing about GraphQL is that you can select which fields should be present in response
run this query
```
query {
allPosts {
id
title
}
}
```and response should be like this
```
{
"data": {
"allPosts": [
{
"id": "321",
"title": "Who is Radial Edward"
},
{
"id": "543",
"title": "Who is Son Goku"
}
]
}
}
```### Get Author data in response
Since now Author is part of Post in schema, we can request this info in our query
```
query {
allPosts {
title
createdBy {
name
}
}
}
```should produce this response
```
{
"data": {
"allPosts": [
{
"title": "Who is Radial Edward",
"createdBy": {
"name": "Ed Wong IV"
}
},
{
"title": "Who is Son Goku",
"createdBy": {
"name": "Son Goku"
}
}
]
}
}
```### Modify data throgh GraphQL
Now that Mutation is added to graphql schema, authers can be added and removed
#### Add Author
to add author run this query in GraphiQL
```
mutation {
addAuthor(name:"New Author") {
id
name
}
}
```response should be something like this
```
{
"data": {
"addAuthor": {
"id": "5ac229d4857aba00078c565f",
"name": "New Author"
}
}
}
```#### Remove Author
to remove auhtour run command like this
```
mutation {
removeAuthour(id: "123") {
name
}
}
```response should be like this
```
{
"data": {
"removeAuthour": {
"name": "Ed Wong IV"
}
}
}
```## Branches
### rest-0.1
REST points done
- [https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/rest-0.1](https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/rest-0.1)### graphql-0.1
GraphQL end point with init GraphQL schema
- [https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.1](https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.1)### graphql-0.2
Updated GraphQL schema, so that Author can be accessed through Post
- [https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.2](https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.2)### graphql-0.3
Full GraphQL schema, with all resources connected were made sense
- [https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.3](https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.3)
### graphql-0.4
Added mutations to GraphQL, option to add and remove authors
- [https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.4](https://github.com/vladimir-dejanovic/graphql-vs-traditional-rest-api-conftalk_demo/releases/tag/graphql-0.4)