0

I'm attempting to manipulate this data in react with graphql. As you can see, I have this data or input in the graphql playground, and this is how I wanted the input to look:

enter image description here

In my frontend, I have a cartItem with an objects inside and array, including the product name, id, and so on. I wanted the input to look like the example I provided above. Is there a way to make that happen?

Codes and Data


This is how my cart's Item Data looks.

CartItem Data:

[
  {
    id: "6109401fd86d352a70e3694e",
    name: "asasasasa",
    sku: "sasa",
    shippingTime: "1628812800000",
    quantity: 1,
  },
  {
    id: "61051c14f25d8830a8e238c0",
    name: "Pringles Sour Cream & Onion Potato Crisps 158g",
    sku: "sad89f79dsafs",
    shippingTime: "1627084800000",
    quantity: 1,
  },
];

As stated in the preceding example, all I wanted was the product's id and quantity.

Order.js

const [cartItems, setCartItems] = useContext(CartContext);

const [createOrder, { data, loading }] = useMutation(CREATE_ORDER_MUTATION);

const qty = cartItems.map(({ quantity }) => {
  return quantity;
});

const cartItemId = cartItems.map(({ id }) => {
  return id;
});


function onSubmit() {
  createOrder({
    variables: {
      qty: qty,
      products: cartItemId,
      paymentMethod: paymentMethod,
      address: address,
    },
  })
}

Whenever I need to console someone. If you log the cartItemId, you'll get something like this: enter image description here

Same goes with my qty.

Please let me know if you have any other questions or require any additional code, and I will gladly offer it.

Apollo Mutation:

const CREATE_ORDER_MUTATION = gql`
  mutation createOrder(
    $qty: Int!
    $products: String!
    $paymentMethod: String!
    $address: String!
  ) {
    createOrder(
      orderedItems: [{ qty: $qty, products: $products }]
      paymentMethod: $paymentMethod
      address: $address
    ) {
      id
      orderedItems {
        qty
        products {
          id
          name
          sku
          description
        }
      }
    }
  }
`;

1 Answer 1

1

The code below will transform the cartItems into the desired result. You can loop through the cartItems and create an object with the required structure for each item.

const orderedItems = cartItems.map(({ id, quantity }) => {
  return {
    qty: quantity,
    products: id,
  };
});

Complete code will look something like this

const [cartItems, setCartItems] = useContext(CartContext);
const [createOrder, { data, loading }] = useMutation(CREATE_ORDER_MUTATION);

// Restructure the array to desired format
const orderedItems = cartItems.map(({ id, quantity }) => {
  return {
    qty: quantity,
    products: id,
  };
});

console.log(orderedItems); // To check if it looks correct

function onSubmit() {
  createOrder({
    variables: {
      orderedItems: orderedItem,
      paymentMethod: paymentMethod,
      address: address,
    },
  })
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hello thanks for answering, ok will try it
seems what I want but can u please explain what do you mean by loop you mean to map it again? or something.
Could you please make it fully with the loop and stuff? if possible
I've edited the answer to include a more complete example. map function is already a loop. You have some data in an array, called cartItems, but it's not in the correct format for the API call. So, you loop over it by using the map function to return a new structure for each item.
Hello yes I tried that already it says qty and product is not provided the data
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.