Customizing the type of a Relay edge #497
-
Hi there 👋 I am having trouble with customizing a Relay connection, specifically the TypeScript type of an edge. The goal is to add a field To achieve that I created a Does anyone have some pointers on how to achieve the goal? import SchemaBuilder from "@pothos/core";
import RelayPlugin from "@pothos/plugin-relay";
export interface SchemaTypes {
DefaultFieldNullability: true;
Interfaces: {
HasAttachments: any;
};
Scalars: {
DateTime: { Input: Date; Output: Date };
};
}
export type SchemaTypesWithDefaults =
PothosSchemaTypes.ExtendDefaultTypes<SchemaTypes>;
export const builder = new SchemaBuilder<SchemaTypes>({
plugins: [RelayPlugin],
relayOptions: {},
defaultFieldNullability: true
});
class File {
constructor(public name: string) {}
}
class Attachment {
constructor(public file: File, public insertedAt: Date) {}
}
export const AttachmentConnectionType = builder.connectionObject(
{
type: File,
name: "AttachmentConnectionType",
nodeNullable: false,
edgesNullable: false,
},
{
name: "AttachmentConnectionEdge",
fields: (t) => ({
attachedAt: t.field({
type: "DateTime",
nullable: false,
resolve: (parent) => {
// TODO: Here I want to `parent` to be of type `{ edge: Attachment; node: File; cursor: string }`
return parent.edge.insertedAt;
},
}),
}),
}
);
export const HasAttachmentsType = builder.interfaceType("HasAttachments", {
fields: (t) => ({
attachments: t.field({
type: AttachmentConnectionType,
args: { ...t.arg.connectionArgs() },
// TODO: The return type of `toConnectionFromEdges` is causing type issues
resolve: async (_parent, _args) => {
// Simplified code
const result = [
new Attachment(new File("file"), new Date()),
new Attachment(new File("file"), new Date()),
];
return toConnectionFromEdges(result, { nodeKey: "file" });
},
}),
}),
});
// Simplified
function toConnectionFromEdges(
result: Attachment[],
{ nodeKey }: { nodeKey: keyof Attachment }
): { edge: Attachment; node: File; cursor: string }[] {
return result.map((attachment) => ({
edge: attachment,
node: attachment[nodeKey] as File,
cursor: "<generatedValue>",
}));
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Unfortunately I think this is something that doesn't work well with builder.connectionObject today. Connections were designed to infer the shape from the connection resolver (which you don't have when you define the connection object in isolation). There are 2 ways you can do this: You can define the connection inline with the connection field: https://stackblitz.com/edit/typescript-tifnwy?file=index.ts or you can pass in some generics when creating the connectionObject: https://stackblitz.com/edit/typescript-x6ph9p?file=index.ts I did need to change the return shape of toConnectionFromEdges slightly to match the expected shape of connections, but that should get you close to what you need |
Beta Was this translation helpful? Give feedback.
Unfortunately I think this is something that doesn't work well with builder.connectionObject today. Connections were designed to infer the shape from the connection resolver (which you don't have when you define the connection object in isolation).
There are 2 ways you can do this:
You can define the connection inline with the connection field: https://stackblitz.com/edit/typescript-tifnwy?file=index.ts
or you can pass in some generics when creating the connectionObject: https://stackblitz.com/edit/typescript-x6ph9p?file=index.ts
I did need to change the return shape of toConnectionFromEdges slightly to match the expected shape of connections, but that should get you close to what you need