【发布时间】:2021-10-04 14:56:07
【问题描述】:
我有一个匿名用户的购物车,并希望将购物车通过突变传递到数据库中。我创建了一个测试,但不能一次传递多个值。我最终会遇到类似的错误......
[GraphQL error]: Message: Variable "$id" got invalid value ["87b7dsfg6ds78ga", "36b34b5hb5hjbj23b", "36b34b5hb5hjbj23b"]; ID cannot represent value: ["87b7dsfg6ds78ga", "36b34b5hb5hjbj23b", "36b34b5hb5hjbj23b"],
这是我的突变:
const ADD_TO_CART_MUTATION = gql`
mutation ADD_TO_CART_MUTATION($id: ID!) {
addToCart(productId: $id) {
id
}
}
`;
export default function SendToCartButton() {
var cartItems = useReactiveVar(cartItemsVar);
console.log(cartItems)
const [sendToCart] = useMutation(ADD_TO_CART_MUTATION, {
variables: { id: cartItems },
refetchQueries: [{ query: CURRENT_USER_QUERY }],
});
return (
<div className="add-to-cart-button" >
<CartButton onClick={sendToCart}>
Send To Cart db
</CartButton>
</div>
);
}
我已尝试将突变配置为 [ID]!但是得到:
[GraphQL error]: Message: Variable "$id" of type "[ID!]" used in position expecting type "ID"., Location: [object Object],[object Object], Path: undefined
谁能解释一下我需要做什么?
谢谢!
【问题讨论】:
-
谢谢@Manny。那是传递多个值,但我的新错误是:
Error: Variable "$where" got invalid value { in: ["87b7dsfg6ds78ga", "87b7dsfg6ds78ga"] } at "where.AND[1].product.id"; ID cannot represent value: { in: ["87b7dsfg6ds78ga", "87b7dsfg6ds78ga"] } -
您已经包含了您调用自定义突变的代码,但没有包含定义突变本身的代码。你在哪里给
extendGraphqlSchema打电话? -
万一有人在寻找它——目前,关于
extendGraphqlSchema()功能的最佳文档在演示它的示例项目中:github.com/keystonejs/keystone/tree/master/examples/… -
嗨,最后我在前端使用了这个:
const … mutation ADD_TO_CART_MUTATION($id: [ID]!) { sendToCart(productIds: $id) { …,在后端使用:async function sendToCart( root: any, { productIds }: { productIds: string[] }, context: KeystoneContext ): Promise<void> { const sesh = context.session; const quantities = [] productIds.forEach(function (x) { quantities[x] = (quantities[x] || 0) + 1; }); { Object.entries(quantities).map(async ([key, value]) => { // rest of code …
标签: arrays reactjs cart graphql-js keystonejs