2017-08-04 4 views
0

私は完全にGraphQLに新しいグラフェンだと私はちょうどgraphene_django tutorialgraphene_djangoでデータをPOSTするにはどうしたらいいですか?

を終え、私は非常に簡単である、サーバーからデータを取得する方法を理解するが、私は

を作成または更新しますする方法がわかりません

POST用にdjango restフレームワークを使用する必要がありますか、またはグラフェンを使用してデータを取得して配置することは可能ですか?

+0

私は突然変異を使用して既存のデータを更新するためにどのように突然変異(挿入)を行っていますか? –

答えて

0

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

+0

突然変異を使用して既存のデータを更新するにはどうすればよいですか? –

関連する問題