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

Update generated Typescript schema for Directus SDK v11 #31

Closed
stuikomma opened this issue Jul 26, 2023 · 14 comments
Closed

Update generated Typescript schema for Directus SDK v11 #31

stuikomma opened this issue Jul 26, 2023 · 14 comments

Comments

@stuikomma
Copy link

The latest release of the directus SDK version 11 requires the collections to be of array types. See the example code taken from https://docs.directus.io/guides/sdk/getting-started.html#creating-a-composable-client

import { createDirectus } from '@directus/sdk';
import { rest } from '@directus/sdk/rest';
import { graphql } from '@directus/sdk/graphql';

interface Article {
	id: number;
	title: string;
	content: string;
}

interface Schema {
	articles: Article[]; // <-- Note the array
}

// The extension currently generates types like this
// interface Schema {
//	articles: Article; // <-- Not an array
//}

// Client with REST support
const client = createDirectus<Schema>('http://directus.example.com').with(rest());

// Client with GraphQL support
const client = createDirectus<Schema>('http://directus.example.com').with(graphql());

If the array is missing, you will get type errors when making a call like this:

client.request(readItems('articles'));

I propose adding a checkbox [ ] Generate types for SDK v11.

Thanks for making this extension!

@stuikomma stuikomma changed the title Update generated schema for Directus SDK v11 Update generated Typescript schema for Directus SDK v11 Jul 26, 2023
@stuikomma
Copy link
Author

I found another issue: Optional fields should be be typed optionalField: ValueType | null instead of optionalField?: ValueType.

When using the current version, optional fields become required and vice-versa.

@maltejur
Copy link
Owner

maltejur commented Aug 3, 2023

Are you talking about the export type CustomDirectusTypes being generated? If that is required, adding a checkbox (probably enabled by default) sounds like a sensible change.

I found another issue: Optional fields should be be typed optionalField: ValueType | null instead of optionalField?: ValueType.

I would argue that is pretty much up to personal preference. Or are there any objective reasons it should be done this way and not with the ?? Of couse we can also add a checkbox to configure this behavior.

@stuikomma
Copy link
Author

Are you talking about the export type CustomDirectusTypes being generated? If that is required, adding a checkbox (probably enabled by default) sounds like a sensible change.

Yes, that's what I meant.

I would argue that is pretty much up to personal preference. Or are there any objective reasons it should be done this way and not with the ?? Of couse we can also add a checkbox to configure this behavior.

The types in the new directus SDK do not resolve properly with ?. They can be resolved by using | null instead of ?. Here is an example use case I have in my projects:

import { createDirectus, readItems, rest } from '@directus/sdk';

interface Article {
    id: number;
    title: string;
    content: string;
    author: Author | null; // <-- this works
    // author?: Author; // <-- this doesn't work
}

interface Author {
    first_name: string;
}

interface Schema {
    articles: Article[]; // <-- Note the array
    authors: Author[];
}

// Client with REST support
const client = createDirectus<Schema>('http://directus.example.com').with(
    rest(),
);

function fetchArticles() {
    return client.request(
        readItems('articles', {
            fields: ['id', 'title', { author: ['first_name'] }],
        }),
    );
}

const mockArticles = [
    { id: 1, title: 'Mock Title', author: null },
    //                              ^ errors if you choose `?`
    // { id: 2, title: 'Some Title', author: undefined },
    //                                ^ this errors in both cases, so this is not a workaround
    {
        id: 3,
        title: 'Another Title',
        author: {
            first_name: 'Edgar',
        },
    },
] satisfies Awaited<ReturnType<typeof fetchArticles>>;

@maxbec
Copy link

maxbec commented Aug 9, 2023

Is there any progress regarding the array types implementation?

@maltejur
Copy link
Owner

maltejur commented Aug 9, 2023

Implemented in 84afcd2

Can anybody try it out and check if I changed the right thing? I currently don't actively use Directus anymore, so it isn't that easy for me.

index.js

@JeremieLeblanc
Copy link

Implemented in 84afcd2

Can anybody try it out and check if I changed the right thing? I currently don't actively use Directus anymore, so it isn't that easy for me.

index.js

I have tested it quickly and it does seem to work except for the fields that add "any[]"

An example of this is translations fields like the following:

export type Articles = {
  date_created?: string | null;
  date_updated?: string | null;
  id: string;
  status: string;
  translations: any[] | ArticlesTranslations[];
  user_created?: string | DirectusUsers | null;
  user_updated?: string | DirectusUsers | null;
};

This seems to break the deep filtering.

As soon as I removed all of the "any" in the types, the errors went away.

@JeremieLeblanc
Copy link

One more thing.

Singletons should not me marked as arrays in the CustomDirectusTypes.

@maltejur
Copy link
Owner

Singletons should not me marked as arrays in the CustomDirectusTypes.

Ah yes, fixed.

I have tested it quickly and it does seem to work except for the fields that add "any[]"

Is that related to the original issue or something else? I originally wrote it like that because, depending on the query, the API either returned a list of primary keys (of which we do not know the type), or complete objects of the type we know.

@marquetd
Copy link

marquetd commented Sep 5, 2023

Sorry another thing, don't forget to update the example when " Generate Types for Directus SDK >= v11" is checked :

Capture d’écran du 2023-09-05 15-18-33

@maltejur
Copy link
Owner

maltejur commented Sep 6, 2023

@marquetd what exactly needs to change there?

@marquetd
Copy link

marquetd commented Sep 6, 2023

When the checkbox is checked :

image

unchecked :

image

@maltejur
Copy link
Owner

maltejur commented Sep 7, 2023

Thanks, I've updated the example code in 95c53f6

@maltejur
Copy link
Owner

maltejur commented Sep 7, 2023

https://github.com/maltejur/directus-extension-generate-types/releases/tag/0.5.0

@maltejur maltejur closed this as completed Sep 7, 2023
@D3VL-Jack
Copy link
Contributor

@maltejur

Ah yes, fixed.

In 0.5.0, Singletons are still returning as arrays.

export type CustomDirectusTypes = {
    // ...
    global_variables: GlobalVariables[];
    // ...
};

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants