I want to update a customer metafield (list of dates and times) using GraphQL

I want to update a customer metafield (list of dates and times) using GraphQL

遼太
Shopify Partner
6 1 0

I have a question about updating customer meta fields using GraphQL.

What I want to do

I want to change a customer metafield (content type: date and time list) using GraphQL mutation.

I have listed the necessary elements for this below in list form.

Getting the value of the meta-field
Formatting into a more manageable form (String -> array)
Adding a new timestamp (by doing push() )
Formatting into an appropriate form for the request


Issue
I was able to fetch the value of the meta field using a query, but since it was returned as a String type as shown below, it was difficult to change it, so I managed to change it to an array format.

After that, I pushed a new timestamp and changed it to an appropriate form for the request, but the value is not being updated.

The Current Situation

I believe that it is highly unlikely that access permissions are the cause of the problem, as I am able to update the values of other meta fields (boolean values and one timestamp) without any problems. Personally, I do not think that processes 1-3 above are the problem, and I think the problem is process 4.

Source Code

//Result of getting metafield
"[\"2022-09-29T02:30:00Z\",\"2022-10-02T02:31:36.211Z\"]"

//Transforms an array using replace() and split()
TimeListValue = TimeListValue. Replace("[", "").replace("]", "").replace(/[\"]/g, "").split(",");
//Output result of TimeListValue  Transformation succeeded
[ '2022-09-29T02:30:00Z', '2022-10-05T04:55:13.336Z' ]

//Push the new timestamp into the above array.
let date = new Date()
date = date.toISOString()
TimeListValue. Push(date)
//The process of transforming an array into the appropriate form as a request.
const newArray = TimeListValue.map((arr)=>{
const test = '\"' + arr + '\"'
return test
}
const newTimeList = newArray.join(",\\");

//the output result of newTimeList
"2022-09-29T02:30:00Z\",\"2022-10-05T05:40:19.145Z\"
//mutationのvariable 
const mutatinonTimeVariable = {
  input:{
   id:customerId,//customer id
   metafields: [
    { id:TimeMetafieldId,//metafield id
      value:`"[\\${newTimeList}]"`,},
   ],
},
};

const mutationTime = `
mutation($input:CustomerInput!){
customerUpdate(input:$input){
 customer {
 lastName
 firstName
 metafield(key:"_timeList" namespace:"custom"){
  value
  key
}
}
}
}`;  
//graphql operation using shopify-api-node 1st argument operation description 2nd argument variable
shopify.graphql(mutationTime,mutationTimeVariable);



Development Environment

 

node v16.16.0

shopify-api-node: 3.11.2

I don't think the development environment is relevant, as other metafield values can be updated in the above environment, but I mention this just in case.

If there is any alternative solution to this error, please let me know.

Thank you in advance.

 

P.S. 

I'm using the translation site deepl, so I apologize if the English is not clear.

Replies 0 (0)