Last active
August 18, 2023 18:43
-
-
Save lukevandekieft/df16116896cdebc5d0f8ba44af78c4b8 to your computer and use it in GitHub Desktop.
Basic AppSync Subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { compose, graphql } from 'react-apollo'; | |
import gql from 'graphql-tag'; | |
import OnUpdateUser from './OnUpdateUser'; | |
import GetUserQuery from './GetUser'; | |
class Dashboard extends Component { | |
//Set up subscription to user info changes | |
componentDidUpdate(){ | |
if(this.props.subscribeToNewEvents){ | |
this.props.subscribeToNewEvents(); | |
} | |
} | |
render() { | |
if(this.props.loading) { | |
return ( | |
<div> | |
<h1>Loading..</h1> | |
</div> | |
); | |
} else { | |
return ( | |
<section className="section margin-theta-bottom"> | |
{ this.props.getUser.events.map((item, index) => { | |
return ( | |
<div> | |
<h6>{ item.name }</h6> | |
<p>{ item.details }</p> | |
</div> | |
); | |
})} | |
</section> | |
); | |
} | |
} | |
} | |
export default compose( | |
graphql(GetUserQuery, { | |
name: "GetUserQuery", | |
options: props => ({ | |
fetchPolicy: 'cache-and-network', | |
variables: { | |
id: props.userId | |
} | |
}), | |
props: (props) => { | |
return { | |
getUser: props.GetUserQuery.getUser, | |
loading: props.GetUserQuery.loading, | |
error: props.GetUserQuery.error, | |
// Subscription function: re-fire GetUserQuery whenever updateUser is called on logged in user | |
subscribeToNewEvents: params => { | |
props.GetUserQuery.subscribeToMore({ | |
document: OnUpdateUser, | |
variables: { | |
id: props.ownProps.userId | |
}, | |
updateQuery: (prev, { subscriptionData: { data : { onUpdateUser } } }) => { | |
return { | |
getUser: onUpdateUser | |
}; | |
} | |
}); | |
} | |
}; | |
} | |
}) | |
)(Dashboard); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gql from 'graphql-tag'; | |
export default gql ` | |
query getUser($id: String!) { | |
getUser( | |
id: $id | |
){ | |
accountLinked | |
createdAt | |
events | |
id | |
KYCStatus | |
name | |
nickname | |
password | |
phoneNumber | |
updatedAt | |
walletLinked | |
} | |
} | |
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import AppSyncApp from './AppSyncApp'; | |
import AWSAppSyncClient from "aws-appsync"; | |
import { Rehydrated } from 'aws-appsync-react'; | |
import { ApolloProvider } from 'react-apollo'; | |
import appSyncConfig from './AppSync'; | |
import Amplify, { Auth } from 'aws-amplify'; | |
Amplify.configure(awsconfig); | |
const client = new AWSAppSyncClient({ | |
url: appSyncConfig.graphqlEndpoint, | |
region: appSyncConfig.region, | |
auth: { | |
type: appSyncConfig.authenticationType, | |
credentials: async () => { | |
let credentials = await Auth.currentCredentials(); | |
return credentials; | |
} | |
}, | |
disableOffline: true | |
}); | |
const WithProvider = () => ( | |
<ApolloProvider client={client}> | |
<Rehydrated> | |
<App /> | |
</Rehydrated> | |
</ApolloProvider> | |
); | |
ReactDOM.render(<WithProvider />, document.getElementById('root')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gql from 'graphql-tag'; | |
export default gql ` | |
subscription onUpdateUser($id: String!) { | |
onUpdateUser(id: $id){ | |
id | |
} | |
} | |
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These are the updates needed in your schema to support basic changes: | |
schema { | |
query: Query | |
mutation: Mutation | |
subscription: Subscription | |
} | |
type Mutation { | |
createUser(input: CreateUserInput!): User | |
deleteUser(input: DeleteUserInput!): User | |
updateUser(input: UpdateUserInput!): User | |
} | |
type Query { | |
getUser(id: String!): User | |
} | |
type Subscription { | |
onUpdateUser( | |
updatedAt: String, | |
id: String, | |
name: String, | |
nickname: String, | |
password: String | |
): User | |
@aws_subscribe(mutations: ["updateUser"]) | |
} | |
type User { | |
events: [String] | |
id: String! | |
name: String | |
} | |
input CreateUserInput { | |
events: [String] | |
id: String! | |
name: Strings | |
} | |
input DeleteUserInput { | |
id: String! | |
} | |
input UpdateUserInput { | |
events: [String] | |
id: String! | |
name: String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment