我对GraphQL和石墨烯完全陌生,而且我刚刚完成了graphene_django tutorial
我知道如何从服务器获取数据,这很容易,但我不知道如何创建或更新
我是否需要使用django rest框架来发布帖子,或者是否可以只使用石墨烯来获取和放置数据?
发布于 2017-10-04 12:47:04
要在graphQL中创建或编辑对象,您必须使用称为突变的东西,有关更多信息以及如何使用它,请阅读this from graphQL page。
现在,在Django中,您有一个名为schema.py的程序,您可以在其中放置类查询。在这里,我们还放置了突变类来创建我们的突变,就像我们创建查询一样。你的问题太宽泛了,所以我给你留了一个教程,解释如何在Django中使用突变。但应该是这样的:
class CreateMessageMutation(graphene.Mutation):
class Input:
message = graphene.String() #Parameters to create our model
message = graphene.Field(MessageType) #This field is required to show our message
@staticmethod
def mutate(root, info, **kwargs):
message = args.get('message', '').strip()
obj = models.Message.objects.create(message=message)
return CreateMessageMutation(status=200, message=obj)
#Here we return the object to show what we have created
class Mutation(graphene.AbstractType):
create_message = CreateMessageMutation.Field()和另一个schempa.py:
class Query(ingredients.schema.Query, graphene.ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass
class Mutation(ingredients.schema.Mutation, graphene.ObjectType):
pass
schema = graphene.Schema(query=Query, mutation=Mutation)https://github.com/mbrochh/django-graphql-apollo-react-demo#add-mutation
https://stackoverflow.com/questions/45506777
复制相似问题