Skip to content

Instantly share code, notes, and snippets.

@dabit3
Created November 6, 2018 19:06
Show Gist options
  • Save dabit3/6759e9f70ad17790fc9c9c5c0f933b53 to your computer and use it in GitHub Desktop.
Save dabit3/6759e9f70ad17790fc9c9c5c0f933b53 to your computer and use it in GitHub Desktop.
Querying for data from AWS AppSync
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { API, graphqlOperation } from 'aws-amplify'
import { listRestaurants } from './graphql/queries.js'
class App extends Component {
state = { restaurants: [] }
async componentDidMount() {
try {
const apiData = await API.graphql(graphqlOperation(listRestaurants))
const restaurants = apiData.data.listRestaurants.items
this.setState({ restaurants })
} catch (err) {
console.log('error: ', err)
}
}
render() {
return (
<div className="App">
{
this.state.restaurants.map((rest, i) => (
<div style={styles.item}>
<p style={styles.name}>{rest.name}</p>
<p style={styles.description}>{rest.description}</p>
</div>
))
}
</div>
);
}
}
const styles = {
item: {
padding: 10,
borderBottom: '2px solid #ddd'
},
name: { fontSize: 22 },
description: { color: 'rgba(0, 0, 0, .45)' }
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment