Skip to content

Instantly share code, notes, and snippets.

@jmn
Created November 21, 2018 15:39
Show Gist options
  • Save jmn/98c0740086046c23ce97b3911349ad04 to your computer and use it in GitHub Desktop.
Save jmn/98c0740086046c23ce97b3911349ad04 to your computer and use it in GitHub Desktop.
React Native GraphQL Apollo Boost in FlatList
import React from 'react';
import { Text, View, SafeAreaView, FlatList } from 'react-native';
import ApolloClient from "apollo-boost";
import gql from "graphql-tag";
import { ApolloProvider } from "react-apollo";
import { Query } from "react-apollo";
const client = new ApolloClient({
uri: "https://www.fedry.net/graphql"
});
const App = () => (
<ApolloProvider client={client}>
<Text>My first Apollo app ??</Text>
<ExchangeRates />
</ApolloProvider>
);
export default App;
const ExchangeRates = () => (
<Query
query={gql`
{
posts(first: 6) {
edges {
node {
title
id
}
}
}
}
`}
>
{({ loading, error, data }) => {
if (loading) return <Text>Loading...</Text>;
if (error) return <Text>Error </Text>;
return <FlatList
data={data.posts.edges}
renderItem={({item}) => <Text>{`${item.node.title}`}</Text>}
keyExtractor={(item, index) => index.toString()}
/>
}}
</Query>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment