Error while executing Magento Graphql query from NodeJS. | Community
Skip to main content
Nikhil-Kumar
Community Advisor
Community Advisor
September 28, 2020
Question

Error while executing Magento Graphql query from NodeJS.

  • September 28, 2020
  • 5 replies
  • 2215 views

Hello everyone,

I want to get the result in my Node JS when I run a specific query on the magento server.
I have used the Apollo libraries to hit the Magento endpoint, but getting below error:

"message": "Encountered a sub-selection on the query, but the store doesn't have an object reference. This should never happen during normal use unless you have custom code that is directly manipulating the store; please file an issue.",


Below is the code snippet that I am using:

const gql = require("graphql-tag");
const ApolloClient = require("apollo-client").ApolloClient;
const fetch = require("node-fetch");
const createHttpLink = require("apollo-link-http").createHttpLink;
const InMemoryCache = require("apollo-cache-inmemory").InMemoryCache;

const httpLink = createHttpLink({
    uri: 'http://localhost:3001/graphql',
    fetch: fetch
  });
   
  const client = new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache()
  });


var queryField = `query {
        countries {
            id
            available_regions {
                name
            }
        }
    }
    
    `;
    
    const query = gql(queryField);

    var variables = {};

        const result = client.query({
          query,
          variables
        });


@joerghoh @arunpatidar  @vanegi @mhaack  Can you guys provide some inputs ?


This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

5 replies

September 28, 2020

This comment is deleted

September 28, 2020

Hi,

 

This is error is related to the Apollo Client (see details here). I tried the same code with Apollo Client v3 and it worked just fine, I guess you are still using version 2

 

Hope this helps,

Daniel

Nikhil-Kumar
Community Advisor
Community Advisor
September 28, 2020
Thanks Daniel,
Nikhil-Kumar
Community Advisor
Community Advisor
September 28, 2020

@dplatonThese are the dependency I am using : "apollo-client": "2.0.4", "apollo-link": "1.1.0", "apollo-link-http": "1.3.3",

What are the exact version you are using for client ? 
Also are you using react or node ?

 

September 28, 2020

I used Apollo Client 3.0. It did require adding React as a dependency but I'm not sure why. Looking briefly around the internet it seems that while you can use Apollo Client directly in Node (using just Apollo Link) it's not the recommended approach since you don't get the caching and all the rest of the goodies you get from the browser (relevant SO thread here)