Skip to content

Commit dc5bc39

Browse files
authored
Adding complex object docs (#744)
1 parent 8aa3884 commit dc5bc39

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,86 @@ export default graphql(listPosts, {
576576
})(App);
577577
```
578578

579+
### Complex objects (Apollo V2)
580+
581+
Many times you might want to create logical objects that have more complex data, such as images or videos, as part of their structure. For example, you might create a Person type with a profile picture or a Post type that has an associated image. With AWS AppSync, you can model these as GraphQL types, referred to as complex objects. If any of your mutations have a variable with bucket, key, region, mimeType and localUri fields, the SDK uploads the file to Amazon S3 for you.
582+
583+
For a complete working example of this feature, see [aws-amplify-graphql](https://github.com/aws-samples/aws-amplify-graphql) on GitHub.
584+
585+
If you're using AWS Amplify's GraphQL transformer, then configure your resolvers to write to DynamoDB and point at S3 objects when using the `S3Object` type. For example, run the following in an Amplify project:
586+
587+
```bash
588+
amplify add auth #Select default configuration
589+
amplify add storage #Select S3 with read/write access
590+
amplify add api #Select Cognito User Pool for authorization type
591+
```
592+
593+
When prompted, use the following schema:
594+
595+
```graphql
596+
type Todo @model {
597+
id: ID!
598+
name: String!
599+
description: String!
600+
file: S3Object
601+
}
602+
type S3Object {
603+
bucket: String!
604+
key: String!
605+
region: String!
606+
}
607+
input CreateTodoInput {
608+
id: ID
609+
name: String!
610+
description: String
611+
file: S3ObjectInput # This input type will be generated for you
612+
}
613+
```
614+
615+
Save and run `amplify push` to deploy changes.
616+
617+
To use complex objects you need AWS Identity and Access Management credentials for reading and writing to Amazon S3 which `amplify add auth` configures in the default setting along with a Cognito user pool. These can be separate from the other auth credentials you use in your AWS AppSync client. Credentials for complex objects are set using the `complexObjectsCredentials` parameter, which you can use with AWS Amplify and the complex objects feature like so:
618+
619+
```javascript
620+
const client = new AWSAppSyncClient({
621+
url: ENDPOINT,
622+
region: REGION,
623+
auth: { ... }, //Can be User Pools or API Key
624+
complexObjectsCredentials: () => Auth.currentCredentials(),
625+
});
626+
(async () => {
627+
let file;
628+
if (selectedFile) { // selectedFile is the file to be uploaded, typically comes from an <input type="file" />
629+
const { name, type: mimeType } = selectedFile;
630+
const [, , , extension] = /([^.]+)(\.(\w+))?$/.exec(name);
631+
const bucket = aws_config.aws_user_files_s3_bucket;
632+
const region = aws_config.aws_user_files_s3_bucket_region;
633+
const visibility = 'private';
634+
const { identityId } = await Auth.currentCredentials();
635+
const key = `${visibility}/${identityId}/${uuid()}${extension && '.'}${extension}`;
636+
file = {
637+
bucket,
638+
key,
639+
region,
640+
mimeType,
641+
localUri: selectedFile,
642+
};
643+
}
644+
const result = await client.mutate({
645+
mutation: gql(createTodo),
646+
variables: {
647+
input: {
648+
name: 'Upload file',
649+
description: 'Uses complex objects to upload',
650+
file: file,
651+
}
652+
}
653+
});
654+
})();
655+
```
656+
657+
When you run the above mutation, a record will be in a DynamoDB table for your AppSync API as well as the corresponding file in an S3 bucket.
658+
579659
### Offline configuration (Apollo V2)
580660

581661
When using the AWS AppSync SDK offline capabilities (e.g. `disableOffline: false`), you can provide configurations for the following:

0 commit comments

Comments
 (0)