The API category provides React components for working with GraphQL data using the Amplify GraphQL client. The `` component is used to execute a GraphQL query or mutation. You can execute GraphQL queries by passing your queries in `query` or `mutation` attributes: ## Queries ```javascript import React, { Component } from 'react'; import { Amplify, graphqlOperation } from "aws-amplify"; import { Connect } from "aws-amplify-react"; import * as queries from './graphql/queries'; import * as subscriptions from './graphql/subscriptions'; class App extends Component { render() { const ListView = ({ todos }) => (

All Todos

); return ( {({ data: { listTodos }, loading, errors }) => { if (errors) return (

Error

); if (loading || !listTodos) return (

Loading...

); return ( ); }}
) } } export default App; ``` ## Subscription Also, you can use the `subscription` and `onSubscriptionMsg` attributes to enable subscriptions: ```javascript { console.log(onCreateTodo); return prev; }} > {({ data: { listTodos }, loading, error }) => { if (error) return (

Error

); if (loading || !listTodos) return (

Loading...

); return (); }}
``` ## Mutations For mutations, a `mutation` function needs to be provided with the `Connect` component. A `mutation` returns a promise that resolves with the result of the GraphQL mutation. ```jsx import React, { Component } from 'react'; import * as mutations from './graphql/mutations'; import * as queries from './graphql/queries'; import * as subscriptions from './graphql/subscriptions'; class AddTodo extends Component { constructor(props) { super(props); this.submit = this.submit.bind(this); this.state = { name: '', description: '', }; } handleChange(name, event) { this.setState({ [name]: event.target.value }); } async submit() { const { onCreate } = this.props; const input = { name: this.state.name, description: this.state.description } console.log(input); try { await onCreate({input}) } catch (err) { console.error(err); } } render(){ return (
{ this.handleChange('name', event)}} /> { this.handleChange('description', event)}} />
); } } class App extends Component { render() { const ListView = ({ todos }) => (

All Todos

) return (
{({mutation}) => ( )} { console.log('Subscription data:', onCreateTodo) return prev; } }> {({ data: { listTodos }, loading, error }) => { if (error) return

Error

; if (loading || !listTodos) return

Loading...

; return (); }}
); } } ```