Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of a delete mutation?

What should be the result of a delete mutation in Graphql? I'm using the graphql-ruby gem. Here's an example of my mutation, but I'm just not sure what I should be returning as the response.

Mutations::Brands::Delete = GraphQL::Relay::Mutation.define do
  name "DeleteBrand"
  description "Delete a brand"

  input_field :id, types.ID

  # return_field ??

  resolve ->(object, inputs, ctx) {
    brand = Brand.find(inputs[:id])
    brand.destroy
  }
end
like image 424
Jimmy Baker Avatar asked May 22 '17 19:05

Jimmy Baker


People also ask

What is the result of a deletion mutation?

A deletion changes the DNA sequence by removing at least one nucleotide in a gene. Small deletions remove one or a few nucleotides within a gene, while larger deletions can remove an entire gene or several neighboring genes. The deleted DNA may alter the function of the affected protein or proteins.

Does a deletion mutation remove DNA?

A deletion mutation is the removal of pieces of the genetic code in the form of DNA. DNA deletion mutations can be detrimental to protein synthesis.

What could be the possible result if one gene is deleted?

Chromosomal Deletions As with duplications, deletions can affect gene dosage and thus the resulting phenotype. Also, the larger the deletion, the more genes are likely to be involved, and the more drastic the resulting defect is likely to be.

Is deletion mutation harmful?

Because an insertion or deletion results in a frame-shift that changes the reading of subsequent codons and, therefore, alters the entire amino acid sequence that follows the mutation, insertions and deletions are usually more harmful than a substitution in which only a single amino acid is altered.


2 Answers

I don't think a clear de facto standard exists as of July, 2017, and I see a lot of differences between implementations (GitHub, Yelp, GraphCool, Shopify).

However, if you look at some of recent GraphQL APIs to come out, there seems to be a common trend. Largely, the input type and response type are specific to the mutation. So for instance, for an updateBrand mutation you might expect an UpdateBrandInput, and return an UpdateBrandPayload response. Notice, the input is not BrandInput, and responding with Brand. Nor would you respond with a scalar boolean (eg. true if successful) or the id of the deleted entity (in the case of a delete mutation). Per this convention, you could have a createBrand mutation, with a CreateBrandInput and a CreateBrandPayload response.

By creating mutation specific input and payload types, you have a lot of flexibility in the fields you expect and respond with. Per deletion, you might have a DeleteBrandPayload response that not only includes shallow (eg. only scalar) fields of the brand, but also other related data (eg. clientMutationId), etc..

To be honest, I think the GraphQL spec gives just enough rope to hang yourself with, so it's smart to look at how some of the big guys are rolling this out.

like image 95
bjunc Avatar answered Oct 02 '22 09:10

bjunc


You can return deleted_id or message. If it is an associated object you can return updated object like below example.

Destroy = GraphQL::Relay::Mutation.define do
name 'DestroyComment'
description 'Delete a comment and return post and deleted comment ID'

# Define input parameters
input_field :id, !types.ID

# Define return parameters
return_field :deletedId, !types.ID
return_field :article, ArticleType
return_field :errors, types.String

resolve ->(_obj, inputs, ctx) {
  comment = Comment.find_by_id(inputs[:id])
  return { errors: 'Comment not found' } if comment.nil?

  article = comment.article
  comment.destroy

  { article: article.reload, deletedId: inputs[:id] }
}

http://tech.eshaiju.in/blog/2017/05/15/graphql-mutation-query-implementation-ruby-on-rails/

like image 42
eshaiju Avatar answered Oct 02 '22 11:10

eshaiju