Open HikaruTakakura opened 3 weeks ago
Hi @HikaruTakakura can you share your schema?
@chrisbonifacio
Thank you for your prompt reply. This is my schema.
type Cat
@model
@searchable
@auth(
rules: [
{ allow: public, operations: [read, create, update, delete] }
]
) {
id: ID!
name: String
about: String
deletedAt: AWSDateTime
updatedAt: AWSDateTime!
createdAt: AWSDateTime!
}
@HikaruTakakura hmm, I don't think that we support a total
field from openSearch out of the box. You may have to edit the VTL template for the resolver to set a track_total_hits
parameter to true
as mentioned in this feature request:
https://github.com/aws-amplify/amplify-category-api/issues/1079
@chrisbonifacio Thanks for the information. However, the issue I am raising is not the implementation of the total
field but its type.
Here is the query and its type generated by the schema shown above.
// queries.ts
export const searchCats = /* GraphQL */ `query SearchCats(
$filter: SearchableCatFilterInput
$sort: SearchableCatSortInput
$limit: Int
$nextToken: String
$from: Int
) {
searchCats(
filter: $filter
sort: $sort
limit: $limit
nextToken: $nextToken
from: $from
) {
items {
// ...
}
nextToken
total
__typename
}
}
` as GeneratedQuery<
APITypes.SearchCatsQueryVariables,
APITypes.SearchCatsQuery
>;
// API.ts
export type SearchCatsQuery = {
searchCats?: {
__typename: "SearchableCatConnection",
items: Array< {
__typename: "Cat",
// ...
} | null >,
nextToken?: string | null,
total?: number | null,
} | null,
};
It is clear that the response to the query should contain a total
field, and in fact it does return a value for that field, but it gives a type error.
As you can see in the screenshot below, it does not appear in the predictive text.
So now we use @ts-ignore
to suppress that error.
Hi @HikaruTakakura 👋 thanks for raising this issue!
I was able to get total
from the return type by using the SearchCatsQuery
type from API.ts
like this:
import { SearchCatsQuery } from "@/src/API";
import { searchCats } from "@/src/graphql/queries";
import { GraphQLQuery } from "aws-amplify/api";
// ...
const searchCatsQuery = async () => {
const result = await client.graphql<GraphQLQuery<SearchCatsQuery>>({
query: searchCats,
})
Hi @chrisbonifacio thank you very much.
In this case, however, the presence of the total
field should be inferred without type annotation. And that can be done by the modification of WithListsFixed
I first showed.
What are the problems with this modification approach?
@HikaruTakakura re-opened this issue and will discuss with the team!
In the meantime, please submit a Pull Request with the proposed fix for the team to review
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
GraphQL API
Amplify Version
v6
Amplify Categories
api
Backend
Amplify CLI
Environment information
Describe the bug
Property total does not exist on type 'PagedList<{ __typename: "Cat"; ...'.
The reason is that
WithListsFixed<T>
removes properties that exist inT
but are not included inPagedList
. https://github.com/aws-amplify/amplify-js/blob/ced891c2e4f6b0f1fdeaf44ab80cae9d585b6d15/packages/api-graphql/src/types/index.ts#L105In fact, the error disappeared after making the following changes
Expected behavior
The total property exists, so there should not be an error
Reproduction steps
searchCats
) using@searchable
.const response = await client.graphql({ query: searchCats })
response.data.searchCats
Code Snippet
No response
Log output
No response
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response