import ApolloClient from 'apollo-boost'; const client = new ApolloClient({ uri: 'https://48p1r2roz4.sse.codesandbox.io', });import { gql } from "apollo-boost"; // or you can use `import gql from 'graphql-tag';` instead ... client .query({ query: gql` { rates(currency: "USD") { currency } } ` }) .then(result => console.log(result));
Connect your client to React
用到ApolloProvider ,
!!! 如果用到react-router 一般会在包裹在 root route 组件外层
import { ApolloProvider } from '@apollo/react-hooks';
const App = () => (
  <ApolloProvider client={client}>
    <div>
      <h2>My first Apollo app ?</h2>
    </div>
  </ApolloProvider>
);
Request data
在 添加ApolloProvider 之后 就可以用useQuery hook 请求数据
import { useQuery } from '@apollo/react-hooks';
import { gql } from 'apollo-boost';
function ExchangeRates() {
  const { loading, error, data } = useQuery(gql`
    {
      rates(currency: "USD") {
        currency
        rate
      }
    }
  `);
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;
  return data.rates.map(({ currency, rate }) => (
    <div key={currency}>
      <p>
        {currency}: {rate}
      </p>
    </div>
  ));
}