AppSync React
#6 FukuokaJS
2018/9/13
•
• Fusic
• Twitter @kobatako_
• Qiita @kobatako
• AppSync
• React
•
AppSync
AppSync
• AWS GraphQL API
•
•
• Schema
• Data Source
• Resolvers
AppSync
• GraphQL Schema
• Query Mutation Subscription
• Scalars AWSDateTime AWSPhone
AWSIPAddress
AppSync
• Data Source
• AppSync
• Lambda DynamoDB Elasticsearch 

AppSync
• Resolvers
• Data Source
• DynamoDB DynamoDB
AppSync
•
AppSync
AppSync
•
• React React Native NodeJS iOS
Android SDK
• AppSync Client Apollo Client
AppSync
•
• API_KEY
• AWS_IAM
• OPENID_CONNECT
• AMAZON_COGNITO_USER_POOLS
Cognito
AppSync
React
DynamoDB
• Cognito
• Amazon Cognito
• DynamoDB
• Key Value NoSQL
•
• React
• React Router v4
• React Redux
• Bloomer CSS
• AppSync Client AppSync
• Amplify AWS SDK
• Cognito AWS Amplify
• AWS Amplify
• MFA
•
React
AppSync
AppSync
• AppSync API
•
AppSync
•


Event App
AppSync
•
• API_KEY
AppSync
• Schema
• DynamoDB
AppSync
• Schema
• DynamoDB
AppSync
• Cognito
• Cognito
Region user pool
AppSync
• AppSync
AppSync
• AppSync Client
# AppSync.js
{
"graphqlEndpoint": “https://xxxxxxxxxxxxxxx.appsync-api.ap-northeast-1.amazonaws.com/graphql",
"region": "ap-northeast-1",
"authenticationType": "AMAZON_COGNITO_USER_POOLS",
"apiKey": null
}
React
React
• AppSync
import appSyncConfig from “./AppSync"; // AppSync
import AWSAppSyncClient from "aws-appsync";
import awsmobile from './AwsExports';
import Amplify, { Auth } from 'aws-amplify';
Amplify.configure(awsmobile);
const client = new AWSAppSyncClient({
disableOffline: true,
url : appSyncConfig.graphqlEndpoint,
region: appSyncConfig.region,
auth: {
type: appSyncConfig.authenticationType,
jwtToken: async () => (await Auth.currentSession()
.then(data => {
return data
})
.catch(err => {
return err
})).getIdToken().getJwtToken(), },
});
AppSync.js
React
• Provider
import React, { Component } from 'react';
import './App.css';
import appSyncConfig from "./AppSync";
import { ApolloProvider } from 'react-apollo';
class App extends Component {
render() {
return (
<div className="App">
<Provider store={ store }>
<PersistGate loading={ null } persistor={ persistor }>
<ApolloProvider client={ client }>
<MainContainer />
</ApolloProvider>
</PersistGate>
</Provider>
</div>
);
}
}
React
• Sign In
import SignInContainer from '../containers/signInContainer.jsx';
class MainContainer extends Component {
render() {
return (
<Router>
<Switch>
<Route exact path="/" component={ ConditionsContainer } />
<Route exact path="/conditions" component={ ConditionsContainer } />
<Route exact path="/conditions/add" component={ CreateConditionContainer } />
<Route exact path="/conditions/edit/:id" component={ UpdateConditionContainer } />
<Route />
</Switch>
</Router>
);
}
}
export default withAuthenticator(MainContainer, true, [
<SignInContainer />,
<ConfirmSignIn />,
<VerifyContact />,
<SignUp />,
<ConfirmSignUp />,
<ForgotPassword />,
]);
SignInContainer Sign In
SignUp
React
• Sign In
import Amplify, { Auth } from 'aws-amplify';
class SignInContainer extends SignIn {
signIn(user) {
if (user) {
Auth.signIn(user.name, user.password)
.then( u => {
this.props.signIn({name: u.username});
this.props.onStateChange("signedIn", u);
})
.catch( e => console.error(e));
}
}
then
catch
Cognito
• Apollo GraphQL
import gql from 'graphql-tag';
export default gql`
mutation createCondition($condition: CreateConditionInput!) {
createCondition(input: $condition) {
id
user_name
day
physical_state
physical_note
work_state
work_note
work_after
leave_time
}
}`;
export default connect(
mapStateToProps,
)(compose(
graphql(CreateConditionMutation, {
props: (props) => ({
onCreate: (condition) => {
console.log(condition);
props.mutate({
variables: {
condition: condition
}
})
}
}),
• Mutation
DynamoDB
• AppSync GraphQL
• AppSync
• Lambda ElasticSearch
• DynamoDB
DynamoDB
AppSyncをReactで使ってみた

AppSyncをReactで使ってみた