forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
63 lines (57 loc) · 1.27 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { Component } from 'react';
import ApolloClient, { gql } from 'apollo-boost';
import { ApolloProvider, Query } from 'react-apollo';
const GET_PIKA = gql`
{
pokemon(name: "Pikachu") {
name
}
}
`;
const client = new ApolloClient({
uri: 'https://graphql-pokemon.now.sh/graphql',
});
class Pokemon extends Component {
render() {
const { name } = this.props.pokemon;
return (
<h1>
Pokemon name: <span className="Pokemon-Name-Data">{name}</span>
</h1>
);
}
}
class Data extends Component {
state = {};
componentDidCatch() {
this.setState({ hasError: true });
}
render() {
const { hasError } = this.state;
return hasError ? (
<div className="Pokemon-Name-Data">Error :(</div>
) : (
<Query query={GET_PIKA}>
{({ loading, error, data }) => {
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return <div className="Pokemon-Name-Data">Error :(</div>;
}
return <Pokemon pokemon={data.pokemon} />;
}}
</Query>
);
}
}
class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<Data />
</ApolloProvider>
);
}
}
export default App;