0

这些是我的 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 gui 创建成分,但触发了我的语法错误

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

在此处输入图像描述

谁能告诉我我无法创建记录的可能原因是什么?

4

1 回答 1

1

在 graphql 中,您需要对字符串值使用双引号,而不是单引号。所以,如果你这样做,一切都应该工作:

{
  createIngredient(
    name: "rice cooking",
    note: "a simple note",
  )
}
于 2020-11-28T21:10:05.287 回答