重组和Apollo查询

时间:2018-06-20 17:38:27

标签: apollo recompose

是否可以将Recompose用于react-apollo的查询组件?

https://www.apollographql.com/docs/react/essentials/queries.html#basic

我在想这样的事情:

const props = {component:"Query", query: gql(ListResults), children: ViewResults};
export default withProps(props)(componentFromProp('component'))

1 个答案:

答案 0 :(得分:1)

您可以编写查询,而不使用查询组件。

使用您发布的链接中的示例。

import { compose } from 'recompose';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';

const Dogs = ({ data, onDogSelected }) => (
  <select name="dog" onChange={onDogSelected}>
    {data.dogs.map(dog => (
      <option key={dog.id} value={dog.breed}>
        {dog.breed}
      </option>
    ))}
  </select>
);

const GET_DOGS = gql`
  {
    dogs {
      id
      breed
    }
  }
`;

const getDogsQuery = graphql(GET_DOGS, {
  options: props => ({
    fetchPolicy: props.fetchPolicy // example of passing to the query from props.
  })
});

export default compose(getDogsQuery)(Dogs);  // { data } is now a prop with the query results.
相关问题