2017-08-04 118 views
0

我完全新的GraphQL和石墨烯和我刚刚完成了graphene_django tutorial如何使用graphene_django发布数据?

我知道如何从服务器获取,这是很容易的数据,但我不知道该怎么做创建或更新

我是否需要使用django休息framwork的POST或是否有可能使用石墨烯来获取和放置数据?

+0

我也做了突变:

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:但是应该像呢? –

回答

0

要在graphQL中创建或编辑对象,您必须使用称为突变的东西,以获取更多信息以及如何使用它读取this from graphQL page

现在在Django中,您可以使用schema.py来放置您的Class查询。在这里,我们还将突变类用于创建我们的突变,就像我们创建查询一样。你的问题太广泛了,所以我给你解释了如何在Django中使用突变的教程。 (插入)如何更新现有数据使用突变

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

+0

如何使用突变更新现有数据? –