pothos icon indicating copy to clipboard operation
pothos copied to clipboard

type parameters should accept "readonly" arrays

Open hayes opened this issue 3 years ago • 7 comments

Some places where pothos accepts arrays in options (eg type for list types) don't work with readonly arrays. This can cause problems when using as const to create options compatible objects.

hayes avatar Sep 03 '22 19:09 hayes

This would technically be a breaking change because all plugins that extend various interfaces accepting type parameters would need to be updated.

hayes avatar Sep 10 '22 04:09 hayes

That means we can not use this pattern right?

type Payload = {
    readonly items: Item[]
}

PaginatedDeliveries.implement({
	description: "paginated deliveries",
	fields: (t) => ({
		items: t.expose("items", { type: [Item] }),
	}),
});

minhlucvan avatar Dec 21 '22 01:12 minhlucvan

no, this is would be unrelated.

As far as I can tell, that should work just fine

hayes avatar Dec 21 '22 01:12 hayes

The thing that this is referring to would be something like:

const itemsType = [Item] as const;

const PaginatedDeliveries = builder.objectRef<Payload>('PaginatedDeliveries');
PaginatedDeliveries.implement({
  description: 'paginated deliveries',

  fields: (t) => ({
    items: t.expose('items', { type: itemsType }),
  }),
});

This shouldn't be something most people run into, but it can come up when trying to build helpers that generate fields for you.

hayes avatar Dec 21 '22 01:12 hayes

thanks for the response, my case actually like this

export type PaginatedDeliveriesShape = {
	readonly items: readonly Delivery[];
};
export const PaginatedDeliveries = graphql.objectRef<PaginatedDeliveriesShape>("PaginatedDeliveries");

PaginatedDeliveries.implement({
	description: "paginated deliveries",
	fields: (t) => ({
		items: t.expose("items", { type: [delivery] }),
	}),
});

then the eslint error thrown

image

I think it's related

minhlucvan avatar Dec 21 '22 02:12 minhlucvan

Ah, I see, yeah. Similar issue, but not specifically what this issue was about. This should be easier to fix though

hayes avatar Dec 21 '22 02:12 hayes

@minhlucvan this should be fixed in the latest release

hayes avatar Dec 23 '22 23:12 hayes

This ended up being too big a breaking change to add into the 4.0 release, and hasn't really come up again, so closing this out for now

hayes avatar Jul 10 '24 18:07 hayes