Django graphen создает ошибку запуска Mution

#django #graphql #graphene-python #graphene-django

#django #graphql #graphene-python #graphene-django

Вопрос:

Это мои schmema

 import graphene
from graphene import relay, ObjectType, Mutation
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from cookbook.models import Ingredient



class IngredientType(DjangoObjectType):
    class Meta:
        model = Ingredient

class IngredientCreate(Mutation):
    class Arguments:
        name = graphene.String(required=True)
        note = graphene.String(required=True)
        id = graphene.ID()

    ingredient = graphene.Field(IngredientType)

    @classmethod
    def mutate(cls, root, info, name, note, id):
        ingredient = IngredientType.objects.create(
            name = name,
            note = note
        )
        return IngredientCreate(ingredient=ingredient)



class Query(graphene.ObjectType):
    create_ingredient = IngredientCreate.Field()
 

и это мои модели.

 class Ingredient(models.Model):
    name = models.CharField(max_length=100)
    notes = models.TextField()
 

Я пытаюсь создать ингредиент из графического интерфейса graphql django, но выдает синтаксическую ошибку

 {
  createIngredient(
    name: 'rice cooking',
    note: 'a simple note',
  )
}
 

введите описание изображения здесь

Может кто-нибудь сказать мне, пожалуйста, какова возможная причина, по которой я не могу создать запись?

Ответ №1:

В graphql вам нужно использовать двойные кавычки для строковых значений, а не одинарные кавычки. Итак, если вы это сделаете, все должно работать:

 {
  createIngredient(
    name: "rice cooking",
    note: "a simple note",
  )
}