如何将过滤器插入graphql查询字符串?

时间:2019-10-01 18:26:15

标签: javascript graphql aws-appsync

不幸的是,据我所知,在文档中没有关于如何将变量传递到过滤器中的好例子。这是在App Sync中工作的查询示例:

 query listPayments {
        listPayments(filter: {residentId: {contains: "some_random_id"}}) {
      items {
        timestamp
        totalAmount
        feeAmount
        transactionId
        paymentTraceId
        paymentReferenceId
        paymentMethodId
      }
    }
 }

但是我不知道如何将其传递给我的函数,然后该函数与模板字符串进行交互:

export const getResidentPayments = async (residentId) => {
  console.log('getting payments...')
  try {
    paymentHistoryResponse = (await API.graphql(
      graphqlOperation(listPayments, {
        input: {id: residentId}
      }),
    )).data
    console.log('payment history res', paymentHistoryResponse);
    paymentHistoryResponse = JSON.parse(paymentHistoryResponse);
    return paymentHistoryResponse
  } catch (error) {
    console.log('got payment history err', error);
    throw error;
  }
}

const listPayments = ` query listPayments ($residentId: id ) {
  listPayments(filter: {residentId: {contains: $residentId}}) {
    items {
      timestamp
      totalAmount
      feeAmount
      transactionId
      paymentTraceId
      paymentReferenceId
      paymentMethodId
    }
  }
}
`;

如果有人能告诉我我做错了,将不胜感激,因为这可能是一个相当明显的菜鸟错误。令我感到震惊的是,graphql没有为此提供文档的示例,除非我也对此有所误解。

1 个答案:

答案 0 :(得分:0)

AppSync为您构建了许多功能。 listPayments的类型定义是什么?

您是否为listPayments输入了包含字段和字符串的过滤器输入?