0

I have a query to which i would like to insert a variable:

this is the query :

query = gql`{getStuff{outputs(filter: ["10_lorries", "3_cars"]){id, brand}}}`
const {data, error, loading} = useQuery(query)

which by itself works fine, but what if instead of explicitely passing the array I had it defined in a variable and want to pass it to the query?

const vehicles= ["10_lorries", "3_cars"]

when trying this data returns an undefined:

query = gql`{getStuff{outputs(filter: vehicles){id, brand}}}`

I am struggling a bit to understand how to pass the variable into the gql query..

1 Answer 1

0

You can pass variable through options provided by apollo useQuery hooks:

query = gql`{getStuff{outputs(filter: myFilter){id, brand}}}`

const vehicles= ["10_lorries", "3_cars"]

const { loading, error, data } = useQuery(query, {
  variables: { myFilter: vehicles },
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.