简体   繁体   English

GraphQl突变错误结果

[英]GraphQl mutation wrong result

I have some troubles with mutating data within Graphql.我在 Graphql 中的数据变异时遇到了一些麻烦。 I am saving the origin image into my database and then mutating it with Graphql (external).我将原始图像保存到我的数据库中,然后使用 Graphql(外部)对其进行变异。

In the following points in the code I get the correct data在代码的以下几点中,我得到了正确的数据

####################1111111111111111111#################### ####################222222222222222#################### ####################333333333333333#################### ###################111111111111111111############################# ###########222222222222222##################################### ##333333333333333####################

But at point但此时

####################444444444444444444#################### ####################444444444444444444####################

after I mutate the data I am getting wrong image src.在我改变数据后,我得到了错误的图像 src。 It is the edited image src and not the origin src I retrieved from database in my revertImages() function.这是编辑后的图像 src,而不是我在 revertImages() function 中从数据库中检索到的原始 src。

Although I pass the correct variable "newVariable" with correct data, the mutation takes over the mutation function edited data that I had previously passed, but takes over the newVariable data.虽然我用正确的数据传递了正确的变量“newVariable”,但突变接管了我之前传递的突变 function 编辑数据,而是接管了 newVariable 数据。 Do I need to clear the cache maybe?我是否需要清除缓存?

The newVariable data is:新变量数据是:

{
  productId: 'gid://shopify/Product/6166892019882',
    image: {
    altText: '',
      id: 'gid://shopify/ProductImage/23268973543594',
        src: 'https://cdn.shopify.com/s/files/1/0508/3516/1258/products/180622-05-2.jpg?v=1611416719'
  }
}

After mutation the result is:变异后的结果是:

{
  productImageUpdate: {
    image: {
      altText: null,
        id: 'gid://shopify/ProductImage/23268973543594',
          src: 'https://cdn.shopify.com/s/files/1/0508/3516/1258/products/180622-05-2.jpg?v=1611416762'
    },
    userErrors: []
  }
}

Here are my functions:这是我的功能:

const revertImages = async (ctx) => {
  let dataToRevert = ctx.request.body.data;
  const { accessToken } = ctx.session;

  let productsDoc = await Product.find({ productId: { $in: dataToRevert.productId } });

  if (!productsDoc) {
    ctx.throw('Could not find products');
  }
  console.log('####################1111111111111111111####################');
  console.log(productsDoc);
  console.log('####################1111111111111111111####################');
  const res = await revertProductImages(productsDoc, accessToken);

  if (res) {
    console.log('Products reverted');
    ctx.response.status = 200;
  }
}

async function revertProductImages(products, accessToken) {
  console.log('Revert Product Images')
  return new Promise((resolve, reject) => {
    const map = {};
    let updateProduct = null;
    let variables = null;

    products.forEach(async (item) => {
      map[item.productId] = map[item.productId] + 1 || 1;

      variables = {
        "productId": `gid://shopify/Product/${item.productId}`,
        "image": {
          "altText": "",
          "id": `gid://shopify/ProductImage/${item.imageId}`,
          "src": item.originalSrc
        }
      };

      console.log('####################222222222222222####################');
      console.log(variables);
      console.log('####################222222222222222####################');
      updateProduct = await updateProductImage(UPDATE_PRODUCT_BY_ID, variables, accessToken);

      if (updateProduct) {

        const res = await removeProductFromDb(map);

        if (res) {
          resolve(res);
        }
      }

    });
  })
}

async function updateProductImage(queryName, variables, token) {
  console.log('updateProductImage..');

  const newVariable = variables;
  console.log('####################333333333333333####################');
  console.log(newVariable);
  console.log('####################333333333333333####################');
  return new Promise(async (resolve, reject) => {
    let res = null;
    try {
      res = await axios({
        headers: {
          'X-Shopify-Access-Token': token,
        },
        method: 'post',
        data: {
          query: queryName,
          variables: newVariable,
        },
        url: url,
      });
    } catch (err) {
      console.log(err.message);
    }

    if (res) {
      console.log('Image updated ✔️');
      console.log('####################444444444444444444####################');
      console.log(res.data.data);
      console.log('####################444444444444444444####################');
      resolve(res);
    } else {
      reject('Can not update image');
    }
  }).catch((err) => {
    console.log(err);
  });

}

Maybe I didn't understood correctly but here's an answer...也许我没有正确理解,但这里有一个答案......

I cannot find the src property in available fields for this mutation (i'm not talking about the ImageInput but the image object).我在此突变的可用字段中找不到src属性(我不是在谈论 ImageInput 而是在谈论图像对象)。

Can you try with the following request:您可以尝试以下请求:

mutation productImageUpdate($productId: ID!, $image: ImageInput!) {
  productImageUpdate(productId: $productId, image: $image) {
    image {
      id
      originalSrc
      transformedSrc
    }
    userErrors {
      field
      message
    }
  }
}

The docs says:文档说:

originalSrc : The location of the original image as a URL. originalSrc :原始图像的位置为 URL。

transformedSrc : The location of the transformed image as a URL. transformedSrc :转换后图像的位置为 URL。

Maybe what you are expecting is in originalSrc ?也许您期望的是originalSrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM