Skip to content

How to return a costum error message in graphene? #1160

Answered by danish-wani
aliscie asked this question in Q&A
Discussion options

You must be logged in to vote

@Alkaraawi-Ali-Husham-Shakir

Raising a custom Exception should do the job:

from graphql import GraphQLError

class UpdatePost(graphene.Mutation):
      # owner(added_by)+admin can update and delete
      class Arguments:
            id = graphene.Int(required=True)
            description = graphene.String()
      post = graphene.Field(schema.Posts)
      
      @classmethod
      def mutate(cls, root, info, id, description):
            post = models.Post.objects.get(id=id)
            if (info.context.user == post.added_by):
                  post.description = description
                  post.save()
                  return CreatePost(post=post)
            else:
                 rais…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by zbyte64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1147 on April 14, 2021 19:59.