保存用户Graphql Apollo Client

时间:2018-01-08 23:52:43

标签: javascript django graphql apollo graphene-python

我正在尝试将用户ID保存到新的商家。我一直收到400错误,无法弄清楚原因。我使用django作为后端,使用graphql和apollo客户端作为vue js的前端。根据我的请求发送用户ID但由于某种原因抛出400错误的请求错误。

创建Biz Mutation Apollo

export const CREATE_BIZ_MUTATION = gql`

    mutation CreateBizMutation($name: String!, $owner: ID!) {
      createBiz(name: $name, ownerId: $owner) {
        name

      }
    }`

创建Biz变异Django

class CreateBiz(graphene.Mutation):
    id = graphene.Int()
    name = graphene.String()
    code = graphene.String()
    owner = graphene.Field(UserType)


    class Arguments:
        name = graphene.String()


    def mutate(self, info, name):
        user = get_user(info) or None
        code = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(6))
        biz = Biz(
            code = code,
            name = name,
            owner = user
        )

        biz.save()

        return CreateBiz(
            id= biz.id,
            name = biz.name,
            code = biz.code,
            owner = biz.owner
        )

创建商务组件

createBiz () {
        const owner = localStorage.getItem(DJANGO_USER_ID)
        if (!owner) {
            console.error('No user logged in')
            return
        }
        const { name } = this.$data
        this.$apollo.mutate({
            mutation: CREATE_BIZ_MUTATION,
            variables: {
                name,
                owner
            }
        }).catch((error) => {
                    console.log(error)
                    })
        }
    }

0 个答案:

没有答案