Search code examples
javascriptreactjsgraphqlaws-appsyncaws-amplify

How to use the GraphQL Get query from aws in React


I'm trying to make use of the getquery for graphql in react.js. But I can't figure out how to go about doing this. I already succeeded to use the list query.

state = { patients: [] }

async componentDidMount() {
  try {
    const apiData = await API.graphql(graphqlOperation(listxxxx))
    const patie = apiData.data.listxxxx.items
    this.setState({ patie })
    console.log(patie)
  } catch (err) {
    console.log('qqqqqqqqqqqqqqqqq ', err)
  }
}

How does one go about using the get query? Thanks!


Solution

  • You need an ID to retrieve an item with any get query. getPatient(id:"YOUR ID HERE"){}`

    Something like...

    query Get_Patient_By_Id{
      getPatient(id:"2dbcb870-e302-4ed5-a419-68751597129c"){
         id        
         name
      }
    }
    

    For React, you'll add in the id to the variables list argument:

    const getPatient = await API.graphql(
      graphqlOperation(
        queries.getPatient, 
        {id: "2dbcb870-e302-4ed5-a419-68751597129c"}
      )
    );
    console.log(getPatient.data.getPatient);
    

    docs: https://aws-amplify.github.io/docs/js/api#simple-query