Skip to content

Instantly share code, notes, and snippets.

@redbluenat
Last active December 11, 2018 11:03
Show Gist options
  • Save redbluenat/bd322bbd6e34b2005e867bfd3927c0ce to your computer and use it in GitHub Desktop.
Save redbluenat/bd322bbd6e34b2005e867bfd3927c0ce to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-boost';
import { ApolloProvider, graphql } from 'react-apollo';
import gql from 'graphql-tag';
const dogQuery = gql`
query {
dogs {
name
type
}
}
`;
const DogComponent = graphql(dogQuery)(props => {
const { error, dogs } = props.data;
if (error) {
return <Text>{error}</Text>;
}
if (dogs) {
return <Text>{dogs[0].name}</Text>;
}
return <Text>Loading...</Text>;
});
const client = new ApolloClient({
link: new HttpLink({
uri: 'https://eu1.prisma.sh/natalia-majkowska/dogs-service/dev',
headers: {
authorization: 'YOUR_TOKEN' // on production you need to store token in storage or in redux persist, for demonstration purposes we do this like that
}
}),
cache: new InMemoryCache()
});
export default class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<View style={styles.container}>
<Text style={styles.welcome}>Be ready for:</Text>
<DogComponent />
</View>
</ApolloProvider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment