From cf023d950ab4ba63de97f4dde07be5dfdb1816e2 Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 11 Jul 2024 07:27:14 -0400 Subject: [PATCH 1/8] add callout for sql custom query/mutation return types with example --- .../connect-postgres-mysql-database/index.mdx | 31 ++++++++++++++++++- .../index.mdx | 27 ++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx index a720275fa95..5add870e53d 100644 --- a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx @@ -327,6 +327,34 @@ VALUES (:name, :address, ST_SetSRID(ST_MakePoint(:long, :lat), 4326)) + +The return type for custom queries and mutations expecting to return row data from SQL statements must +be an array of the corresponding model. This is true even for custom `get` queries, where a single row is expected. + +**Example** + +```ts +sqlSchema.addToSchema({ + getPostBySlug: a + .query() + .arguments({ + slug: a.string().required(), + }) + // highlight-start + .returns(a.ref("Post").array()) + // highlight-end + .handler( + a.handler.inlineSql(` + SELECT id, title, slug, content, created_at, updated_at + FROM posts + WHERE slug = :slug; + `) + ) + .authorization((allow) => [allow.guest()]), +}); +``` + + ## How does it work? The Amplify uses AWS Lambda functions to enable features like querying data from your database. To work properly, these Lambda functions need access to common logic and dependencies. @@ -418,4 +446,5 @@ To return the actual SQL error instead of a generic error from underlying API re ### My SQL table doesn't get generated when running `npx ampx generate schema-from-database` -This is likely because the table doesn't have a designated primary key. A primary key is required for `npx ampx generate schema-from-database` to infer the table structure and create a create, read, update, and delete API. +This is likely because the table doesn't have a designated primary key. A primary key is required for `npx ampx generate schema-from-database` to infer the table structure and create a create, read, update, and delete API.import ts from 'typescript'; + diff --git a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx index 1a09d114375..7520aa372b5 100644 --- a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx +++ b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx @@ -498,8 +498,31 @@ type Mutation { - The return type for custom queries and mutations expecting row data must - be an array of the corresponding model. +The return type for custom queries and mutations expecting to return row data from SQL statements must +be an array of the corresponding model. This is true even for custom `get` queries, where a single row is expected. + +**Example** + +```ts +sqlSchema.addToSchema({ + getPostBySlug: a + .query() + .arguments({ + slug: a.string().required(), + }) + // highlight-start + .returns(a.ref("Post").array()) + // highlight-end + .handler( + a.handler.inlineSql(` + SELECT id, title, slug, content, created_at, updated_at + FROM posts + WHERE slug = :slug; + `) + ) + .authorization((allow) => [allow.guest()]), +}); +``` From d360ed338719177e340c75f92464e9606b30ae8d Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 11 Jul 2024 07:34:49 -0400 Subject: [PATCH 2/8] add gen 1 example --- .../index.mdx | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx index 7520aa372b5..39a7756fccb 100644 --- a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx +++ b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx @@ -504,24 +504,13 @@ be an array of the corresponding model. This is true even for custom `get` queri **Example** ```ts -sqlSchema.addToSchema({ - getPostBySlug: a - .query() - .arguments({ - slug: a.string().required(), - }) - // highlight-start - .returns(a.ref("Post").array()) - // highlight-end - .handler( - a.handler.inlineSql(` - SELECT id, title, slug, content, created_at, updated_at - FROM posts - WHERE slug = :slug; - `) +type Query { + getPostBySlug(slug: String!): [Post] + @sql( + statement: "SELECT * FROM posts WHERE slug = :slug LIMIT 1;" ) - .authorization((allow) => [allow.guest()]), -}); + @auth(rules: [{ allow: public }]) +} ``` From b899989dc3f17aaaa915935818d11561653b3d0e Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 11 Jul 2024 07:36:53 -0400 Subject: [PATCH 3/8] shorten gen 2 example a bit --- .../connect-postgres-mysql-database/index.mdx | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx index 5d9200674c2..b143212a3a7 100644 --- a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx @@ -322,24 +322,21 @@ be an array of the corresponding model. This is true even for custom `get` queri **Example** ```ts -sqlSchema.addToSchema({ - getPostBySlug: a - .query() - .arguments({ - slug: a.string().required(), - }) - // highlight-start - .returns(a.ref("Post").array()) - // highlight-end - .handler( - a.handler.inlineSql(` - SELECT id, title, slug, content, created_at, updated_at - FROM posts - WHERE slug = :slug; - `) - ) - .authorization((allow) => [allow.guest()]), -}); +getPostBySlug: a + .query() + .arguments({ + slug: a.string().required(), + }) + // highlight-start + .returns(a.ref("Post").array()) + // highlight-end + .handler( + a.handler.inlineSql(` + SELECT id, title, slug, content, created_at, updated_at + FROM posts + WHERE slug = :slug; + `) + ) ``` From 56411cc80b72c4bc7cceb743e31236b82abf247c Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 11 Jul 2024 07:37:12 -0400 Subject: [PATCH 4/8] remove line breaks from gen 1 example --- .../graphqlapi/connect-api-to-existing-database/index.mdx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx index 39a7756fccb..18dd52f4253 100644 --- a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx +++ b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx @@ -506,9 +506,7 @@ be an array of the corresponding model. This is true even for custom `get` queri ```ts type Query { getPostBySlug(slug: String!): [Post] - @sql( - statement: "SELECT * FROM posts WHERE slug = :slug LIMIT 1;" - ) + @sql(statement: "SELECT * FROM posts WHERE slug = :slug LIMIT 1;") @auth(rules: [{ allow: public }]) } ``` From 8db261cd6da50cbcf1c3a9a444a5585551ca0180 Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 11 Jul 2024 10:41:05 -0400 Subject: [PATCH 5/8] remove line break --- .../connect-postgres-mysql-database/index.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx index b143212a3a7..65ef7f95a36 100644 --- a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx @@ -316,8 +316,7 @@ VALUES (:name, :address, ST_SetSRID(ST_MakePoint(:long, :lat), 4326)) -The return type for custom queries and mutations expecting to return row data from SQL statements must -be an array of the corresponding model. This is true even for custom `get` queries, where a single row is expected. +The return type for custom queries and mutations expecting to return row data from SQL statements must be an array of the corresponding model. This is true even for custom `get` queries, where a single row is expected. **Example** From bed027b750e79546b794f69bdc67715d5716cf61 Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Thu, 11 Jul 2024 23:51:19 -0400 Subject: [PATCH 6/8] Update index.mdx Co-authored-by: josef --- .../graphqlapi/connect-api-to-existing-database/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx index 18dd52f4253..b6cb92f0048 100644 --- a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx +++ b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx @@ -503,7 +503,7 @@ be an array of the corresponding model. This is true even for custom `get` queri **Example** -```ts +```graphql title="schema.graphql" type Query { getPostBySlug(slug: String!): [Post] @sql(statement: "SELECT * FROM posts WHERE slug = :slug LIMIT 1;") From 64be13b9689cb3d467ce98ae557b80865657623e Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Fri, 12 Jul 2024 11:31:19 -0400 Subject: [PATCH 7/8] Update src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx Co-authored-by: josef --- .../graphqlapi/connect-api-to-existing-database/index.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx index b6cb92f0048..ddc737d0132 100644 --- a/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx +++ b/src/pages/gen1/[platform]/build-a-backend/graphqlapi/connect-api-to-existing-database/index.mdx @@ -498,8 +498,7 @@ type Mutation { -The return type for custom queries and mutations expecting to return row data from SQL statements must -be an array of the corresponding model. This is true even for custom `get` queries, where a single row is expected. +The return type for custom queries and mutations expecting to return row data from SQL statements must be an array of the corresponding model. This is true even for custom `get` queries, where a single row is expected. **Example** From 740e05e28ec4d81686adad35293c97198ad0c47b Mon Sep 17 00:00:00 2001 From: Chris Bonifacio Date: Fri, 12 Jul 2024 12:41:01 -0400 Subject: [PATCH 8/8] remove ts import from bottom of page --- .../connect-postgres-mysql-database/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx index 65ef7f95a36..5b7f0242fd1 100644 --- a/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/index.mdx @@ -430,5 +430,5 @@ To return the actual SQL error instead of a generic error from underlying API re ### My SQL table doesn't get generated when running `npx ampx generate schema-from-database` -This is likely because the table doesn't have a designated primary key. A primary key is required for `npx ampx generate schema-from-database` to infer the table structure and create a create, read, update, and delete API.import ts from 'typescript'; +This is likely because the table doesn't have a designated primary key. A primary key is required for `npx ampx generate schema-from-database` to infer the table structure and create a create, read, update, and delete API.