【问题标题】:How to fetch data with apollo and react native如何使用 apollo 获取数据并做出原生反应
【发布时间】:2017-05-06 00:30:21
【问题描述】:

我已经能够使用 express-graphql 制作一个简单的 graphql 服务器。

http://localhost:8080/graphql 中查询:

    {
      grocers {
        _id
        name
        description
        location
      },
      products(grocerId: "585676ef987ed341fed7fdd2") {
        name
      }
    }

我得到以下信息:

{
  "data": {
    "grocers": [
      {
        "_id": "58567074987ed341fed7fcb1",
        "name": "Martina",
        "description": "Las mejores verduras de la alpujarra",
        "location": "Ugíjar"
      },
      {
        "_id": "585676ef987ed341fed7fdd2",
        "name": "Antonio",
        "description": "Leña y calorcito para todos!",
        "location": "Ugíjar"
      }
    ],
    "products": [
      {
        "name": "Concierto de guitarra"
      }
    ]
  }
}

到目前为止一切顺利。现在我想从 React Native 查询。我一直在尝试使用 Apollo 客户端这样做。目前,我已经能够使用他们文档中解释的 vanilla 方法进行查询,并且得到了正确的结果。

但是现在我正在努力使用 react apollo 工具来做这件事,正如这里 http://dev.apollodata.com/react/ 所解释的那样。代码如下:

// ...

const networkInterface = createNetworkInterface({ uri: 'http://localhost:8080/graphql' });

const client = new ApolloClient({
  networkInterface
});

class Alpuzon extends Component {

  constructor() {
    super(...arguments);

  }

  _renderScene(route, navigator) {
    if (route.id === 1) {
      return <AlpuzonPage navigator={navigator} />
    } else if (route.id === 2) {
      return <PageTwo navigator={navigator} />
    }

    return false;
  }

  _configureScene() {
    return Navigator.SceneConfigs.FloatFromRight;
  }

  render() {
    return (
      <ApolloProvider client={client}>
        <Navigator
          initialRoute={{ id: 1 }}
          renderScene={this._renderScene}
          configureScene={this._configureScene}
        />
      </ApolloProvider>
    );
  }
}

// ...

let AlpuzonPage = React.createClass({

  _handlePress() {
    this.props.navigator.push({id: 2});
  },

  render() {

    const getGrocersQuery = gql`query getGrocersQuery { grocers { name } }`;

    class GrocerListView extends Component {
      render() {
        <ListView
          dataSource={data.grocers}
          renderRow={(obj) => <Text>{obj.name}</Text>}
        />
      }
    }

    GrocerListView.propTypes = {
      data: PropTypes.shape({
        loading: PropTypes.array.isRequired,
        grocers: PropTypes.array
      }).isRequired
    };

    const GrocerListViewWithData = graphql(getGrocersQuery)(GrocerListView);

    return (
      <View style={{ flex: 1, flexDirection: 'column'}}>
        <GrocerListView />
      </View>
    );
  }
});

...我不知道 apollo 是如何工作的,我也是 React Native 和 GraphQl 的新手,所以基本上我迷路了。文档对我帮助不大:/

【问题讨论】:

    标签: reactjs react-native graphql apollo react-apollo


    【解决方案1】:
    • 您正在定义GrocerListViewWithData,但仅在AlpuzonPage 中使用GrocerListView
    • GrocerListView 中,您应该访问this.props.data 而不是data,您还必须等待this.props.data.loading 完成加载

    这是更新后的代码:

    let AlpuzonPage = React.createClass({
    
      _handlePress() {
        this.props.navigator.push({id: 2});
      },
    
      render() {
    
        return (
          <View style={{ flex: 1, flexDirection: 'column'}}>
            <GrocerListViewWithData />
          </View>
        );
      }
    });
    
    class GrocerListView extends Component {
      render() {
        if (this.props.data.loading) {
          return (<div>Loading</div>)
        }
    
        <ListView
          dataSource={this.props.data.grocers}
          renderRow={(obj) => <Text>{obj.name}</Text>}
        />
      }
    }
    
    GrocerListView.propTypes = {
      data: PropTypes.shape({
        loading: PropTypes.array.isRequired,
        grocers: PropTypes.array
      }).isRequired
    };
    
    const getGrocersQuery = gql`query getGrocersQuery { grocers { name } }`;
    
    const GrocerListViewWithData = graphql(getGrocersQuery)(GrocerListView);
    

    如果您想用 Apollo 试验 React Native 的工作示例,请查看 Learn Apollo,还有一个 React Native 和一个 Exponent JS 轨道:)

    【讨论】:

    • 昨天我终于弄明白了。无论如何,谢谢,因为你是对的。文档的内容是 apollo 允许您以各种方式做事,但文档并不总是显示所有这些。
    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    相关资源
    最近更新 更多