Skip to content

Instantly share code, notes, and snippets.

View jaunesarmiento's full-sized avatar

Jaune Carlo Sarmiento jaunesarmiento

View GitHub Profile
type Message
@model
@key(name: "byChannelID", fields: ["channelID", "createdAt"], queryField: "messagesByChannelID") {
id: ID!
channelID: ID!
author: String!
body: String!
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
import '@aws-amplify/pubsub';
import { onCreateMessage } from '../graphql/subscriptions';
useEffect(() => {
API
.graphql(graphqlOperation(messagesByChannelID, {
channelID: '1',
sortDirection: 'ASC'
}))
.then((response) => {
const items = response?.data?.messagesByChannelID?.items;
if (items) {
.app {
display: flex;
width: 100%;
flex-direction: column;
align-items: center;
}
.header {
position: absolute;
top: 0;
function App() {
// ... rest of our code here
return (
<div className="app">
<div className="header">
<AmplifySignOut />
</div>
<div className="container">
import { withAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react';
import React, { useEffect, useState } from 'react';
import Amplify from '@aws-amplify/core';
import API, { graphqlOperation } from '@aws-amplify/api';
import '@aws-amplify/pubsub';
import { createMessage } from './graphql/mutations';
import { onCreateMessage } from './graphql/subscriptions';
import { messagesByChannelID } from './graphql/queries';
// ... import statements here
function App() {
// Placeholder function for handling changes to our chat bar
const handleChange = () => {};
// Placeholder function for handling the form submission
const handleSubmit = () => {};
{
provider: { ... }, // this contains AppSync subscription metadata
value: {
data: {
onCreateMessage: {
id: "374fdfe6-71e4-4e02-b41f-d9d78037c916",
channelID: "1",
author: "Dave",
body: "This should be the new message",
createdAt: "2020-08-01T04:43:58.335Z",
const [messageBody, setMessageBody] = useState('');
// useEffect(...) for loading initial messages
// useEffect(...) for subscriptions
const handleChange = (event) => {
setMessageBody(event.target.value);
};