【问题标题】:How do I change my mutation to accept an array of IDs instead of a single ID?如何更改我的突变以接受 ID 数组而不是单个 ID?
【发布时间】: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&lt;void&gt; { const sesh = context.session; const quantities = [] productIds.forEach(function (x) { quantities[x] = (quantities[x] || 0) + 1; }); { Object.entries(quantities).map(async ([key, value]) =&gt; { // rest of code …

标签: arrays reactjs cart graphql-js keystonejs


【解决方案1】:

利用[ID!]!

  • 声明输入将是类型 ID 列表,不能有任何空条目,$id 不能为空
const ADD_TO_CART_MUTATION = gql`
  mutation ADD_TO_CART_MUTATION($id: [ID!]!) {
   ...
  }
`;

【讨论】:

    【解决方案2】:

    所以最后我在前端使用了这个:

    const …
      mutation ADD_TO_CART_MUTATION($id: [ID]!) {
        sendToCart(productIds: $id) {
    …
    

    sendToCart({
        variables: {
            id: cartItems,
        },
    });
    

    在后台:

    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 …
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 2016-04-06
      • 2019-06-22
      • 1970-01-01
      • 2019-09-04
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多