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.
JavaScript
x
13
13
1
state = { patients: [] }
2
3
async componentDidMount() {
4
try {
5
const apiData = await API.graphql(graphqlOperation(listxxxx))
6
const patie = apiData.data.listxxxx.items
7
this.setState({ patie })
8
console.log(patie)
9
} catch (err) {
10
console.log('qqqqqqqqqqqqqqqqq ', err)
11
}
12
}
13
How does one go about using the get query? Thanks!
Advertisement
Answer
You need an ID to retrieve an item with any get query. getPatient(id:”YOUR ID HERE”){}`
Something like…
JavaScript
1
7
1
query Get_Patient_By_Id{
2
getPatient(id:"2dbcb870-e302-4ed5-a419-68751597129c"){
3
id
4
name
5
}
6
}
7
For React, you’ll add in the id to the variables list argument:
JavaScript
1
8
1
const getPatient = await API.graphql(
2
graphqlOperation(
3
queries.getPatient,
4
{id: "2dbcb870-e302-4ed5-a419-68751597129c"}
5
)
6
);
7
console.log(getPatient.data.getPatient);
8
docs: https://aws-amplify.github.io/docs/js/api#simple-query