Add graphql api!
This commit is contained in:
15
.graphqlconfig.yml
Normal file
15
.graphqlconfig.yml
Normal 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
|
||||
5
amplify/backend/api/lunchrun/parameters.json
Normal file
5
amplify/backend/api/lunchrun/parameters.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"AppSyncApiName": "lunchrun",
|
||||
"DynamoDBBillingMode": "PAY_PER_REQUEST",
|
||||
"DynamoDBEnableServerSideEncryption": "false"
|
||||
}
|
||||
5
amplify/backend/api/lunchrun/schema.graphql
Normal file
5
amplify/backend/api/lunchrun/schema.graphql
Normal file
@@ -0,0 +1,5 @@
|
||||
type Todo @model {
|
||||
id: ID!
|
||||
name: String!
|
||||
description: String
|
||||
}
|
||||
61
amplify/backend/api/lunchrun/stacks/CustomResources.json
Normal file
61
amplify/backend/api/lunchrun/stacks/CustomResources.json
Normal 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": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
4
amplify/backend/api/lunchrun/transform.conf.json
Normal file
4
amplify/backend/api/lunchrun/transform.conf.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"Version": 5,
|
||||
"ElasticsearchWarning": true
|
||||
}
|
||||
@@ -1 +1,20 @@
|
||||
{}
|
||||
{
|
||||
"api": {
|
||||
"lunchrun": {
|
||||
"service": "AppSync",
|
||||
"providerPlugin": "awscloudformation",
|
||||
"output": {
|
||||
"authConfig": {
|
||||
"additionalAuthenticationProviders": [],
|
||||
"defaultAuthentication": {
|
||||
"authenticationType": "API_KEY",
|
||||
"apiKeyConfig": {
|
||||
"description": "lunchrunapikey",
|
||||
"apiKeyExpirationDays": 7
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
98
src/App.js
98
src/App.js
@@ -1,26 +1,78 @@
|
||||
import React from 'react';
|
||||
import logo from './logo.svg';
|
||||
import './App.css';
|
||||
/* src/App.js */
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { API, graphqlOperation } from 'aws-amplify'
|
||||
import { createTodo } from './graphql/mutations'
|
||||
import { listTodos } from './graphql/queries'
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src={logo} className="App-logo" alt="logo" />
|
||||
<p>
|
||||
Edit <code>src/App.js</code> and save to reload.
|
||||
</p>
|
||||
<a
|
||||
className="App-link"
|
||||
href="https://reactjs.org"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Learn React
|
||||
</a>
|
||||
</header>
|
||||
</div>
|
||||
);
|
||||
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 })
|
||||
}
|
||||
|
||||
export default App;
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={styles.container}>
|
||||
<h2>Amplify Todos</h2>
|
||||
<input
|
||||
onChange={event => setInput('name', event.target.value)}
|
||||
style={styles.input}
|
||||
value={formState.name}
|
||||
placeholder="Name"
|
||||
/>
|
||||
<input
|
||||
onChange={event => setInput('description', event.target.value)}
|
||||
style={styles.input}
|
||||
value={formState.description}
|
||||
placeholder="Description"
|
||||
/>
|
||||
<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>
|
||||
)
|
||||
}
|
||||
|
||||
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
45
src/graphql/mutations.js
Normal 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
32
src/graphql/queries.js
Normal 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
2192
src/graphql/schema.json
Normal file
File diff suppressed because it is too large
Load Diff
36
src/graphql/subscriptions.js
Normal file
36
src/graphql/subscriptions.js
Normal 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
|
||||
}
|
||||
}
|
||||
`;
|
||||
Reference in New Issue
Block a user