Skip to content

Commit 42d0316

Browse files
committed
Initial commit
0 parents  commit 42d0316

29 files changed

+842
-0
lines changed

.gitignore

Whitespace-only changes.

AppSync/Mutation.addPicture.request

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version" : "2017-02-28",
3+
"operation" : "PutItem",
4+
"key" : {
5+
"id" : { "S" : "${util.autoId()}" }
6+
},
7+
8+
#set( $attribs = $util.dynamodb.toMapValues($ctx.args) )
9+
#set( $attribs.owner = $util.dynamodb.toDynamoDB($util.defaultIfNullOrBlank($context.identity.claims.username, $util.defaultIfNull($context.identity.username, 'ANON'))))
10+
#set( $attribs.file = $util.dynamodb.toS3Object($ctx.args.file.key, $ctx.args.file.bucket, $ctx.args.file.region, $ctx.args.file.version))
11+
#set( $attribs.createdAt = $util.dynamodb.toDynamoDB($util.time.nowISO8601()))
12+
"attributeValues" : $util.toJson($attribs)
13+
}

AppSync/Picture.file.request

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "2017-02-28",
3+
"payload": {}
4+
}

AppSync/Picture.file.response

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$util.toJson($util.dynamodb.fromS3ObjectJson($context.source.file))

AppSync/Query.listPictures.request

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "2017-02-28",
3+
"operation": "Query",
4+
"query": {
5+
"expression": "#owner = :owner",
6+
"expressionNames": {
7+
"#owner": "owner",
8+
},
9+
"expressionValues": {
10+
":owner": {"S": "$util.defaultIfNull($context.identity.claims.username, $util.defaultIfNull($context.identity.username, 'ANON'))"},
11+
},
12+
},
13+
"index": "owner-index",
14+
"limit": $util.defaultIfNull($ctx.args.first, 20),
15+
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
16+
"scanIndexForward": true,
17+
"select": "ALL_ATTRIBUTES",
18+
}

AppSync/schema.graphql

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
type Mutation {
2+
addPicture(name: String, visibility: Visibility, file: S3ObjectInput): Picture
3+
}
4+
5+
type Picture {
6+
id: ID!
7+
name: String
8+
visibility: Visibility
9+
owner: String
10+
file: S3Object
11+
createdAt: String
12+
}
13+
14+
type Query {
15+
empty: Boolean
16+
}
17+
18+
type S3Object {
19+
bucket: String!
20+
region: String!
21+
key: String!
22+
}
23+
24+
input S3ObjectInput {
25+
bucket: String!
26+
region: String!
27+
localUri: String
28+
visibility: Visibility
29+
key: String
30+
mimeType: String
31+
}
32+
33+
enum Visibility {
34+
public
35+
private
36+
}
37+
38+
schema {
39+
query: Query
40+
mutation: Mutation
41+
}

CODE_OF_CONDUCT.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Code of Conduct
2+
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
3+
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
4+
opensource-codeofconduct@amazon.com with any additional questions or comments.

CONTRIBUTING.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Contributing Guidelines
2+
3+
Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional
4+
documentation, we greatly value feedback and contributions from our community.
5+
6+
Please read through this document before submitting any issues or pull requests to ensure we have all the necessary
7+
information to effectively respond to your bug report or contribution.
8+
9+
10+
## Reporting Bugs/Feature Requests
11+
12+
We welcome you to use the GitHub issue tracker to report bugs or suggest features.
13+
14+
When filing an issue, please check [existing open](https://github.com/aws-samples/aws-amplify-graphql/issues), or [recently closed](https://github.com/aws-samples/aws-amplify-graphql/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20), issues to make sure somebody else hasn't already
15+
reported the issue. Please try to include as much information as you can. Details like these are incredibly useful:
16+
17+
* A reproducible test case or series of steps
18+
* The version of our code being used
19+
* Any modifications you've made relevant to the bug
20+
* Anything unusual about your environment or deployment
21+
22+
23+
## Contributing via Pull Requests
24+
Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that:
25+
26+
1. You are working against the latest source on the *master* branch.
27+
2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already.
28+
3. You open an issue to discuss any significant work - we would hate for your time to be wasted.
29+
30+
To send us a pull request, please:
31+
32+
1. Fork the repository.
33+
2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change.
34+
3. Ensure local tests pass.
35+
4. Commit to your fork using clear commit messages.
36+
5. Send us a pull request, answering any default questions in the pull request interface.
37+
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.
38+
39+
GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
40+
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).
41+
42+
43+
## Finding contributions to work on
44+
Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels ((enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any ['help wanted'](https://github.com/aws-samples/aws-amplify-graphql/labels/help%20wanted) issues is a great place to start.
45+
46+
47+
## Code of Conduct
48+
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
49+
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
50+
opensource-codeofconduct@amazon.com with any additional questions or comments.
51+
52+
53+
## Security issue notifications
54+
If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue.
55+
56+
57+
## Licensing
58+
59+
See the [LICENSE](https://github.com/aws-samples/aws-amplify-graphql/blob/master/LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution.
60+
61+
We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes.

LICENSE

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Amazon Software License 1.0
2+
3+
This Amazon Software License ("License") governs your use, reproduction, and
4+
distribution of the accompanying software as specified below.
5+
6+
1. Definitions
7+
8+
"Licensor" means any person or entity that distributes its Work.
9+
10+
"Software" means the original work of authorship made available under this
11+
License.
12+
13+
"Work" means the Software and any additions to or derivative works of the
14+
Software that are made available under this License.
15+
16+
The terms "reproduce," "reproduction," "derivative works," and
17+
"distribution" have the meaning as provided under U.S. copyright law;
18+
provided, however, that for the purposes of this License, derivative works
19+
shall not include works that remain separable from, or merely link (or bind
20+
by name) to the interfaces of, the Work.
21+
22+
Works, including the Software, are "made available" under this License by
23+
including in or with the Work either (a) a copyright notice referencing the
24+
applicability of this License to the Work, or (b) a copy of this License.
25+
26+
2. License Grants
27+
28+
2.1 Copyright Grant. Subject to the terms and conditions of this License,
29+
each Licensor grants to you a perpetual, worldwide, non-exclusive,
30+
royalty-free, copyright license to reproduce, prepare derivative works of,
31+
publicly display, publicly perform, sublicense and distribute its Work and
32+
any resulting derivative works in any form.
33+
34+
2.2 Patent Grant. Subject to the terms and conditions of this License, each
35+
Licensor grants to you a perpetual, worldwide, non-exclusive, royalty-free
36+
patent license to make, have made, use, sell, offer for sale, import, and
37+
otherwise transfer its Work, in whole or in part. The foregoing license
38+
applies only to the patent claims licensable by Licensor that would be
39+
infringed by Licensor's Work (or portion thereof) individually and
40+
excluding any combinations with any other materials or technology.
41+
42+
3. Limitations
43+
44+
3.1 Redistribution. You may reproduce or distribute the Work only if
45+
(a) you do so under this License, (b) you include a complete copy of this
46+
License with your distribution, and (c) you retain without modification
47+
any copyright, patent, trademark, or attribution notices that are present
48+
in the Work.
49+
50+
3.2 Derivative Works. You may specify that additional or different terms
51+
apply to the use, reproduction, and distribution of your derivative works
52+
of the Work ("Your Terms") only if (a) Your Terms provide that the use
53+
limitation in Section 3.3 applies to your derivative works, and (b) you
54+
identify the specific derivative works that are subject to Your Terms.
55+
Notwithstanding Your Terms, this License (including the redistribution
56+
requirements in Section 3.1) will continue to apply to the Work itself.
57+
58+
3.3 Use Limitation. The Work and any derivative works thereof only may be
59+
used or intended for use with the web services, computing platforms or
60+
applications provided by Amazon.com, Inc. or its affiliates, including
61+
Amazon Web Services, Inc.
62+
63+
3.4 Patent Claims. If you bring or threaten to bring a patent claim against
64+
any Licensor (including any claim, cross-claim or counterclaim in a
65+
lawsuit) to enforce any patents that you allege are infringed by any Work,
66+
then your rights under this License from such Licensor (including the
67+
grants in Sections 2.1 and 2.2) will terminate immediately.
68+
69+
3.5 Trademarks. This License does not grant any rights to use any
70+
Licensor's or its affiliates' names, logos, or trademarks, except as
71+
necessary to reproduce the notices described in this License.
72+
73+
3.6 Termination. If you violate any term of this License, then your rights
74+
under this License (including the grants in Sections 2.1 and 2.2) will
75+
terminate immediately.
76+
77+
4. Disclaimer of Warranty.
78+
79+
THE WORK IS PROVIDED "AS IS" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
80+
EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF
81+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR
82+
NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY ACTIVITIES UNDER
83+
THIS LICENSE. SOME STATES' CONSUMER LAWS DO NOT ALLOW EXCLUSION OF AN
84+
IMPLIED WARRANTY, SO THIS DISCLAIMER MAY NOT APPLY TO YOU.
85+
86+
5. Limitation of Liability.
87+
88+
EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL
89+
THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE
90+
SHALL ANY LICENSOR BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY DIRECT,
91+
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR
92+
RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK (INCLUDING
93+
BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION, LOST PROFITS
94+
OR DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER COMM ERCIAL DAMAGES
95+
OR LOSSES), EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF
96+
SUCH DAMAGES.

NOTICE

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
AWS Amplify for with GraphQL sample
2+
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# AWS AppSync GraphQL Photo Sample
2+
3+
![Demo](media/demo.gif)
4+
5+
This sample application shows how to use GraphQL to build an application that a user can login to the system, then upload and download photos which are private to them. The sample is written in React and uses AWS AppSync, Amazon Cognito, Amazon DynamoDB and Amazon S3 as well as the AWS Mobile CLI.
6+
7+
## Architecture Overview
8+
![Architecture](media/architecture_diagram.png)
9+
10+
## Prerequisites
11+
+ [AWS Account](https://aws.amazon.com/mobile/details/)
12+
13+
+ [NodeJS](https://nodejs.org/en/download/) with [NPM](https://docs.npmjs.com/getting-started/installing-node)
14+
15+
+ [AWS Mobile CLI](https://github.com/aws/awsmobile-cli)
16+
- `npm install -g awsmobile-cli`
17+
18+
19+
## Getting Started
20+
1. Clone this repo locally.
21+
22+
2. Navigate to the [AWS AppSync Console](https://us-east-2.console.aws.amazon.com/appsync/home). Select **Create API** with a name of **Pictures**, select **Custom Schema** and click **Create**.
23+
24+
3. Click **Schema** navigation on the left. Copy the contents of `./AppSync/schema.graphql` into the console editor and press **Save**.
25+
26+
4. Still on the Schema page, press **Create Resources** in the upper right corner.
27+
- Select the **Picture** type from the drop down.
28+
- Then click on **Additional Indexes** followed by **Add Index**, set the index name as **owner-index** with Primary key as **owner** and sort key as **id**.
29+
- And finally click **Create** at the bottom.
30+
31+
This will create DynamoDB tables, some default queries/mutations and connect them to Resolvers
32+
33+
5. Click the **Attach** button next to `addPicture` on the right of the schema editor and select **PictureTable** as the data source. For the request mapping template use the code in `./AppSync/Mutation.addPicture.request` and for the response mapping template select **Return single item** from the drop-down. Press **Save** at the bottom.
34+
35+
6. Click the **Data Sources** navigation on the left, select **New** and create a data source with **None** as the type with a name of **Local**.
36+
37+
7. Go back to the Schema page and on the right hand side under the **Picture** type click the **Attach** button next to the `file:S3Object` field and select the **Local** data source that you just created. For the request mapping template use the code in `./AppSync/Picture.file.request` and for the response mapping template use `./AppSync/Picture.file.response`. Press **Save** at the bottom.
38+
39+
8. Navigate to the Schema page again and on the right hand side find `listPictures(...):[PictureConnection]` under the Query type and click the resolver to edit it. Update the **REQUEST** mapping template with the code in `./AppSync/Query.listPictures.request`. Use "Return single item" as the response template. Press **Save** at the bottom.
40+
- This will use fine grained authorization controls to filter GraphQL responses so that users only see their data from DynamoDB.
41+
42+
9. Now use the [awsmobile-cli](https://github.com/aws/awsmobile-cli) to create your Amazon Cognito User Pool and Identity Pool, as well as an Amazon S3 bucket with private directories to store each user's photo:
43+
44+
```
45+
cd ./photo-client
46+
awsmobile init --yes
47+
awsmobile user-signin enable
48+
awsmobile user-files enable
49+
awsmobile push
50+
```
51+
52+
This process will configure all the per-user security settings when users register and login to your application, and automatically download the configuration file to `./photo-client/src/aws-exports.js`. The sample uses [AWS Amplify](https://github.com/aws/aws-amplify) to perform the Sign-Up and Sign-In flows with a Higher Order Component.
53+
54+
10. On AppSync console, click the **Settings** tab on the left and select **Amazon Cognito User Pool** as the _Authorization Type_.
55+
- Select user pool region
56+
- Select the user pool (You can type **photo** and that will filter the user pool)
57+
- Set default action **ALLOW**
58+
- Click **Save**.
59+
60+
11. On the left navigation of the AppSync console, select the root of your API (called **Pictures**) and scroll down to the bottom where **Web** is listed. Click the **Download** button and save the `AppSync.js` file into the `./photo-client/src` directory.
61+
62+
63+
12. Finally in a terminal navigate to your cloned repo and run the application:
64+
65+
```
66+
cd ./photo-client
67+
yarn start
68+
```
69+
70+
If the application runs successfully you should be able to enter the name of a photo, choose a file and then press **Add photo**. This will make a GraphQL call to enter the record into the database and simultaneously upload the object to S3. An immediate fetch of the record will then be at the bottom of the screen.

media/architecture_diagram.png

147 KB
Loading

media/demo.gif

6.34 MB
Loading

photo-client/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "photo-client",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"aws-amplify": "^0.2.9",
7+
"aws-amplify-react": "^0.1.34",
8+
"aws-appsync": "^1.0.11",
9+
"aws-appsync-react": "^1.0.5",
10+
"aws-sdk": "^2.177.0",
11+
"graphql-tag": "^2.6.1",
12+
"react": "^16.2.0",
13+
"react-apollo": "^2.0.4",
14+
"react-dom": "^16.2.0",
15+
"react-scripts": "1.0.17",
16+
"semantic-ui-css": "^2.2.14",
17+
"semantic-ui-react": "^0.78.2",
18+
"uuid": "^3.1.0"
19+
},
20+
"scripts": {
21+
"start": "react-scripts start",
22+
"build": "react-scripts build",
23+
"test": "react-scripts test --env=jsdom",
24+
"eject": "react-scripts eject"
25+
}
26+
}

photo-client/public/favicon.ico

3.78 KB
Binary file not shown.

photo-client/public/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<meta name="theme-color" content="#000000">
7+
<!--
8+
manifest.json provides metadata used when your web app is added to the
9+
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
10+
-->
11+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
12+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
13+
<!--
14+
Notice the use of %PUBLIC_URL% in the tags above.
15+
It will be replaced with the URL of the `public` folder during the build.
16+
Only files inside the `public` folder can be referenced from the HTML.
17+
18+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
19+
work correctly both with client-side routing and a non-root public URL.
20+
Learn how to configure a non-root public URL by running `npm run build`.
21+
-->
22+
<title>React App</title>
23+
</head>
24+
<body>
25+
<noscript>
26+
You need to enable JavaScript to run this app.
27+
</noscript>
28+
<div id="root"></div>
29+
<!--
30+
This HTML file is a template.
31+
If you open it directly in the browser, you will see an empty page.
32+
33+
You can add webfonts, meta tags, or analytics to this file.
34+
The build step will place the bundled scripts into the <body> tag.
35+
36+
To begin the development, run `npm start` or `yarn start`.
37+
To create a production bundle, use `npm run build` or `yarn build`.
38+
-->
39+
</body>
40+
</html>

0 commit comments

Comments
 (0)