question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List input to @connection fields parameter

See original GitHub issue

Which Category is your question related to? API, GraphQL, schema defnitions, documentation

Amplify CLI Version 4.24.0

What AWS Services are you utilizing? API

Provide additional details e.g. code snippets Hi guys, I have a type of relation in my data that I have been trying to solve for quite a while now. I have gone over the documentation but could find any information on how to achieve the desired result.

Lets say that I have two @models, one for Characters and one for Items. Characters can own items, which means there is a one way connection. If we are restricted to just a single item per character, I could define the schema like below:

type Character @model
{
    id: ID!
    itemId: String
    item: Item @connection(fields: ["itemId"])
}
type Item @model
{
    id: ID!
}

In this, multiple characters could own the same item, but there wouldn’t have to be any duplicate entries in the item database (because the characters point to the same object).

But now to my question, how do I achieve this kind of relation when a character can own many items? My hope was that I could do something like this:

type Character @model
{
    id: ID!
    itemIds: [String]
    items: [Item] @connection(fields: ["itemIds"])
}
type Item @model
{
    id: ID!
}

and that the resolver would loop over itemIds in order to fetch all of the items specified. However, as you can probably guess it does not work in this way and does not accept list inputs. So what is the recommended approach for these kinds of relations?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
RossWilliamscommented, Jul 16, 2020

There are two approaches.

  1. Create a many-to-many mapping. This means having a CharacterItems model that holds connections to both the Character and Items model. This has a latency impact, as there is another round trip to the database.
  2. Create a custom resolver for Character.items.req.vtl instead of the auto-generated one created when you use the @connection directive. In your custom resolver, use a BatchGet dynamodb operation to get all items in your items list. There is an AppSync tutorial on using BatchGet / TransactGet in templates. Using this approach, you are limited to 100 items, so choose it if you have a bounded list of items < 100.
0reactions
github-actions[bot]commented, May 26, 2021

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Input macro field with list of fields as parameter
I've a mapping in IICS - Data Integration. Within the mapping I have an expression `expValidateFields`. This expression checks all columns of an...
Read more >
UI parameter for "List" field - what's the correct input type?
For reference fields, I'd select "Choice list" as UI parameter input type. I can't seem to find the correct "Input type" on the...
Read more >
REST API Best practice: How to accept list of parameter ...
We are launching a new REST API and I wanted some community input on best practices around how we should have input parameters...
Read more >
Use parameters in queries, forms, and reports
This article explains how to use forms in Access to enhance your use of parameters in queries, forms, and reports.
Read more >
Use Field List from Input Feature Class as Parameter in ...
I've manage to do that through this workaround (see images below):. Note: You can access the Get Field Value Tool through Insert >...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found