2

我对 GraphQL 和石墨烯完全陌生,我刚刚完成了graphene_django 教程

我了解如何从服务器获取数据,这很容易,但我不知道如何创建或更新

我需要使用 django rest 框架进行 POST 还是可以只使用石墨烯来获取和放置数据?

4

1 回答 1

2

要在 graphQL 中创建或编辑对象,您必须使用称为突变的东西,有关更多信息以及如何使用它,请从 graphQL 页面阅读此内容

现在在 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

于 2017-10-04T04:47:04.243 回答