2017-08-04 79 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

如何使用突變更新現有數據? –