如何使用关系参数过滤 GraphQL 查询,React Native

How to filter GraphQL query with relational parameters, React Native

我有一个 React Native 应用程序,它为我提供了一个属性列表。每个 属性 都有自己的页面和一个 link 屏幕,其中列出了与该 属性 和该用户相关的所有笔记。因此,用户只能在此屏幕上看到他们为此特定 属性 创建的笔记。

我在编写获取与当前 属性 和当前用户关联的所有注释的 GraphQL 查询时遇到问题。

我的基本直觉是这样写的...

const getNotes = gql`
  query getNotes {
    allPropertyNotes(filter: {
        AND: [{
            propertyId: ${this.props.navigation.state.params.id}
        }, {
            userId: ${this.props.screenProps.user.id}
        }]
    }, orderBy: createdAt_DESC) {
        id
        title
        body
    }
}
`;

但这似乎不起作用,因为查询是在组件外部定义的,无法访问 this.props(我假设)。

我的下一步是这样做

const getNotes = gql`
  query getNotes($userId: ID!, $propertyId: ID!) {
    allPropertyNotes(filter: {
        AND: [{
            propertyId: $propertyId
        }, {
            userId: $userId
        }]
    }, orderBy: createdAt_DESC) {
        id
        title
        body
    }
}
`;

但我不确定如何将变量绑定到我需要的 ID。关于如何执行此操作的任何指示?

如果您只需要 'query on mount' 那么只需使用 props 作为变量渲染 <Query/> 组件。 Example with variables

你可以用 graphql() options.variables

做同样的事情

传递查询(默认情况下在启动时触发)和 [许多] 突变作为 props 适用于更复杂的情况。 docs

这里的文档并不冗长,你必须从许多places/pieces中收集知识或搜索教程。