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

[SDK] Unable to resolve inferred partial field types, when using _and/_or filters in queries #20633

Closed
sensedrive opened this issue Dec 4, 2023 · 5 comments · Fixed by #22288
Assignees

Comments

@sensedrive
Copy link
Contributor

Describe the Bug

I am using the SDK (13.0.2) with Types.

With the SDK it is possible to retrieve the types of the partial object, when explitely queriing single fields like in the example below.

const response = await this.directus.request(
      readItems('products', {
        fields: [
          'id',
          'title'
        ],
      }),
    );

I get

grafik

const response: Merge<MapFlatFields<object & Product, "id" | "title", MappedFunctionFields<Collections, object & Product>>, {}>[]

To Reproduce

But when I additionally filter with _and/_or I get the type for the whole object back.

grafik

const response: Merge<MapFlatFields<object & Product, keyof Product | FunctionFields<Collections, Product>, MappedFunctionFields<Collections, object & Product>>, {}>[]

Maybe this ticket is connected with the following issue? #19285

Thank you in advance

Directus Version

v10.7.2

Hosting Strategy

Self-Hosted (Docker Image)

@br41nslug
Copy link
Member

What is the actual issue you're seeing? I dont directly see anything wrong with that generated type but is always hard to tell without the appropriate context.

@sensedrive
Copy link
Contributor Author

sensedrive commented Dec 4, 2023

What is the actual issue you're seeing? I dont directly see anything wrong with that generated type but is always hard to tell without the appropriate context.

The TypeScript issue I'm facing involves incorrect type resolution for queries. Ideally, only the queried fields should be resolved, but instead, it resolves the entire object type from the collection, when filtering additionally with _and/_or filters.

Edit: The first example is showing a correct example. The second the kinda buggy one.

@br41nslug
Copy link
Member

Mind sharing the important parts of the schema for reproduction too? the root and product types should do.

Maybe this ticket is connected with the following issue? #19285

If not related to the issue it will be related to the PR as thats where these conditionals were implemented

@sensedrive
Copy link
Contributor Author

I uploaded a test repo here https://github.com/sensedrive/directus-types-test

Filtering with or without _and makes a significant difference in the response types.

You should see an ts errror in line 16. Or I am doing something wrong ...

@triplecasquette
Copy link

triplecasquette commented Dec 18, 2023

I face a similar problem.
I'm using nextjs. I see using chatGPT the problem could be in ts config : "moduleResolution": "bundler",
But this is the next config and don't want to change it.

Actually what i run into is when i want to filter a readItems the filter couldn't be partial.
I mean i just want to filter by Slug like this:

export const findServiceBySlug = async (slug: string) => {
  const service = await directus.request(
    readItems("service", {
      fields: ["*", { fimg: ["*"] }, { block_image: ["*"] }],
      filter: { slug: { _eq: slug } },
      limit: 1,
    })
  );

And ts throw this error underlining the world filter:

Type '{ slug: { _eq: string; }; }' doesn't have properties of type 'QueryFilter<ISchema, IService>': id, name, subtitle, price and 8 more.ts(2740)
(property) Query<ISchema, IService>.filter?: QueryFilter<ISchema, IService> | undefined

@sensedrive The only solution I find for now is using //ts-ignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants