BURGERFLIPPER101
BURGERFLIPPER101

Reputation: 1332

GraphQL queries must be strings

I am writing a data fetching service on an Express backend. It needs to fetch data from a graphQL endpoint.

I get the following error. I know it's descriptive of the issue but I don't understand it.

'GraphQL queries must be strings. It looks like you\\'re sending the internal graphql-js representation of a parsed query in your request instead of a request in the GraphQL query language. You can convert an AST to a string using the `print` function
from `graphql`, or use a client like `apollo-client` which converts the internal representation to a string for you.' }

This is the function I am using:

fetchMultipleProducts(first : Number, offset : number){
        fetch({
            query: gql`
            query {
                getProduct(query: {}, first : ${first}, offset : ${offset}) {
                    id
                    code
                }
                }
              `
        })
        .then(res => {
            Logger.info("Fetched data");
            console.log(res);
            return res;
        })
        .catch(err => {
            Logger.error("Failed to fetch", err);
        });   

I am trying to pass in variables into it, I assume that's allowed? And using the Gql tag is standard?

Some help would be appreciated, thanks guys.

Upvotes: 1

Views: 3808

Answers (1)

BURGERFLIPPER101
BURGERFLIPPER101

Reputation: 1332

I removed the Gql tag and sent a string as instructed in the error message. Apologies for my silliness.

Upvotes: 1

Related Questions