Solved

Graphql metaobjects Query does not return anything, what am I doing wrong with my query

Filip-S
Shopify Partner
5 1 0

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

FilipS_0-1692379019336.png

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.

 

Accepted Solution (1)

Filip-S
Shopify Partner
5 1 0

This is an accepted solution.

I forgot the "await" before the client.query for the GET function, and my GET was incorrect. Here is the a working query to get metaobjects in Node.js, since the documentation doesn't have an example for this.

const data = await client.query({
      data:`
          query {
          metaobjects(type:"return_order",first:100){
            nodes{
              id
              type
              displayName
            }
          }
        }`
      });

View solution in original post

Replies 2 (2)

Filip-S
Shopify Partner
5 1 0

If anyone has any advice whatsoever I'd greatly appreciate it.

Filip-S
Shopify Partner
5 1 0

This is an accepted solution.

I forgot the "await" before the client.query for the GET function, and my GET was incorrect. Here is the a working query to get metaobjects in Node.js, since the documentation doesn't have an example for this.

const data = await client.query({
      data:`
          query {
          metaobjects(type:"return_order",first:100){
            nodes{
              id
              type
              displayName
            }
          }
        }`
      });