aws-amplify / aws-amplify/amplify-data

Feature Request: Amplify Gen 2 - Define Global SelectionSets in resource.ts

Open
#458 5 comments 2 reactions 1 assignee Claimed by @stocaaro View on GitHub
data-schema feature-request Gen 2 GraphQL pending-community-response
Dominant language
TypeScript
Stars
18
Forks
23
Avg merge
26m
Merged PRs (30d)
1

Description

### Is this related to a new or existing framework?

React Native

### Is this related to a new or existing API?

GraphQL API, New API

### Is this related to another service?

DynamoDB - Gen 2

### Describe the feature you'd like to request

Right now you have to define Selection Sets on your Gen 2 Queries each and every time you call them:
```typescript
const response = await client.models.Friendship.listFriendshipsByReceiverId(
{ receiverId: currentUser.userId },
{ selectionSet: friendshipSelectionSet },
)
```

This also requires you define global selectionSet types like so:

```typescript
export const friendshipSelectionSet = [
"id",
"status",
"senderId",
"receiverId",
"receiver.*", // want deeper hasMany relationship data included
"sender.*", // want deeper hasMany relationship data included
"createdAt",
"updatedAt",
] as const

export type AWSFriendship = SelectionSet<
Schema["Friendship"]["type"],
typeof friendshipSelectionSet
>
```

But you may call this same type of query many times and you want the selectionSet the same every time.

### Describe the solution you'd like

I would like it to be as simple as defining it within the schema in backend.ts:
One option:
```typescript
Friendship: a
.model({
id: a.id().required(),
receiverId: a.id().required(),
receiver: a.belongsTo("User", "receiverId").includeWithSelectionSet(), // added extra tag
senderId: a.id().required(),
sender: a.belongsTo("User", "senderId").includeWithSelectionSet(), // added extra tag
status: a.ref("FriendStatus").required(),
})
.authorization((allow) => [allow.publicApiKey()])
.secondaryIndexes((index) => [
index("senderId")
.name("bySender")
.sortKeys(["receiverId"])
.queryField("listFriendshipsBySenderId"),
index("receiverId")
.name("byReceiver")
.sortKeys(["senderId"])
.queryField("listFriendshipsByReceiverId"),
]),
```

Option 2:
```typescript
Friendship: a
.model({
id: a.id().required(),
receiverId: a.id().required(),
receiver: a.belongsTo("User", "receiverId")
senderId: a.id().required(),
sender: a.belongsTo("User", "senderId")
status: a.ref("FriendStatus").required(),
})
.authorization((allow) => [allow.publicApiKey()])
.secondaryIndexes((index) => [
index("senderId")
.name("bySender")
.sortKeys(["receiverId"])
.queryField("listFriendshipsBySenderId"),
index("receiverId")
.name("byReceiver")
.sortKeys(["senderId"])
.queryField("listFriendshipsByReceiverId"),
])
.selectionSet(["id",
"status",
"senderId",
"receiverId",
"receiver.*",
"sender.*",
"createdAt",
"updatedAt"
]),
```

### Describe alternatives you've considered

Right now you have to define selectionSet on every query you want deeper data in and I just want it everywhere.

### Additional context

_No response_

### Is this something that you'd be interested in working on?

- [ ] 👋 I may be able to implement this feature request
- [ ] ⚠️ This feature might incur a breaking change

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.