14 Temmuz 2021 Çarşamba

GraphQL Mutation - CRUD İşlemleridir

Giriş
POST isteği ile gönderilir. JSON döner. Mutation ile create, update, delete işlemleri yapılır. Mutation bir sonuç dönebilir.
Metodun içine gönderilecek parametreler yazılır, döndürülecek sonuçlar ise süslü parantez içindedir.
Örnek
Şöyle yaparız. Burada name ve country değerleri gönderiliyor. Sonuç olarak yine aynı parametreler isteniyor.
mutation {
  createAuthor(name: 'Brandon Sanderson', country: "USA") {
    name
    country
  }
}
Eğer yeni bir kayıt yaratsaydık ve sonuç olarak id isteseydik şöyle yaparız
mutation {
  createAuthor(name: 'Brandon Sanderson', country: "USA") {
    id
  }
}

Örnek
Şöyle yaparız
mutation {
  project_tracker {
    createTicket(title : "New ticket", author_id : 11824) {
      id
    }
  }
}
Açıklaması şöyle
In our example, the createTicket mutation accepts two arguments for creating a ticket: title and author_id. On ticket creation, we return the $id of the newly created ticket. Just like the query, the mutation is a root object type. Mutations, for the most part, are very flexible and can return whatever you desire: scalars such as int, string, bool, and core types like the Ticket, or even custom response objects. Similar to queries, if the mutation field returns an object type, you can ask for nested fields. 
Örnek
Şöyle yaparız
type Author {
  id: ID!
  firstName: String!
  lastName: String!
}

type Query {
  findAllAuthors: [Author]!
  countAuthors: Long!
}

type Mutation {
  newAuthor(firstName: String!, lastName: String!) : Author!
}

Hiç yorum yok:

Yorum Gönder