I'm completely new to GraphQL and graphene and i just finished the graphene_django tutorial
I understand how to get data from server, which is pretty easy, but i don't know how to do create or update
do i need to use django rest framwork for POSTs or is it possible to use just graphene to get and put data?
答案 0 :(得分:1)
要在graphQL中创建或编辑对象,您必须使用称为变异的内容,以获取更多信息以及如何使用它阅读this from graphQL page。
现在在Django中你有一个名为schema.py的东西,你可以放置你的Class查询。在这里,我们还使用变异类来创建我们的突变,就像我们创建了查询一样。你的问题太宽泛,所以我给你一个教程,解释如何使用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