我正在尝试将用户 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)
})
}
}