英文:
GraphQL returns null for update and delete mutation
问题
I am using graphene-django library for integrating GraphQL Schema into my Django application.
我正在使用graphene-django库将GraphQL模式集成到我的Django应用程序中。
I have implemented Queries; get all and get specific, as well as implemented create, update and delete Mutations. All endpoints are working as expected except for the update and delete.
我已经实现了查询; 获取所有数据和获取特定数据,还实现了创建、更新和删除操作。除了 update 和 delete 之外,所有端点都按预期工作。
Here is what my model looks like:
这是我的模型的样子:
COUNTRIES = (
("sierra leone", "Sierra Leone"),
("guinea", "Guinea"),
)
class School(models.Model):
name = models.CharField(_("Name"), max_length=255)
abreviation = models.CharField(_("Abreviation"), max_length=10)
label = models.TextField(_("Label"), max_length=255, blank=True, null=True)
school_id = models.CharField(_("School ID"), max_length=100, unique=True, blank=True, null=True)
adresse_line_1 = models.CharField(_("Adresse Line 1"), max_length=255, blank=True)
adresse_line_2 = models.CharField(_("Adresse Line 2"), max_length=255, blank=True)
city = models.CharField(_("City"), max_length=255, blank=True)
country = models.CharField(max_length=60, choices=COUNTRIES, blank=True, null=True)
phone_number = models.CharField(_("Phone number"), max_length=15)
email = models.EmailField(_("Email"))
website = models.CharField(_("Website"), max_length=50, blank=True)
logo = models.ImageField(_("Logo"), upload_to='logo/', blank=True)
small_logo = models.ImageField(_("Small Logo"), upload_to='logo/', blank=True, null=True)
site_favicon = models.ImageField(_("Favicon"), upload_to='logo/', blank=True, null=True)
And here is the code for my update and delete mutations:
这是我的更新和删除操作的代码:
class SchoolType(DjangoObjectType):
class Meta:
model = School
fields = (
"name",
"abreviation",
"label",
"school_id",
"adresse_line_1",
"adresse_line_2",
"city",
"country",
"phone_number",
"email",
"website",
"logo",
"small_logo",
"site_favicon",
)
interfaces = (graphene.relay.Node,)
convert_choices_to_enum = False
class UpdateSchoolMutation(graphene.Mutation):
school = graphene.Field(SchoolType)
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
name = graphene.String()
abreviation = graphene.String()
label = graphene.String()
school_id = graphene.String()
adresse_line_1 = graphene.String()
adresse_line_2 = graphene.String()
city = graphene.String()
country = graphene.String()
phone_number = graphene.String()
email = graphene.String()
website = graphene.String()
logo = Upload()
small_logo = Upload()
site_favicon = Upload()
@classmethod
def mutate(cls, info, id, **kwargs):
id = int(from_global_id(id)[1])
try:
school = School.objects.get(pk=id)
except School.DoesNotExist:
raise Exception("School does not exist".format(id))
for field, value in kwargs.items():
setattr(school, field, value)
school.save()
return UpdateSchoolMutation(school=school, success=True)
class DeleteSchoolMutation(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
@classmethod
def mutate(cls, info, id, **kwargs):
id = int(from_global_id(id)[1])
try:
school = School.objects.get(pk=id)
except School.DoesNotExist:
raise Exception("School does not exist".format(id))
school.archived = True
school.save()
return DeleteSchoolMutation(success=True)
When I carry out the delete mutation as such:
当我执行删除操作如下:
mutation {
deleteSchool(id: "U2Nob29sVHlwZToy") {
success
}
}
I get the following results;
我得到以下结果:
{
"data": {
"deleteSchool": {
"success": null
}
}
}
The same goes for the update mutation. These are the versions I am using incase if it helps:
同样的情况也适用于更新操作。这是我使用的版本,如果有帮助的话:
django==4.0.8
graphene-django==3.0.0
django-filter==22.1
django-graphql-jwt==0.3.4
graphene-file-upload==1.3.0
英文:
I am using graphene-django library for integrating GraphQL Schema into my Django application.
I have implemented Queries; get all and get specific, as well as implemented create, update and delete Mutations. All endpoints are working as expected except for the update and delete.
Here is what my model looks like:
COUNTRIES = (
("sierra leone", "Sierra Leone"),
("guinea", "Guinea"),
)
class School(models.Model):
name = models.CharField(_("Name"), max_length=255)
abreviation = models.CharField(_("Abreviation"), max_length=10)
label = models.TextField(_("Label"), max_length=255, blank=True, null=True)
school_id = models.CharField(_("School ID"), max_length=100, unique=True, blank=True, null=True)
adresse_line_1 = models.CharField(_("Adresse Line 1"), max_length=255, blank=True)
adresse_line_2 = models.CharField(_("Adresse Line 2"), max_length=255, blank=True)
city = models.CharField(_("City"), max_length=255, blank=True)
country = models.CharField(max_length=60, choices=COUNTRIES, blank=True, null=True)
phone_number = models.CharField(_("Phone number"), max_length=15)
email = models.EmailField(_("Email"))
website = models.CharField(_("Website"), max_length=50, blank=True)
logo = models.ImageField(_("Logo"), upload_to='logo/', blank=True)
small_logo = models.ImageField(_("Small Logo"), upload_to='logo/', blank=True, null=True)
site_favicon = models.ImageField(_("Favicon"), upload_to='logo/', blank=True, null=True)
And here is the code for my update and delete mutations:
class SchoolType(DjangoObjectType):
class Meta:
model = School
fields = (
"name",
"abreviation",
"label",
"school_id",
"adresse_line_1",
"adresse_line_2",
"city",
"country",
"phone_number",
"email",
"website",
"logo",
"small_logo",
"site_favicon",
)
interfaces = (graphene.relay.Node,)
convert_choices_to_enum = False
class UpdateSchoolMutation(graphene.Mutation):
school = graphene.Field(SchoolType)
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
name = graphene.String()
abreviation = graphene.String()
label = graphene.String()
school_id = graphene.String()
adresse_line_1 = graphene.String()
adresse_line_2 = graphene.String()
city = graphene.String()
country = graphene.String()
phone_number = graphene.String()
email = graphene.String()
website = graphene.String()
logo = Upload()
small_logo = Upload()
site_favicon = Upload()
@classmethod
def mutate(self, info, id, **kwargs):
id = int(from_global_id(id)[1])
try:
school = School.objects.get(pk=id)
except School.DoesNotExist:
raise Exception("School does not exist".format(id))
for field, value in kwargs.items():
setattr(school, field, value)
school.save()
return UpdateSchoolMutation(school=school, success=True)
class DeleteSchoolMutation(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
@classmethod
def mutate(self, info, id, **kwargs):
id = int(from_global_id(id)[1])
try:
school = School.objects.get(pk=id)
except School.DoesNotExist:
raise Exception("School does not exist".format(id))
school.archived = True
school.save()
return DeleteSchoolMutation(success=True)
When I carry out the delete mutation as such:
mutation {
deleteSchool(id: "U2Nob29sVHlwZToy") {
success
}
}
I get the following results;
{
"data": {
"deleteSchool": {
"success": null
}
}
}
The same goes for the update mutation. These are the versions I am using incase if it helps:
django==4.0.8
graphene-django==3.0.0
django-filter==22.1
django-graphql-jwt==0.3.4
graphene-file-upload==1.3.0
答案1
得分: 0
错误出在mutate
方法中。mutate
方法在接收自定义用户参数之前需要三个初始参数:cls
、root
和info
。
因此,这个删除Mutation:
class DeleteSchoolMutation(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
@classmethod
def mutate(self, info, id, **kwargs):
# Delete School
应该改为:
class DeleteSchoolMutation(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
@classmethod
def mutate(cls, root, info, id, **kwargs):
# Delete School
英文:
The mistake is in the mutate
method. The mutate method requires 3 initial arguments cls
, root
, and info
before the custom user arguments
Therefore this Delete Mutation:
class DeleteSchoolMutation(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
@classmethod
def mutate(self, info, id, **kwargs):
# Delete School
Would become:
class DeleteSchoolMutation(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
id = graphene.String(required=True)
@classmethod
def mutate(self, root, info, id, **kwargs):
# Delete School
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论