e1commerce.com
Ask a question
  • New questions
  • With the answers
  • Unanswered
  1. Home
  2. javascript - How can in get an array of Shopify product ids to return a correct response in GraphQl

122 votes
1 answers

javascript - How can in get an array of Shopify product ids to return a correct response in GraphQl

I am trying to get a correct response from a graphQl query of an array of products. My goal is to take this array of products and sort it from news to oldest published date. I have tested it with a simple array of two product gids but i am receiving an error response.

Parse error on "gid://shopify/Product/777229615129" (STRING) at [1, 16]

here is my code:

const token = 'Storefront API custom app token';

const queryPage = JSON.stringify({
  query:`query ($ids : ["gid:\/\/shopify\/Product\/777229615129","gid:\/\/shopify\/Product\/7772297068794"]) {
    nodes(ids:$ids){
      ... on Product{
        id
        title
      }
    }
  }`
});
const storefrontSettingsPage = {
  "async": true,
  "crossDomain": true,
  "url": "https://"+shopname+".myshopify.com/api/2022-07/graphql.json",
  "method": "POST",
  "headers": {
    "X-Shopify-Storefront-Access-Token": token,
    "Content-Type": "application/json"
  },
  "data": queryPage,
};
$.ajax(storefrontSettingsPage).done(function (response) {
  console.log(response);
});

I am still learning to use graphQl, what am I doing wrong.

Thank you for your support.


Undefined asked
2022-10-29


357
votes

Answer

Solution:

Still not one hundred percent sure on how it works, but was able to makes this work using a different type of writing style, and using variables.

const token = 'Storefront API custom app token';

$.ajax({
  url: "https://"+shopname+".myshopify.com/api/2022-07/graphql.json",
  contentType: "application/json",
  type:'POST',
  headers:{
    "X-Shopify-Storefront-Access-Token": token,
    "Content-Type": "application/json"
  },
  data: JSON.stringify({
    query:`query ($ids : [ID!]!) {
      nodes(ids:$ids){
        ... on Product{
          id
          title
        }
      }
    }`,
    variables: {
      ids:["gid://shopify/Product/7772296151290","gid://shopify/Product/7772297068794"]
    }
  }),
}).done(function (response) {
  console.log(response);
});
Undefined answered
2022-10-29
Link to answer


Source

Didn't find the answer?

Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Similar questions

Find the answer in similar questions on our website.

107 javascript - How to add custom textbox field on shopify product page that also shown in cart under product description
2 shopify - How To display title, image etc. from product meta field of type product?
57 javascript - show product title in shopify product-template
254 javascript - How do I return the response from an asynchronous call?
323 arrays - Instead of null, send a message for no value in Javascript
470 liquid - How to no-index specific pages on shopify? Don't want to use "contains" example for shopify, can I just type "="? And what denotes as the handle?
240 Is there a way to get shipping information and available shipping methods on the Product Detail Page in Shopify?
608 python - Most efficient way to transform a JS React app/widget into a public shopify-app on the Shopify store
585 css - Shopify Dawn + Product Reviews App - Remove Star rating on product page, if no reviews
188 node.js - How to get URL of shop in Shopify embeded app?

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.







Latest questions:

818 javascript - Redeclaration of Let error - How to find where it's being reassigned?

882 Can we develop plugins for shopify using MERN stack?

125 javascript - Shopify - Facing issue when creating customer using shopify api

457 javascript - add class on scroll not working in Shopify enviroment

188 json - Adding a specific collection to a specific page in Shopify



© 2021

E-mail: infot@e1commerce.com