Skip to content

add section for sql custom query/mutation return types with example #7816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,34 @@ VALUES (:name, :address, ST_SetSRID(ST_MakePoint(:long, :lat), 4326))
</Block>
</BlockSwitcher>

<Callout info>
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()]),
});
```
</Callout>

## 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.
Expand Down Expand Up @@ -406,4 +434,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';

Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,31 @@ type Mutation {
</BlockSwitcher>

<Callout info>
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()]),
});
```
</Callout>


Expand Down