I’m new to Graphql and Shopify, and I would like to store data as metaobjects.
I have a return_orders object that is structured as followed
and defined in the following
const DEFINE_MUTATION=`mutation CreateMetaobjectDefinition($definition: MetaobjectDefinitionCreateInput!) {
metaobjectDefinitionCreate(definition: $definition) {
metaobjectDefinition {
name
type
fieldDefinitions {
name
key
}
}
userErrors {
field
message
code
}
}
}`
export async function DefineReturn(session){
const client = new shopify.api.clients.Graphql({session});
const today = new Date();
try{
const data = await client.query({
data:{
query:DEFINE_MUTATION,
variables:{
"definition": {
"access":"MERCHANT_READ_WRITE",
"storefront":"NONE",
"name": "Return Orders",
"type": "return_orders",
"fieldDefinitions": [
{
"name": "Created At",
"key": "created_at",
"type": "date_time",
},
{
"name": "Items",
"key": "line_items",
"type": "list.product_reference"
},
{
"name": "Status",
"key": "status",
"type":"list.single_line_text_field"
},
{
"name": "Show Title",
"key": "show_name",
"type":"single_line_text_field"
}
]
}
}
}
})
}catch(error){
if (error instanceof GraphqlQueryError) {
throw new Error(
`${error.message}\n${JSON.stringify(error.response, null, 2)}` // output error message
);
} else { //if not from GraphqlQueryError, throw a normal error
throw error;
}
}
console.log("DEFINERETURN QUERY SUCCESSFUL");
}
The query is structured as such:
const GET_MUTATION=
`{
metaobjects(type:"return_orders",first:1) {
nodes {
handle
type
created_at: field(key:"created_at") { value }
line_items: field(key:"line_items") { value }
status: field(key:"status") { value }
show_name: field(key:"show_name" ) { value }
}
}
}`
export async function GetReturn(session){
const client = new shopify.api.clients.Graphql({session});
try{
const data = client.query({
data:{
query: GET_MUTATION
}
});
console.log(JSON.stringify(data));
return data;
}catch(error){
if (error instanceof GraphqlQueryError) {
throw new Error(
`${error.message}\n${JSON.stringify(error.response, null, 2)}` // output error message
);
} else { //if not from GraphqlQueryError, throw a normal error
throw error;
}
}
}
I find the documentation on the Shopify queries confusing, and there is very little online about pulling metaobjects using NodeJS and GraphQL. If anyone can tell what I’m doing wrong here, and why it is so, I’d greatly appreciate it.
