码迷,mamicode.com
首页 > 其他好文 > 详细

[GraphQL] Apollo React Query Component

时间:2018-03-10 00:22:42      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:log   bak   ring   width   https   addition   frame   ide   div   

In this lesson I refactor a React component that utilizes the graphql higher-order component to the new Query render prop component baked into react-apollo.

Additional Resources: What‘s next for React Apollo

 

import React from "react";
import { render } from "react-dom";
import ApolloClient, { gql } from "apollo-boost";
import { ApolloProvider, Query } from "react-apollo";

const client = new ApolloClient({ uri: "https://graphql-pokemon.now.sh" });

const GET_POKEMON = gql`
  query($name: String!) {
    pokemon(name: $name) {
      name
      image
    }
  }
`;

const Pokemon = ({ name, image }) => {
  return (
    <div>
      <div>{name}</div>
      <img src={image} />
    </div>
  );
};

const PokemonQuery = () => (
  <Query query={GET_POKEMON} variables={{ name: "pikachu" }}>
    {({ loading, error, data }) => {
      if (loading) return <div>Loading...</div>;
      if (error) return <div>Error</div>;
      return <Pokemon name={data.pokemon.name} image={data.pokemon.image} />;
    }}
  </Query>
);

const ApolloApp = () => (
  <ApolloProvider client={client}>
    <PokemonQuery />
  </ApolloProvider>
);

render(<ApolloApp />, document.getElementById("root"));

 

 

[GraphQL] Apollo React Query Component

标签:log   bak   ring   width   https   addition   frame   ide   div   

原文地址:https://www.cnblogs.com/Answer1215/p/8536853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!