I am using Graphene-Django and trying to query a mutation.
The client side is like this:
sendMessage = async () => {
const mutation = `mutation sendMessage($text: String!) {
createMessage(text: $text roomSlug: "${this.state.slug}") {
ok
message {
slug
createdDate
text
creator {
firstName
lastName
username
profilePictures {
file
pictureNumber
}
}
}
}
}
`;
const { message, ok } = await apolloClient.mutate(mutation, {text: this.state.text})
.then(result => result.data.createMessage);
The server side is like this:
class CreateMessage(graphene.Mutation):
class Arguments:
text = graphene.String(required=True)
room_slug = graphene.String(required=True)
ok = graphene.Boolean()
message = graphene.Field(MessageType)
def mutate(root, info, text, room_slug):
if info.context.user.is_authenticated is False:
raise PermissionDenied('Login required')
ok = False
room = get_object_or_404(models.Room, slug=room_slug)
message = models.Message(room=room, creator=info.context.user, text=text)
message.save()
ok = True
return CreateMessage(ok=ok, message=message)
I have no idea what things got wrong here. The mutation works when I open a built-in graphql admin view and send it through it. However, it does not work when I send the mutation through the real app and throw this error:
Exception: Received incompatible instance “User Model 1”
So I assumed it should be a client error, but apparently, the error comes from Graphene-Python, which is the server side.
I defintely have both
class MessageType(DjangoObjectType):
class Meta:
model = models.Message
and
class UserType(DjangoObjectType):
class Meta:
model = models.User
UserType is for a creator and MessageType is for a message.
Plus: the UserType works fine in every other queries and mutations. The only place it does not work is this specific mutation.
If anyone runs into this problem, change
message = graphene.Field(MessageType)
to
message = graphene.List(MessageType)
it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With