'Expected value found [object, Object] Query Params

I don't get it, I sent in the right input type here:

Caller from JS code:

export interface FilterInput {
    limit?: number;
}

const filter: FilterInput = { limit: 1 };
const query = {
    query: `query { 
        companies(filter: ${filter}) {
            list {
                name
            }
        }
    }`,
};
const response = await sendQuery(query);
        

GraphQL Schema:

companies(filter: FilterInput): Companies

input FilterInput {
  limit: Int
}

Error: Expected value of type "FilterInput", found [object, Object].

When I try this out in graphiql in the browser, it works fine:

query {
    companies(filter: { limit: 6 }) {
        list {
            name
        }
    }
}


Solution 1:[1]

Using a js object in a template string directly resolves to [object Object] i.e. ${filter} resolves to precisely [object Object] hence the error.

To solve it you can use JSON.stringify(filter). Here is the string in the same format as before but with the fix:

`query { 
    companies(filter: ${JSON.stringify(filter)}) {
        list {
            name
        }
    }
}`

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 cascading-jox