query screenshot
const allTeamApplicants = gql`
query ($id: ID!) {
allApplicants(filter: { user: { id: $id } }) {
id
firstName
lastName
appliedPosition
applicationStage
isFormFilled
isContractSigned
email
phoneNumber
}
I use Apollo Client for GraphQL in React web app. Anyone knows how to invoke a query with parameters in an event, for example, I want to trigger the query with a parameter when a user clicks a button.
Executing a query To run a query within a React component, call useQuery and pass it a GraphQL query string. When your component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties you can use to render your UI.
In GraphQL, you fetch data with the help of queries. A query is a GraphQL Operation that allows you to retrieve specific data from the server. We ask the server for all the todos and their titles in the above query. The “ todos " represents an object and " title " a field.
To call a query in an event, instead of as in a higher order component, you should:
Import withApollo Hoc
import { withApollo } from 'react-apollo';
Wrap your component with withApollo
const component = withApollo(Component)
Inside your component, you will receive a new prop called client
and you can use it as a Promise, such as:
function eventHandler(idParam) {
client.query({
query: gql`
query ($id: ID!) {
allApplicants(filter: { user: { id: $id } }) {
id
firstName
lastName
appliedPosition
applicationStage
isFormFilled
isContractSigned
email
phoneNumber
}
}`,
variables: {
// set the variables defined in the query, in this case: query($id: ID!)
id: idParam
}
}
})
.then(...)
.catch(...)
}
more info here: https://www.apollographql.com/docs/react/api/react-apollo.html#withApollo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With