Reputation: 1319
I have the following function:
import ApolloClient from 'apollo-boost'
import gql from 'graphql-tag'
import fetch from 'node-fetch'
global.fetch = fetch
const client = new ApolloClient({
uri: 'myUri'
})
const getPostsByCategory = async category => {
const res = await client.query({
query: gql`
query articlesByCategory($id: String!) {
postsByCategory(id: $id) {
id
}
}
`
})
console.log('res', res)
}
I want to invoke the function as:
await getPostsByCategory('news')
however I just can't understand how I pass the category variable into the query. I want to use qraphql-tag
in my queries and not pass a simple tagged literal as the query.
Upvotes: 3
Views: 3417
Reputation: 3664
You can use the variables
key in your client.query
function params as the following
const getPostsByCategory = async category => {
const res = await client.query({
query: gql`
query articlesByCategory($id: String!) {
postsByCategory(id: $id) {
id
}
}
`,
variables: {
id: category,
},
});
console.log('res', res);
};
Upvotes: 6