保存用户对象以发布Graphql / Apollo Vue

时间:2018-01-07 03:34:30

标签: django vue.js graphql apollo apollo-client

我正在尝试将用户ID保存到新的商家。我一直收到400错误,无法弄清楚原因。我使用django作为后端,使用graphql和apollo客户端作为vue js的前端。我能够获得所有者ID,但由于某种原因无法保存。

创建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 个答案:

没有答案