Skip to content

Commit

Permalink
review and edits
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbonifacio committed May 2, 2024
1 parent e2635e6 commit 7f9cc71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 234 deletions.
3 changes: 3 additions & 0 deletions src/directory/directory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ export const directory = {
},
{
path: 'src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-amazon-translate/index.mdx'
},
{
path: 'src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-http-datasource/index.mdx'
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function getStaticProps() {
};
}

The HTTP Datasource allows you to quickly configure HTTP resolvers within your Data API.
The HTTP Datasource allows you to quickly configure HTTP resolvers within your Data API.

This guide will demonstrate how to establish a connection to an external REST API using an HTTP data source and use GraphQL mutations and queries to interact with the REST API.
This guide will demonstrate how to establish a connection to an external REST API using an HTTP data source and use Amplify Data's custom mutations and queries to interact with the REST API.

## Step 1 - Set up your custom type

Expand All @@ -52,14 +52,9 @@ const schema = a.schema({
.authorization(allow => [allow.publicApiKey()]),
// highlight-start
Post: a.customType({
id: a.id().required(),
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
author: a.string().required(),
}),
// highlight-end
});
Expand Down Expand Up @@ -111,215 +106,17 @@ import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
}),
// highlight-start
addPost: a
.mutation()
.arguments({
id: a.id(),
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
})
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
.handler(
a.handler.custom({
dataSource: "HttpDataSource",
entry: "./addPost.js",
})
),
// highlight-end
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'apiKey',
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});
```
</Block>
<Block name="getPost">
```ts title="amplify/data/resource.ts"
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
}),
// highlight-start
getPost: a
.query()
.arguments({ id: a.id().required() })
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
.handler(
a.handler.custom({
dataSource: "HttpDataSource",
entry: "./getPost.js",
})
),
// highlight-end
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'apiKey',
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});
```
</Block>
<Block name="updatePost">
```ts title="amplify/data/resource.ts"
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
}),
// highlight-start
updatePost: a
.mutation()
.arguments({
id: a.id().required(),
author: a.string(),
title: a.string(),
content: a.string(),
url: a.string(),
expectedVersion: a.integer().required(),
})
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
.handler(
a.handler.custom({
dataSource: "HttpDataSource",
entry: "./updatePost.js",
})
),
// highlight-end
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'apiKey',
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});
```
</Block>
<Block name="deletePost">
```ts title="amplify/data/resource.ts"
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
}),
// highlight-start
deletePost: a
.mutation()
.arguments({ id: a.id().required(), expectedVersion: a.integer() })
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
.handler(
a.handler.custom({
dataSource: "HttpDataSourcee",
entry: "./deletePost.js",
})
),
// highlight-end
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'apiKey',
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});
```
</Block>
</BlockSwitcher>

## Step 4 - Define the custom type and custom query

Define custom queries and mutations

Now that your REST API has been added as a HTTP Data source, you can reference it in custom queries and mutations using the `a.handler.custom()` modifier which accepts the name of the data source and an entry point for your resolvers.

Use the following code examples to add `addPost`, `getPost`, `updatePost`, and `deletePost` as custom queries and mutations to your schema:

<BlockSwitcher>
<Block name="addPost">
```ts title="amplify/data/resource.ts"
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
}),
// highlight-start
addPost: a
.mutation()
.arguments({
id: a.id(),
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
author: a.string().required(),
})
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
Expand Down Expand Up @@ -351,13 +148,9 @@ import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
author: a.string().required(),
}),
// highlight-start
getPost: a
Expand Down Expand Up @@ -393,24 +186,18 @@ import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
author: a.string().required(),
}),
// highlight-start
updatePost: a
.mutation()
.arguments({
id: a.id().required(),
author: a.string(),
title: a.string(),
content: a.string(),
url: a.string(),
expectedVersion: a.integer().required(),
author: a.string(),
})
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
Expand Down Expand Up @@ -442,18 +229,14 @@ import { type ClientSchema, a, defineData } from "@aws-amplify/backend";

const schema = a.schema({
Post: a.customType({
author: a.string().required(),
title: a.string(),
content: a.string(),
url: a.string(),
ups: a.integer(),
downs: a.integer(),
version: a.integer(),
author: a.string().required(),
}),
// highlight-start
deletePost: a
.mutation()
.arguments({ id: a.id().required(), expectedVersion: a.integer() })
.arguments({ id: a.id().required() })
.returns(a.ref("Post"))
.authorization(allow => [allow.publicApiKey()])
.handler(
Expand All @@ -479,9 +262,10 @@ export const data = defineData({
```
</Block>
</BlockSwitcher>

## Step 4 - Configure custom business logic handler code

Next, create the following files in your `amplify/data` folder and use the code examples to define custom resolvers for the custom queries and mutations added to your schema from the previous step. These are AppSync JavaScript resolvers
Next, create the following files in your `amplify/data` folder and use the code examples to define custom resolvers for the custom queries and mutations added to your schema from the previous step. These are AppSync JavaScript resolvers.

<BlockSwitcher>
<Block name="addPost">
Expand All @@ -497,11 +281,9 @@ export function request(ctx) {
"Content-Type": "application/json",
},
body: {
id: ctx.arguments.id,
author: ctx.arguments.author,
title: ctx.arguments.title,
title: ctx.arguments.title,
content: ctx.arguments.content,
url:ctx.arguments.url,
author: ctx.arguments.author,
},
},
};
Expand Down Expand Up @@ -560,10 +342,9 @@ export function request(ctx) {
"Content-Type": "application/json",
},
body: {
author: ctx.arguments.author,
title: ctx.arguments.title,
content: ctx.arguments.content,
url: ctx.arguments.url,
author: ctx.arguments.author,
},
},
};
Expand Down Expand Up @@ -638,7 +419,6 @@ const { data, errors } = await client.queries.getPost({
const { data, errors } = await client.mutations.updatePost({
id: "<post-id>",
title: "An Updated Post",
expectedVersion: 1,
});
```
</Block>
Expand Down

0 comments on commit 7f9cc71

Please sign in to comment.