【问题标题】:How to use the GraphQL Get query from aws in React如何在 React 中使用来自 aws 的 GraphQL Get 查询
【发布时间】:2019-08-04 05:30:30
【问题描述】:

我正在尝试在 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 查询?谢谢!

【问题讨论】:

    标签: javascript reactjs graphql aws-appsync aws-amplify


    【解决方案1】:

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

    类似...

    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

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 2020-04-17
      • 2018-02-16
      • 1970-01-01
      • 2018-11-01
      • 2023-03-30
      • 2016-11-30
      • 2017-12-29
      • 2018-01-01
      相关资源
      最近更新 更多