React Native, AWS AppSync and React Apollo

James Best
Mar 14, 2019 · 7 min read
Image for post
Image for post

The rest of this post…

The rest of this post is going detail how to get up and running with AWS AppSync and React Apollo in a React Native project.

react-native init appsyncApollocd appsyncApollo
react-native run-ios
Image for post
Image for post
yarn global add @aws-amplify/cliamplify configure
AWS Amplify configure walkthrough
yarn add aws-amplify aws-amplify-react-nativereact-native link amazon-cognito-identity-jsamplify init
amplify add authamplify push
import Amplify from 'aws-amplify';
import awsmobile from './aws-exports';
import { withAuthenticator } from 'aws-amplify-react-native';

Amplify.configure(awsmobile);
export default withAuthenticator(App, true);
Image for post
Image for post
yarn add react-native-vector-iconsreact-native link react-native-vector-icons
amplify add api
Image for post
Image for post
type Todo @model {
id: ID!
name: String!
description: String
completed: Boolean!
}
Image for post
Image for post
yarn add aws-appsync aws-appsync-react graphql-tag react-apollo

First Mutation

Let's write our first mutation. We are gonna write the mutation to create a new todo. Without this, our app is pretty useless.

Query

The ListTodos component follows a similar pattern to the CreateTodos component.

Image for post
Image for post
import { LIST_TODOS_QUERY } from './listTodos';[...]
<Mutation
mutation={CREATE_TODO_MUTATION}
variables={{ ...this.state }}
refetchQueries={[{ query: LIST_TODOS_QUERY }]}>
{(createTodo, { data }) => {
console.log("MUTATION DATA: ", data);
return (
<View style={{ flex: 1, width: "100%" }}>
[...]
import { listTodos } from './src/graphql/queries'[...]
<Query query={gql(listTodos)}>
{({ data: { listTodos }, error, loading }) => {
[...]
Image for post
Image for post

See what we have done for others and get in touch to discuss what we can achieve together

Gravitywell UK

Let’s talk about tech, baby

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store