【问题标题】:How can i POST data with graphene_django?如何使用 graphene_django 发布数据?
【发布时间】:2018-01-12 09:22:51
【问题描述】:

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

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

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

【问题讨论】:

  • 我已经完成了突变(插入)如何使用突变更新现有数据?

标签: django graphql graphene-python


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多