首页 > 解决方案 > 如何在 React 中使用来自 aws 的 GraphQL Get 查询

问题描述

我正在尝试在 react.js 中使用 getquery for graphql。但我不知道该怎么做。我已经成功使用了列表查询。

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

如何使用 get 查询?谢谢!

标签: javascriptreactjsgraphqlaws-appsyncaws-amplify

解决方案


您需要一个 ID 才能通过任何 get 查询检索项目。getPatient(id:"您的 ID"){}`

就像是...

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

对于 React,您需要将 id 添加到变量列表参数中:

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

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


推荐阅读