cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Query / Create / Update / Delete data in the repository with GraphQL

oguimard
MEGA

Query / Create / Update / Delete data in the repository with GraphQL

The REST API based on GraphQL allows to perform all actions of the CRUD :

  • Create
  • Read
  • Update
  • Delete

This mechanism allows to perform a various kind of action into the repository. Read action are performed with the keyword "query" in GraphQL whereas Read/Update/Delete are performed with the "mutation" keyword.

 

All the example below are performed with the ITPM schema.

 

Basics queries

 

Get an object with it's attributes

 

  • For instance the following query will return the name of the applications
Query Result
query app {
  application {
  name
  }
}



{
  "data": {
    "application": [
      { "name": "AA" },
      { "name": "Account Management" }
    ]
  }
}

 

Get an object with it's relations

 

  • For instance the following query will return the applications and their related business process
Query Result
query {
  application {
    id
    name
    businessProcess {
      id
      name
    }
  }
}








{
  "data": {
    "application": [
      {
        "id": "snf5hRn0U91K",
        "name": "AA",
        "businessProcess": []
      },
      {
        "id": "IubjeRlyFfT1",
        "name": "Account Management",
        "businessProcess": [
          {
            "id": "12qog2pV99fG",
            "name": "Financial Reporting"
          }
        ]
      },

 

Basics mutations

 

Create an object

 

  • For instance the following mutation will create a new application. If creation is successful the result returned the created object.
Query Result
mutation {
  createApplication(application:{
    name:"new application"
  }) {
    id
    name
  }
}
{
  "data": {
    "createApplication": {
      "id": "x2wrAYDkWP8H",
      "name": "new application"
    }
  }
}

 

Create an object with a relationship

 

  • For instance the following mutation will create a new application and link it to an existing business process. If creation is successful the result returned the created object.
Query Result
mutation {
  createApplication(application : {
    name: "new app 3"
    businessProcess: {
      action:ADD
      list:[{id:"39cXIxu2HHrI"}]
    }
  }) {
    id
    name
    businessProcess {
      id
      name
    }
  }
}
{
  "data": {
    "createApplication": {
      "id": "Q2wradDkWbGH",
      "name": "new app 3",
      "businessProcess": [
        {
          "id": "39cXIxu2HHrI",
          "name": "Accounting"
        }
      ]
    }
  }
}


 

Updating an object

 

  • For instance the following mutation will update an existing application. If update is successful the result returned the updated object.
Query Result
mutation  {
updateApplication(id:"IubjeRlyFfT1" application:{
  cloudComputing:Cloud_IaaS
}) {
  id
  cloudComputing
}
}
{
  "data": {
    "updateApplication": {
      "id": "IubjeRlyFfT1",
      "cloudComputing": "Cloud_IaaS"
    }
  }
}

 

Delete an object

 

  • For instance the following mutation will delete an existing application. If delete is successful the result returned the number of deleted object.
Query Result
mutation { 
 deleteApplication (id:"snf5hRn0U91K" cascade:false) {
   deletedCount
 } 
}

{
  "data": {
    "deleteApplication": {
      "deletedCount": 1
    }
  }
}

 

 

 

 

 

 

2 Replies
engeh
Super Contributor

Hello

 

Thank you for the article. 

Is it possible when updating or creating an object with a relationship. To search for example an object with the list not trough an id? for example: 

 

      list:[{name:"qwertz"}]

 

Instead of:

      list:[{id:"39cXIxu2HHrI"}]

This would be useful for an interface, because it's not always possible for an external to know the ID of the object in MEGA.

 

Kind regards

 

Hendrik

Hello Hendrik,

 

As the name are not always unique we cannot use them as a mean to connect 2 objects. That's why we use ID.

 

You have 3 ways to connect :

  • internal ID : for existing object that you already know the object
  • external ID : for object that you create and that you have chosen to give an ID to make this type of connection. The ID will be store in the repository
  • temporary ID : same as external ID but won't be store in the repository. this ID cannot be use for future use