Python石墨烯突变不起作用

时间:2019-01-16 01:18:52

标签: python graphql graphene-python

我也是Graphql和Python的新手。

所以当我开发一个lil项目时,我只是遇到了突变问题:

class User(graphene.ObjectType):
    id = graphene.ID(required=False)
    username = graphene.String()
    last_login = graphene.DateTime(required=False)

class CreateUser(graphene.Mutation):

    class Arguments:
        username = graphene.String()

    user = graphene.Field(User)

    def mutate(self, info, username):
        user = User(username=username)
        return CreateUser(user=user)

class Mutations(graphene.ObjectType):
    create_user = CreateUser.Field()

schema = graphene.Schema(mutation=Mutations)

我正在邮递员打这个电话

  

{       “ mutation”:“ {createUser(用户名:'用户测试'){user {用户名}}}”}

我的标题:

  

Content-Type:application / json

我的回答是

我不知道我的问题是实施还是打电话。

-------------------编辑--------------------------- - 我只是在代码中修改了内容,现在有了一个查询,它可以工作,并且我直接在代码中实现了一个简单的请求,并且它可以工作

我刚刚做到了:

import graphene
import json
from datetime import datetime

class User(graphene.ObjectType):
    id = graphene.ID(required=False)
    username = graphene.String()
    last_login = graphene.DateTime(required=False)


def __populate_users__():
    return [
        User(username='User1 teste', last_login=datetime.now()),
        User(username='User2 teste', last_login=datetime.now()),
        User(username='User3 teste', last_login=datetime.now())
    ]


class Query(graphene.ObjectType):

    users = graphene.List(User, first=graphene.Int())
    all_users = graphene.List(User)

    def resolve_users(self, info, first):
        return __populate_users__()[:first]

    def resolve_all_users(self, info):
        return __populate_users__()

class CreateUser(graphene.Mutation):

    class Arguments:
        username = graphene.String()

    user = graphene.Field(User)

    def mutate(self, info, username):
        print "TESTE"
        user = User(username=username)
        return CreateUser(user=user)

class Mutations(graphene.ObjectType):
    create_user = CreateUser.Field()

schema = graphene.Schema(query=Query, mutation=Mutations)

# IT WORKS!!!
result = schema.execute(
    '''
    mutation {
        createUser(username: "User test") {
            user {
                username
            }
        }
    }
    '''
)

items = dict(result.data.items())
print(json.dumps(items))

因此,当我在Postman中调用查询时,它也可以工作:

  

{       “ query”:“ {allUsers {用户名lastLogin}}”}

标题:

  

Content-Type:application / json

我不明白为什么在Postman中查询有效而突变无效。

0 个答案:

没有答案