-
Hi! I'm trying to create the following function for creating a object type for generic input, but I cannot get it to work: interface PageInfo {
hasPreviousPage: boolean;
hasNextPage: boolean;
startCursor: string;
endCursor: string;
}
interface Edge<T> {
node: T;
cursor: string;
}
interface Connection<T> {
edges: Edge<T>[];
pageInfo: PageInfo;
}
const PageInfo = builder.objectRef<PageInfo>('PageInfo').implement({
fields: (t) => ({
hasPreviousPage: t.exposeBoolean('hasPreviousPage'),
hasNextPage: t.exposeBoolean('hasNextPage'),
startCursor: t.field({ type: 'Cuid', nullable: true, resolve: (i) => i.startCursor }),
endCursor: t.field({ type: 'Cuid', nullable: true, resolve: (i) => i.endCursor }),
}),
});
export const buildConnectionType = <T>(name: string, nodeType: ObjectRef<T>) => {
const Edge = builder.objectRef<Edge<T>>(`${name}Edge`).implement({
fields: (t) => ({
node: t.field({
resolve: (parent) => parent.node,
type: nodeType,
}),
cursor: t.field({
resolve: (parent) => parent.cursor,
type: 'Cuid',
}),
}),
});
const Connection = builder.objectRef<Connection<T>>(`${name}Connection`).implement({
fields: (t) => ({
edges: t.field({
resolve: (parent) => parent.edges,
type: [Edge],
}),
pageInfo: t.field({
resolve: (parent) => parent.pageInfo,
type: PageInfo,
}),
}),
});
return Connection;
};
const User = builder.objectType('User', {
fields: (t) => ({
username: t.field({
type: 'String',
resolve: (u) => u.username,
}),
password: t.field({
type: 'String',
resolve: (u) => u.password,
}),
}),
});
const UserConnection = buildConnectionType('User', User); but I'm getting the following typescript error for the parent.node resolver:
I Don't really understand what I'm doing wrong so I was hoping someone could help me. Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
Just to add: I just tested and it works without any issues if I just @ts-ignore the compiler error... |
Beta Was this translation helpful? Give feedback.
-
Curious if there is a reason you are not using the relay plugin to implement this pattern? |
Beta Was this translation helpful? Give feedback.
This is a side effect of some complicated types intended to make resolver errors more clear. It doesn't work well with genetics because the generic might be an array. It should be safe to ignore