[GraphQL] Apollo React Query Component

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GraphQL] Apollo React Query Component相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

GraphQL“必须提供查询字符串” graphql-tag react-apollo

React + Apollo:GraphQL 错误数组未传递到组件中

使用 Express-GraphQL 和 React-Apollo 订阅 GraphQL

React、Apollo 2、GraphQL、身份验证系统

React-Apollo-Prisma-Graphql

react-apollo 没有连接到我的 graphql 本地服务器