This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
App.js
159 lines (132 loc) · 4.97 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { Component } from 'react';
import './App.css';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import "semantic-ui-css/semantic.min.css";
import AllDestinations from "./Components/AllDestinations";
import AddDestination from "./Components/AddDestination";
import NewDestinationsSubscription from './Queries/NewDestinationsSubscription';
import AWSAppSyncClient from "aws-appsync";
import { Rehydrated } from 'aws-appsync-react';
import { AUTH_TYPE } from "aws-appsync/lib/link/auth-link";
import { graphql, ApolloProvider, compose } from 'react-apollo';
import * as AWS from 'aws-sdk';
import AppSync from './aws-exports.js';
import AllDestinationsQuery from './Queries/AllDestinationsQuery';
import NewDestinationMutation from './Queries/NewDestinationMutation';
const client = new AWSAppSyncClient({
url: AppSync.aws_appsync_graphqlEndpoint,
region: AppSync.aws_appsync_region,
auth: {
type: AUTH_TYPE.API_KEY,
apiKey: AppSync.aws_appsync_apiKey,
// type: AUTH_TYPE.AWS_IAM,
// Note - Testing purposes only
/*credentials: new AWS.Credentials({
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY
})*/
// Amazon Cognito Federated Identities using AWS Amplify
//credentials: () => Auth.currentCredentials(),
// Amazon Cognito user pools using AWS Amplify
// type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
// jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken(),
},
options: {
fetchPolicy: 'cache-and-network'
},
disableOffline:false
});
const appHeaderStyle = {
marginBottom: '20px'
};
const headerStyle = {
color: 'white',
padding: '10px'
}
const Home = () => (
<div>
<header className="App-header" style={appHeaderStyle}>
<h2 class="ui center aligned icon header">
<img src="https://d1.awsstatic.com/serverless/Lambda%20Resources%20images/sam_acorn_shadow.8915b92ddd48a78d8c05d55ce4b26e472889c573.png" className="App-logo" alt="logo" />
<span style={headerStyle}>Welcome to Serverless Bytes #3: Serverless Web App powered by AWS AppSync</span>
</h2>
</header>
<div className="ui container">
<h3 className="ui horizontal divider header">
<i className="paper plane icon"></i>
Travel Destinations and Current Conditions
</h3>
<AllDestinationsWithData />
<h4 className="ui horizontal divider header">
<i className="tag icon"></i>
Create a destination
</h4>
<NewDestinationWithData />
</div>
</div>
);
const App = () => (
<Router>
<div>
<Route exact={true} path="/" component={Home} />
</div>
</Router>
);
const AllDestinationsWithData = compose(
graphql(AllDestinationsQuery, {
options: {
fetchPolicy: 'cache-and-network'
},
props: (props) => ({
destinations: props.data.getAllDestinations,
// START - NEW PROP :
subscribeToDestinations: params => {
props.data.subscribeToMore({
document: NewDestinationsSubscription,
updateQuery: (previousResult, { subscriptionData, variables }) => {
// Perform updates on previousResult with subscriptionData
console.log(previousResult);
var newDestination = subscriptionData.data.newDestination;
console.log (newDestination);
const newObj = {};
newObj.getAllDestinations = [newDestination, ...previousResult.getAllDestinations];
console.log(newObj);
return newObj;
}
/*(prev, { subscriptionData: {data: {newDestination}} }) => ({
...prev,
getAllDestinations: { getAllDestinations: [newDestination, prev.getAllDestinations.filter(d => d.id != newDestination.id)] , __typename: 'Destinations'}
})*/
});
}
// END - NEW PROP
})
})
)(AllDestinations);
const NewDestinationWithData = graphql(NewDestinationMutation, {
props: (props) => ({
onAdd: destination => props.mutate({
variables: destination,
optimisticResponse: () => ({ addDestination: { ...destination, __typename: 'Destination', version: 1 } }),
})
}),
options: {
//refetchQueries: [{ query: AllDestinationsQuery }],
update: (dataProxy, { data: { addDestination } }) => {
const query = AllDestinationsQuery;
const data = dataProxy.readQuery({ query });
data.getAllDestinations.destinations.push(addDestination);
dataProxy.writeQuery({ query, data });
},
fetchPolicy: 'cache-and-network',
disableOffline:false
}
})(AddDestination);
const WithProvider = () => (
<ApolloProvider client={client}>
<Rehydrated>
<App />
</Rehydrated>
</ApolloProvider>
);
export default WithProvider;