Skip to content

Instantly share code, notes, and snippets.

View dabit3's full-sized avatar
🎡
probably nothing

Nader Dabit dabit3

🎡
probably nothing
View GitHub Profile
@dabit3
dabit3 / ssr.js
Last active December 30, 2020 08:20
Enabling SSR support in Amplify
// Enable SSR support in Amplify app
Amplify.configure({ ...config, ssr: true })
// Library usage in SSR or API routes
import { withSSRContext } from 'aws-amplify'
const { Auth, API } = withSSRContext({ req })
// Get user session
const user = await Auth.currentAuthenticatedUser()
@dabit3
dabit3 / customauthentication.js
Created October 8, 2020 15:23
Custom authentication flow with Amplify JS
import React, { useState, useEffect } from 'react';
import { API, Auth } from 'aws-amplify'
import { listPosts } from './graphql/queries'
const initialState = {
formState: 'signUp', username: '', password: '', email: '', authCode: ''
}
export default function App() {
const [posts, setPosts] = useState([])
@dabit3
dabit3 / testing-graphql.sh
Created September 17, 2020 15:28
GraphQL requests using CURL
# query
curl \
-X POST \
-H "x-api-key: xxx-xxxx" \
-H "Content-Type: application/json" \
-d '{ "query": "query { listUsers { name } }" }' \
https://app-id.appsync-api.us-east-1.amazonaws.com/graphql
# mutation with variables
curl \
@dabit3
dabit3 / infinitelistexample.js
Last active September 16, 2020 16:26
Infiinite List Example
import { useState, useEffect } from 'react';
import { List, message, Avatar, Spin } from 'antd';
import reqwest from 'reqwest';
import InfiniteScroll from 'react-infinite-scroller';
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
const InfiniteListExample = () => {
let [data, setData] = useState([])
@dabit3
dabit3 / auth.js
Last active January 17, 2021 02:39
Next.js Authentication Component
import React, { useEffect, useState } from 'react';
import Auth from 'aws-amplify'
import { css } from 'emotion';
import { useRouter } from 'next/router'
const primaryColor = "rgba(0,118,255,0.9)"
const intitialFormState = {
username: '',
email: '',
password: '',
@dabit3
dabit3 / schema.graphql
Created September 10, 2020 22:05
Example auth combinations
# Public and owner access
type Post @model
@auth(
{ allow: owner },
{ allow: public, operations: [read] }
)
{
id: ID!
content: String!
}
@dabit3
dabit3 / createUser.js
Created September 1, 2020 00:02
Creating a user
const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient()
async function createUser(user) {
const params = {
TableName: "AppSyncUserTable",
Item: user
}
// user should look like this: user = { id: "3", name: "Chris", location: "Canada" }
try {
@dabit3
dabit3 / SingleTableAppSync.md
Last active February 24, 2023 20:05
GraphQL Single Table Design with DynamoDB and AWS AppSync

GraphQL

GraphQL Schema

type Customer {
  id: ID!
  email: String!
}
type Address
@model
@key(name: "byJob", fields: ["jobId", "addressLine1"])
@auth(
rules: [
{ allow: groups, groups: ["Admin"], queries: [get, list], mutations: [update, delete] }
{ allow: public, operations: [read], queries: [get, list], mutations: [create] }
{ allow: private, operations: [read], mutations: [create] }
]
) {
@dabit3
dabit3 / chat-api.ts
Last active August 21, 2020 18:28
Example AppSync GraphQL API
/* Imports from CDK */
import * as appsync from '@aws-cdk/aws-appsync';
import * as db from '@aws-cdk/aws-dynamodb';
/* Creating the API */
const api = new appsync.GraphQLApi(this, 'Api', {
name: 'my-chat-app', // API name
schemaDefinition: appsync.SchemaDefinition.FILE, // Type of schema
schemaDefinitionFile: './schema.graphql', // location of schema
authorizationConfig: { // authorization config