Add graphql api!

This commit is contained in:
2020-05-24 15:51:29 -05:00
parent 0e71344d9c
commit 886d9ba62e
12 changed files with 2489 additions and 23 deletions

15
.graphqlconfig.yml Normal file
View File

@@ -0,0 +1,15 @@
projects:
lunchrun:
schemaPath: amplify/backend/api/lunchrun/build/schema.graphql
includes:
- src/graphql/**/*.js
excludes:
- ./amplify/**
extensions:
amplify:
codeGenTarget: javascript
generatedFileName: ''
docsFilePath: src/graphql
extensions:
amplify:
version: 3

View File

@@ -0,0 +1,5 @@
{
"AppSyncApiName": "lunchrun",
"DynamoDBBillingMode": "PAY_PER_REQUEST",
"DynamoDBEnableServerSideEncryption": "false"
}

View File

@@ -0,0 +1,5 @@
type Todo @model {
id: ID!
name: String!
description: String
}

View File

@@ -0,0 +1,61 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "An auto-generated nested stack.",
"Metadata": {},
"Parameters": {
"AppSyncApiId": {
"Type": "String",
"Description": "The id of the AppSync API associated with this project."
},
"AppSyncApiName": {
"Type": "String",
"Description": "The name of the AppSync API",
"Default": "AppSyncSimpleTransform"
},
"env": {
"Type": "String",
"Description": "The environment name. e.g. Dev, Test, or Production",
"Default": "NONE"
},
"S3DeploymentBucket": {
"Type": "String",
"Description": "The S3 bucket containing all deployment assets for the project."
},
"S3DeploymentRootKey": {
"Type": "String",
"Description": "An S3 key relative to the S3DeploymentBucket that points to the root\nof the deployment directory."
}
},
"Resources": {
"EmptyResource": {
"Type": "Custom::EmptyResource",
"Condition": "AlwaysFalse"
}
},
"Conditions": {
"HasEnvironmentParameter": {
"Fn::Not": [
{
"Fn::Equals": [
{
"Ref": "env"
},
"NONE"
]
}
]
},
"AlwaysFalse": {
"Fn::Equals": [
"true",
"false"
]
}
},
"Outputs": {
"EmptyOutput": {
"Description": "An empty output. You may delete this if you have at least one resource above.",
"Value": ""
}
}
}

View File

@@ -0,0 +1,4 @@
{
"Version": 5,
"ElasticsearchWarning": true
}

View File

@@ -1 +1,20 @@
{} {
"api": {
"lunchrun": {
"service": "AppSync",
"providerPlugin": "awscloudformation",
"output": {
"authConfig": {
"additionalAuthenticationProviders": [],
"defaultAuthentication": {
"authenticationType": "API_KEY",
"apiKeyConfig": {
"description": "lunchrunapikey",
"apiKeyExpirationDays": 7
}
}
}
}
}
}
}

View File

@@ -1,26 +1,78 @@
import React from 'react'; /* src/App.js */
import logo from './logo.svg'; import React, { useEffect, useState } from 'react'
import './App.css'; import { API, graphqlOperation } from 'aws-amplify'
import { createTodo } from './graphql/mutations'
import { listTodos } from './graphql/queries'
const initialState = { name: '', description: '' }
const App = () => {
const [formState, setFormState] = useState(initialState)
const [todos, setTodos] = useState([])
useEffect(() => {
fetchTodos()
}, [])
function setInput(key, value) {
setFormState({ ...formState, [key]: value })
}
async function fetchTodos() {
try {
const todoData = await API.graphql(graphqlOperation(listTodos))
const todos = todoData.data.listTodos.items
setTodos(todos)
} catch (err) { console.log('error fetching todos') }
}
async function addTodo() {
try {
if (!formState.name || !formState.description) return
const todo = { ...formState }
setTodos([...todos, todo])
setFormState(initialState)
await API.graphql(graphqlOperation(createTodo, {input: todo}))
} catch (err) {
console.log('error creating todo:', err)
}
}
function App() {
return ( return (
<div className="App"> <div style={styles.container}>
<header className="App-header"> <h2>Amplify Todos</h2>
<img src={logo} className="App-logo" alt="logo" /> <input
<p> onChange={event => setInput('name', event.target.value)}
Edit <code>src/App.js</code> and save to reload. style={styles.input}
</p> value={formState.name}
<a placeholder="Name"
className="App-link" />
href="https://reactjs.org" <input
target="_blank" onChange={event => setInput('description', event.target.value)}
rel="noopener noreferrer" style={styles.input}
> value={formState.description}
Learn React placeholder="Description"
</a> />
</header> <button style={styles.button} onClick={addTodo}>Create Todo</button>
{
todos.map((todo, index) => (
<div key={todo.id ? todo.id : index} style={styles.todo}>
<p style={styles.todoName}>{todo.name}</p>
<p style={styles.todoDescription}>{todo.description}</p>
</div>
))
}
</div> </div>
); )
} }
export default App; const styles = {
container: { width: 400, margin: '0 auto', display: 'flex', flex: 1, flexDirection: 'column', justifyContent: 'center', padding: 20 },
todo: { marginBottom: 15 },
input: { border: 'none', backgroundColor: '#ddd', marginBottom: 10, padding: 8, fontSize: 18 },
todoName: { fontSize: 20, fontWeight: 'bold' },
todoDescription: { marginBottom: 0 },
button: { backgroundColor: 'black', color: 'white', outline: 'none', fontSize: 18, padding: '12px 0px' }
}
export default App

45
src/graphql/mutations.js Normal file
View File

@@ -0,0 +1,45 @@
/* eslint-disable */
// this is an auto generated file. This will be overwritten
export const createTodo = /* GraphQL */ `
mutation CreateTodo(
$input: CreateTodoInput!
$condition: ModelTodoConditionInput
) {
createTodo(input: $input, condition: $condition) {
id
name
description
createdAt
updatedAt
}
}
`;
export const updateTodo = /* GraphQL */ `
mutation UpdateTodo(
$input: UpdateTodoInput!
$condition: ModelTodoConditionInput
) {
updateTodo(input: $input, condition: $condition) {
id
name
description
createdAt
updatedAt
}
}
`;
export const deleteTodo = /* GraphQL */ `
mutation DeleteTodo(
$input: DeleteTodoInput!
$condition: ModelTodoConditionInput
) {
deleteTodo(input: $input, condition: $condition) {
id
name
description
createdAt
updatedAt
}
}
`;

32
src/graphql/queries.js Normal file
View File

@@ -0,0 +1,32 @@
/* eslint-disable */
// this is an auto generated file. This will be overwritten
export const getTodo = /* GraphQL */ `
query GetTodo($id: ID!) {
getTodo(id: $id) {
id
name
description
createdAt
updatedAt
}
}
`;
export const listTodos = /* GraphQL */ `
query ListTodos(
$filter: ModelTodoFilterInput
$limit: Int
$nextToken: String
) {
listTodos(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
description
createdAt
updatedAt
}
nextToken
}
}
`;

2192
src/graphql/schema.json Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,36 @@
/* eslint-disable */
// this is an auto generated file. This will be overwritten
export const onCreateTodo = /* GraphQL */ `
subscription OnCreateTodo {
onCreateTodo {
id
name
description
createdAt
updatedAt
}
}
`;
export const onUpdateTodo = /* GraphQL */ `
subscription OnUpdateTodo {
onUpdateTodo {
id
name
description
createdAt
updatedAt
}
}
`;
export const onDeleteTodo = /* GraphQL */ `
subscription OnDeleteTodo {
onDeleteTodo {
id
name
description
createdAt
updatedAt
}
}
`;

View File

@@ -5,7 +5,7 @@ import App from './App';
import * as serviceWorker from './serviceWorker'; import * as serviceWorker from './serviceWorker';
import Amplify from "aws-amplify"; import Amplify from "aws-amplify";
import awsExports from "./aws-exports"; import awsExports from "./aws-exports";
Amplify.configure(awsExports); Amplify.configure(awsExports);
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>