Skip to content
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

Allow refering models from custom types in Gen 2 to allow for basic usable argument/return types in custom queries/mutations #2950

Open
2 tasks
thomasoehri opened this issue Oct 14, 2024 · 5 comments
Labels
feature-request New feature or request Gen 2

Comments

@thomasoehri
Copy link

thomasoehri commented Oct 14, 2024

Describe the feature you'd like to request

I'm integrating OpenSearch in my Gen 2 backend using custom queries where i need to return the items, nextToken as well as the total. The only way of doing this is using a custom type as the return type of the custom query:

// The model i'm working with and trying to integrate search with:
Feedback: a
            .model({
                title: a.string().required(),
                ...
            }),

// The custom type that defines the structure of the return type of the custom search query:
SearchableFeedbackConnection: a.customType({
    items: a.ref("Feedback").array().required(),
    nextToken: a.string(),
    total: a.integer(),
}),

searchFeedback: a
            .query()
            .arguments({
                ...
            })
            // Referencing the custom return type from above:
            .returns(
                a.ref("SearchableFeedbackConnection")
            )

This results in the following error:
Cannot use ".ref()" to refer a model from a "custom type". Field "items" of "SearchableFeedbackConnection" refers to model "Feedback"

The problem is that i NEED to return the nextToken alongside all Feedback items. So i can't just return .returns(a.ref("Feedback").array()), which wouldn't result in the error.

The problem isn't isolated to the opensearch example above, i have this issue using custom queries/mutations in general, for example in all my custom list queries where i also need to return the nextToken to be able to query for the next batch of items.

And the same goes for arguments: #2560

Describe the solution you'd like

I want to be able to refer to models from custom types so that custom queries work.

Describe alternatives you've considered

Creating another custom type that represents a Feedback instead of using the "Feedback" model obviously doesn't result in the above error but also doesn't have all the field-level authorization rules and relationships that allow for nested queries the model has:

// This does not work since i need all the field-level authorization rules and the ability to perform nested queries using the
// relationships on the "Feedback" model
FeedbackResponse: a.customType({
            ...
        }),

SearchableFeedbackConnection: a.customType({
    items: a.ref("FeedbackResponse").array().required(),
    nextToken: a.string(),
    total: a.integer(),
}),

Additional context

No response

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

  • 👋 I may be able to implement this feature request

Would this feature include a breaking change?

  • ⚠️ This feature might incur a breaking change
@thomasoehri thomasoehri changed the title Allow refering models from custom types in Gen 2 Allow refering models from custom types in Gen 2 to allow for basic usable argument/return types in custom queries/mutations Oct 14, 2024
@thomasoehri
Copy link
Author

This is a blocker for me since my custom list & search queries need to return my model's items but without being able to return a nextToken they're kind of useless atm.

@AnilMaktala
Copy link
Member

Hey @thomasoehri, Thanks for requesting this. We are marking this as a feature request for the team to evaluate further.

@1024cainiao
Copy link

Hi @AnilMaktala, I also encountered this problem. Looking forward to your solutions

@jmarshall9120
Copy link

@AnilMaktala - This is a big blocker for custom queries. There should be a type in the generated schema for the referenced model looks like this:

type ModelMyModelConnection @aws_cognito_user_pools @aws_iam {
  items: [Authorization]!
  nextToken: String
}

How do we get a hold of this object for reference on the TS side of things? Is it nested in the Schema type somewhere?

@jmarshall9120
Copy link

jmarshall9120 commented Nov 15, 2024

Work around I just implemented for anyone whose blocked by this. The trick is to duplicate the model, and make the dupe a customType. Its annoying but works.

// The model I'm working with and trying to integrate search with:
Feedback: a.model({
  title: a.string().required(),
  ...
}),

// Duplicate that allows referencing:
Feedback2: a.customType({
  title: a.string().required(),
  ...
}),

// The custom type that defines the structure of the return type of the custom search query:
SearchableFeedbackConnection: a.customType({
    items: a.ref("Feedback2").array(),
    nextToken: a.string(),
    scannedCount: a.integer(),
    startedAt: a.timestamp()
}),

searchFeedback: a.query()
    .arguments({
    ...
  })
    // Referencing the custom return type from above:
    .returns(a.ref("SearchableFeedbackConnection"))
    .handler(a.handler.custom({
        dataSource: a.ref('MasterTenantTable'),
        entry: './MyResolverId.js',
    })),

Appsync will often be none the wiser because the schemas match. However you still won't be able to get nextTokens for nested nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request Gen 2
Projects
None yet
Development

No branches or pull requests

4 participants