I need to use the Shopify GraphQL API to create new entries of a metaobject on a Shopify Store and ran into an issue with my graphql request. I’m gonna post the code below and would like to know if you can spot something wrong with it that would stop it from working properly.
In the code you’ll see that I’m trying to create a new entry into a metaobject with the type : “hive_five_certification” using Express JS and the Shopify API Node npm package
Whenever I run my code I get this as a response to my GraphQL Mutation:
{ metaobjectCreate: { metaobject: null } }
Also when I check the store’s metaobjects, there are no new entries
My Code
const express = require('express');
const server = express();
const port = 3000;
require("dotenv").config()
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: process.env.SHOP_NAME,
accessToken: process.env.SHOPIFY_ADMIN_ACCESS_TOKEN
});
const query = `
mutation createMetaobject($metaobject: MetaobjectCreateInput!){
metaobjectCreate(metaobject: $metaobject){
metaobject{
type
handle
title: field(key: "title"){
value
}
qualities: field(key: "qualities"){
value
}
}
}
}
`;
const variables = {
metaobject: {
type: "hive_five_certificatioon",
handle: "clean-packaging-ancient-nutrition",
fields: [
{
"key": "Title",
"value": "Clean Packaging - Ancient Nutrition"
},
{
"key": "Qualities",
"value": "Lorem Ipsum"
}
]
}
}
server.get('/', (req, res) => {
try{
shopify
.graphql(query, variables)
.then((metaobject) => console.log(metaobject))
.catch((err) => console.error(err));
}
catch(err){
console.error(err)
}
res.send('We finished the graphql request check your console');
});
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
For reference here is an image of the metaobject definition
Also please keep in mind I’m pretty new to using GraphQL so I’m not sure what the issue could be.
Hey Patrick!
Hope you’re well
One thing I noticed is that where you’re setting your variables, the “type” property has an extra “o”:
metaobject: {
type: "hive_five_certificatioon",
I believe it should match the defined metaobject type exactly. If it’s not working after that, you could add userErrors
to get more info on what’s failing, eg:
mutation createMetaobject($metaobject: MetaobjectCreateInput!) {
metaobjectCreate(metaobject: $metaobject) {
metaobject {
type
handle
title: field(key: "title") {
value
}
qualities: field(key: "qualities") {
value
}
}
userErrors {
field
message
}
}
}
Let me know if that works.
Hey @Liam-Shopify, thanks for catching that typo.
Unfortunately, that did not resolve the issue and I’m still getting the same response from the Shopify Admin API: { metaobjectCreate: { metaobject: null } }
Is there anything else I should know about or look into when it comes to properly making GraphQL mutations to Shopify?
I only have experiencing doing regular GraphQL queries and have never tried doing mutations.
If you try running just the mutation in the GraphiQL app, you can check if the mutation itself is working okay - if it is working, then it could be something else in code around the mutation that’s blocking this.
Other things to look out for:
- Ensure your app has the required
write_metaobjects
access scope to create metaobjects.
- If the metaobject definition includes the
publishable
capability, the default status for new entries is DRAFT
. You may need to update the status to ACTIVE
using the metaobjectUpdate
mutation.
@Liam-Shopify I found out why my code wasn’t working and I’ll share the solution below for anyone else that may encounter this issue.
There was no issue with my code. The problem had to do with the Metaobject definition. The initial setup was causing some issues and the code was silently failing.
I didn’t find a useful error message until I added this block of code to my GraphQL mutation.
userErrors {
field
message
code
}
So the final version of my GraphQL mutation looked like this:
const query = `
mutation createMetaobject($data: MetaobjectCreateInput!){
metaobjectCreate(metaobject: $data){
metaobject{
type
title: field(key: "title"){
value
}
qualities: field(key: "qualities"){
value
}
}
userErrors {
field
message
code
}
}
}
`;
Once I added in the userErrors code, I saw this error message:
userErrors: [
{
field: [Array],
message: "Value is invalid JSON: unexpected token at 'Lorem Ipsum'.",
code: 'INVALID_VALUE'
}
]
This error message was strange to me because I thought the metaobject value for my “qualities” field accepted a string instead of JSON. And after looking into it a little more, it was expecting an array of values.
So then I took a closer look at the metaobject defintion and it was set to accept a list of single line text values.
I changed the definition to accept a single value instead of a list of values and that fixed the issue.
So the big lesson here is to keep a close eye on the Metaobject defintion you are working with and also add userErrors to GraphQL queries whenever I can.
1 Like
Awesome Patrick - userErrors
is super useful. Hopefully it will be all smooth sailing from here 