操作 'FooQuery' 包装 'Foo' 需要一个变量,但没有找到 react-apollo

The operation 'FooQuery' wrapping 'Foo' is expecting a variable, but it was not found with react-apollo

我在使用 react-apollo 时遇到问题。即使不在 Foo class:

中调用查询

The operation 'FooQuery' wrapping 'Foo' is expecting a variable: 'id' but it was not found in the props passed to 'Apollo(Foo)'

class Foo extends React.Component {
    render() {
        return <div />
    }
}

const FOO_QUERY = gql`
    query FooQuery($id: ID!) {
        foo(id: $id) {
            bar
        }
    }
`

export default graphql(FOO_QUERY, { name: 'fooQuery' })(Foo)

您的组件看起来是正确的。问题不在于你如何定义它,但你必须确保在你的应用程序中使用它时将 id 道具传递给你的 Foo 组件:

<Foo id="some_id" />