REST API

Microgen also give You SuperrrrPower to use REST API in case You hate GraphQL (I hope not). To use REST API on Microgen is pretty easy, it actually reflect the GQL query, its not that different.

Setup

You don't necessarily setup anything to work with API, simply RUN your Microgen App, then instead use "/graphql", change it to "/api".

So, on above example, your API will be lived on https://dev-myproject5bjqg.microgen.id/api. In this documentation let say the url is "/api" to make it sort.

Authentication

Authentication is simply an POST method which is used on Register and Login.

Register

to Register using API, you can simply do this.

[POST]
/api/register

body: {
    "email": "you@mail.com",
    "password": "secret",
    "firstName": "Your First Name"
}

Login

to Logging in using API, you can simply do this.

[POST]
/api/login

body: {
    "email": "you@mail.com",
    "password": "secret"
}

GET

You can do various query here using REST API. You also have the power from Microgen GraphQL query here.

Read

for example We want to read data from posts with custom query

#ON GRAPHQL

query {
  posts(
    where: {
      text_contains: "x"
    }
    limit: 10
    skip: 1
    orderBy: id_ASC
  ){
    text
    createdBy{
      firstName
    }
  }
}
#ON REST API

[GET]
/api/posts
?where[text_contains]=x
&limit=10
&skip=1
&orderBy=id_ASC
&select=text,createdBy

On above example, You can see that there is no much difference between Graphql Version and the REST API version.

Read with Multiple Filters

Here is simple example about how to use AND queries. Query all Post nodes that are published and whose title is in a given list of strings:

#ON GRAPHQL 

query {
  posts(
    where: {
      title_in: "Dream Big",
      published: true
    }
  ){
    text
    createdBy{
      firstName
    }
  }
}
#ON REST API

[GET]
/api/posts
?where[title_in]=Dream Big
&where[published]=true
&select=text,createdBy

Here is simple example about how to use OR + AND queries. Query all Post nodes that are either published and whose title is in a list of given strings, or have the tags_in we supply:

#ON GRAPHQL 

query {
  posts(
    or: [
      {tags_in: "news"},
      {
        title_in: "Dream Big",
        published: true
      }
    ]
  ){
    text
    createdBy{
      firstName
    }
  }
}
#ON REST API

[GET]
/api/posts
?or[0][tags_in]=news
&or[1][title_in]=Dream Big
&or[1][published]=true
&select=text,createdBy

Detail

No need explanation Right? :D

#ON GRAPHQL

query {
    post(
        id: "5e1d6c171cfd284c590ae97c"
    ){
        id
        text
    }
}
#ON REST API

[GET]
/api/posts/5e1d6c171cfd284c590ae97c

POST

the Create mutation on Gql is equal with POST method on REST API, so You can do almost every Gql Create Mutation on REST API.

# ON GRAPHQL

mutation {
  createPost(input: {
    text: "post 4"
  }){
    id
    text
  }
}

Header: {
  "Authorization": "Bearer YourGeneratedTokenHERE"
}
#ON REST API

[POST]
/api/posts

body: {
	"text": "post x"
}

Header: {
  "Authorization": "Bearer YourGeneratedTokenHERE"
}

PATCH

the Update mutation on Gql is equal with PATCH method on REST API, so You can do almost every Gql Update Mutation on REST API.

# ON GRAPHQL

mutation{
  updatePost(
    id: "5e1d6c171cfd284c590ae97c",
    input: {
      text: "post xxx"
    }
  ){
    id
    text
  }
}

Header: {
  "Authorization": "Bearer YourGeneratedTokenHERE"
}
#ON REST API

[PATCH]
/api/posts/5e1d6c171cfd284c590ae97c

body: {
	"text": "post xxx"
}

Header: {
  "Authorization": "Bearer YourGeneratedTokenHERE"
}

DELETE

the Delete mutation on Gql is equal with DELETE method on REST API, so You can do almost every Gql Delete Mutation on REST API.

# ON GRAPHQL

mutation {
  deletePost(id: "5e1d6c171cfd284c590ae97c"){
    id
  }
}
# ON REST API

[DELETE]
/api/posts/5e1d6c171cfd284c590ae97c

Header: {
  "Authorization": "Bearer YourGeneratedTokenHERE"
}

Last updated