-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDogsScreen.js
124 lines (116 loc) · 3.25 KB
/
DogsScreen.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React, { Component } from 'react';
import { StyleSheet, Text, View, Button, Modal, TextInput } from 'react-native';
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-boost';
import { ApolloProvider, graphql, Mutation } from 'react-apollo';
import gql from 'graphql-tag';
const client = new ApolloClient({
link: new HttpLink({
uri: 'https://eu1.prisma.sh/natalia-majkowska/dogs-service/dev',
headers: {
authorization:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InNlcnZpY2UiOiJkb2dzLXNlcnZpY2VAZGV2Iiwicm9sZXMiOlsiYWRtaW4iXX0sImlhdCI6MTU0NDc5MjU5OSwiZXhwIjoxNTQ1Mzk3Mzk5fQ.dXN4VFKte52AkmF9Gzm_CHKQko0WTGopXsal4QQEVzA' // on production you need to store token in storage or in redux persist, for demonstration purposes we do this like that
}
}),
cache: new InMemoryCache()
});
const dogQuery = gql`
query {
dogs {
name
type
}
}
`;
const addDog = gql`
mutation addDog($type: String!, $name: String!) {
createDog(data: { type: $type, name: $name }) {
id
}
}
`;
const DogComponent = graphql(dogQuery)(props => {
const { error, dogs } = props.data;
if (error) {
return <Text>{error}</Text>;
}
if (dogs) {
return (
<View>
{dogs.map(dog => {
return <Text key={dog.name}>{dog.name}</Text>;
})}
</View>
);
}
return <Text>Loading...</Text>;
});
export class DogsScreen extends Component {
state = {
name: '',
type: ''
};
render() {
return (
<ApolloProvider client={client}>
<View style={styles.container}>
<Mutation mutation={addDog} refetchQueries={[{ query: dogQuery }]}>
{(addDogMutation, { data }) => (
<View>
<Text style={styles.welcome}>Dogs data:</Text>
<TextInput
style={styles.input}
onChangeText={text => this.setState({ name: text })}
value={this.state.name}
placeholder="name"
/>
<TextInput
style={styles.input}
onChangeText={text => this.setState({ type: text })}
value={this.state.type}
placeholder="type"
/>
<Button
onPress={() => {
addDogMutation({
variables: {
type: this.state.type,
name: this.state.name
}
})
.then(res => res)
.catch(err => <Text>{err}</Text>);
this.setState({ type: '', name: '' });
}}
title="Add dog"
/>
</View>
)}
</Mutation>
<Text style={styles.welcome}>My dogs:</Text>
<DogComponent />
</View>
</ApolloProvider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
input: {
height: 30,
width: 150,
borderColor: 'gray',
borderWidth: 1,
marginTop: 5,
padding: 1
}
});