如何用apollo获取数据并做出本机反应

时间:2016-12-20 17:42:41

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

我已经能够使用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方法,并且我得到了正确的结果。

但现在我正在努力用反应阿波罗工具来做这件事,如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>
    );
  }
});

...我不知道阿波罗究竟是如何工作的,我也是React Native和GraphQl的新手,所以基本上我已经相当失落了。文档对我没什么帮助:/

1 个答案:

答案 0 :(得分:1)

  • 您正在定义GrocerListViewWithData但仅在GrocerListView中使用AlpuzonPage
  • 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);

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