'Shopify read files from the backend

I am trying to read the files that I have uploaded in shopify backend with the help of this code and library:

const shopify = new Shopify({
  shopName: process.env.STORE_NAME,
  apiKey: process.env.API_KEY,
  password: process.env.STORE_PASSWORD,
});
const query = `{
  files {
    edges {
      node {
        alt
      }
    }
  }
}`;
shopify
  .graphql(query)
  .then((files) => console.log(files))
  .catch((err) => console.error(err));

But I am running into this error

Error: Field 'files' doesn't exist on type 'QueryRoot'
 at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  locations: [ { line: 2, column: 7 } ],
  path: [ 'query', 'files' ],
  extensions: { code: 'undefinedField', typeName: 'QueryRoot', fieldName: 'files' },
  response: <ref *1> PassThrough 
}

Please if anyone can help me I will really appreciate it. Thank you!



Solution 1:[1]

UPDATE: You're not using the official Shopify library. The real problem you have is the library you use currently doesn't support Shopify files queries.

You should use @shopify/shopify-api to have access to the last updates on APIs direct from Shopify. The rest of my response is based on using the official library to connect with Shopify graphql file


The error msg is wrong. The trouble with your query is a base rule on all shopify graphql API "you can't ask all elements in one shot using graphql", you should paginate and/or at least declare how many first or last elements you like to receive".

Use this:

{
  files(first:250) {
    edges {
      node {
        alt
      }
    }
  }
}

If you really really need all, Exist an asynchronous way using the bulkOperationRunQuery.

EXTRA: I prefer to formulate my graphql operations on Postman first, the msg errors are clear, I can concentrate on writing good graphql, and when I am happy with the result, I apply it to my code. It helps me a lot to accelerate the development.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1