From 7f9cc715ef5c817eaa53b3ecb0b5abcbbe87e34e Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 2 May 2024 15:06:52 -0400 Subject: [PATCH] review and edits --- src/directory/directory.mjs | 3 + .../connect-http-datasource/index.mdx | 248 +----------------- 2 files changed, 17 insertions(+), 234 deletions(-) diff --git a/src/directory/directory.mjs b/src/directory/directory.mjs index 15aa01fc14e..30447c39bd9 100644 --- a/src/directory/directory.mjs +++ b/src/directory/directory.mjs @@ -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' } ] }, diff --git a/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-http-datasource/index.mdx b/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-http-datasource/index.mdx index 0631ad5d350..745d02f3a2f 100644 --- a/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-http-datasource/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-http-datasource/index.mdx @@ -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 @@ -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 }); @@ -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; - -export const data = defineData({ - schema, - authorizationModes: { - defaultAuthorizationMode: 'apiKey', - apiKeyAuthorizationMode: { - expiresInDays: 30, - }, - }, -}); -``` - - -```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; - -export const data = defineData({ - schema, - authorizationModes: { - defaultAuthorizationMode: 'apiKey', - apiKeyAuthorizationMode: { - expiresInDays: 30, - }, - }, -}); -``` - - -```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; - -export const data = defineData({ - schema, - authorizationModes: { - defaultAuthorizationMode: 'apiKey', - apiKeyAuthorizationMode: { - expiresInDays: 30, - }, - }, -}); -``` - - -```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; - -export const data = defineData({ - schema, - authorizationModes: { - defaultAuthorizationMode: 'apiKey', - apiKeyAuthorizationMode: { - expiresInDays: 30, - }, - }, -}); -``` - - - -## 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: - - - -```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()]) @@ -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 @@ -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()]) @@ -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( @@ -479,9 +262,10 @@ export const data = defineData({ ``` + ## 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. @@ -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, }, }, }; @@ -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, }, }, }; @@ -638,7 +419,6 @@ const { data, errors } = await client.queries.getPost({ const { data, errors } = await client.mutations.updatePost({ id: "", title: "An Updated Post", - expectedVersion: 1, }); ```