Call Query of GraphQL of Apollo Client

request screenshot

const allTeamApplicants = gql`
  query ($id: ID!) {
    allApplicants(filter: { user: { id: $id } }) {
      id
      firstName
      lastName
      appliedPosition
      applicationStage
      isFormFilled
      isContractSigned
      email
      phoneNumber
}

I am using Apollo Client for GraphQL in a React web application. Does anyone know how to trigger a query with parameters in an event, for example, I want to trigger a query with a parameter when the user clicks a button.

+4
source share
1 answer

To trigger a request in an event, and not as in a higher order component, you should:

Import using Apollo Hoc

import { withApollo } from 'react-apollo';

Wrap your component with withApollo

const component = withApollo(Component)

Inside your component, you will get a new icon called client, and you can use it as a promise, for example:

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(...)
}

: http://dev.apollodata.com/react/api-graphql.html#withApoll o

+3

Source: https://habr.com/ru/post/1684861/


All Articles