-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ios.js
98 lines (92 loc) · 1.93 KB
/
index.ios.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Sample React Native App with GraphQL
*/
'use strict';
var React = require('react-native');
var Lokka = require('lokka').Lokka;
var Transport = require('lokka-transport-http').Transport;
// Create a lokka client against SWAPI GraphQL API
var client = new Lokka({
transport: new Transport('http://graphql-swapi.parseapp.com/')
});
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var graphqlDemo = React.createClass({
getInitialState() {
return {movies: []}
},
componentDidMount() {
// query when we mount the view
client.query(`
{
allFilms {
films {
title
}
}
}
`).then(result => {
this.setState({movies: result.allFilms.films});
}).catch(error => {
this.setState({error});
});
},
render: function() {
var {movies} = this.state;
return (
<View style={styles.container}>
<Text style={styles.title}>
"Star Wars" Movies
</Text>
{(movies.length > 0)? this.renderMovies(movies) : this.renderLoading()}
</View>
);
},
renderMovies(movies) {
return (
<View>
{movies.map(movie => (
<Text key={movie.title} style={styles.movies}>
{movie.title}
</Text>
))}
</View>
);
},
renderLoading() {
var {error} = this.state;
if(error) {
return (
<View>
<Text>Oops..</Text>
<Text>{error.message}</Text>
</View>
);
} else {
return (<Text>loading...</Text>);
}
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
title: {
fontSize: 25,
textAlign: 'center',
margin: 10,
},
movies: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('graphqlDemo', () => graphqlDemo);