尝试突变/创建新对象时,使用graphene_django生成“未知类型\” CreateAccountInput \”

时间:2019-01-07 03:05:36

标签: python django graphql graphene-python

我是graphQL的新手,我一直在尝试用它创建一个新的帐户对象。这是我现在的代码,但是它是graphQL的GUI,我一直收到“未知类型\” CreateAccountInput”错误。

schema.py

from authentication.models import Account
import graphene
from graphene import relay, ObjectType
from graphene_django import DjangoObjectType
from graphene import InputObjectType

class AccountGraphQL(DjangoObjectType):
    class Meta:
        model = Account
        interfaces = (relay.Node, )

class Query(graphene.ObjectType):
    username = graphene.String(argument=graphene.String(default_value="stranger"))
    email = graphene.String(argument=graphene.String(default_value="stranger"))
    password = graphene.String(argument=graphene.String(default_value="stranger"))
    first_name = graphene.String(argument=graphene.String(default_value="stranger"))

    info = graphene.String()

    def def_resolve_create_account(self, info, argument):
        return account.objects.create_user()

class CreateAccountInput(InputObjectType):
    username = graphene.String(argument=graphene.String(default_value="stranger"))
    email = graphene.String(argument=graphene.String(default_value="stranger"))
    password = graphene.String(argument=graphene.String(default_value="stranger"))
    first_name = graphene.String(argument=graphene.String(default_value="stranger"))


class CreateAccount(relay.ClientIDMutation):

    class Input:
        account = graphene.Argument(CreateAccountInput)

    new_account = graphene.Field(AccountGraphQL)

    @classmethod
    def mutate_and_get_payload(cls, args, context, info):
        account_data = args.get('account') # get account data from args
        account = Account()
        new_account = update_create_instance(account, account_data)

        return cls(new_account=new_account)

class Mutation(ObjectType):
    create_account = CreateAccount.Field()

schema = graphene.Schema(query=Query)
result = schema.execute('{ create_account (argument: "graphql"')

我的突变:

mutation CreateNewAccount($input: CreateAccountInput!) {
  createAccount(input: $input) {
    newAccount {
      username
      email
      password
      first_name
    }
    clientMutationId
  }
}

我的查询

{"input": {"account": 
  {
    "username": "graphql",
    "email": "graphql@gmail.com",
    "password": "graphql",
    "first_name":"charles"
  }
}}

我不确定这是什么问题,就我所担心的我已经定义了输入类型

1 个答案:

答案 0 :(得分:0)

要使用graphQL注册用户,可以使用django-graphql-auth

这是docs的演示:

它涵盖了用户注册和帐户验证

(在新标签上打开视频)

Demo Video

我写了这个包裹,有任何问题,和我谈谈。