@@ -80,7 +80,13 @@ Now define a `ConnectionBuilder` class for your `Connection` object. The builder
8080pagination arguments for the connection, and creating the cursors and ` Edge ` objects that make up the connection.
8181
8282``` ts
83- import { ConnectionBuilder , Cursor , PageInfo , validateParamsUsingSchema } from ' nestjs-graphql-connection' ;
83+ import {
84+ ConnectionBuilder ,
85+ Cursor ,
86+ EdgeInputWithCursor ,
87+ PageInfo ,
88+ validateParamsUsingSchema ,
89+ } from ' nestjs-graphql-connection' ;
8490
8591export type PersonCursorParams = { id: string };
8692export type PersonCursor = Cursor <PersonCursorParams >;
@@ -96,7 +102,7 @@ export class PersonConnectionBuilder extends ConnectionBuilder<
96102 return new PersonConnection (fields );
97103 }
98104
99- public createEdge(fields : { node : TestNode ; cursor : string } ): TestEdge {
105+ public createEdge(fields : EdgeInputWithCursor < PersonEdge > ): PersonEdge {
100106 return new PersonEdge (fields );
101107 }
102108
@@ -108,7 +114,7 @@ export class PersonConnectionBuilder extends ConnectionBuilder<
108114 // A cursor sent to or received from a client is represented as a base64-encoded, URL-style query string containing
109115 // one or more key/value pairs describing the referenced node's position in the result set (its ID, a date, etc.)
110116 // Validation is optional, but recommended to enforce that cursor values supplied by clients must be well-formed.
111- // See documentation for Joi at https://joi.dev/api/?v=17#object
117+ // This example uses Joi for validation, see documentation at https://joi.dev/api/?v=17#object
112118 // The following schema accepts only an object matching the type { id: string }:
113119 const schema: Joi .ObjectSchema <PersonCursorParams > = Joi .object ({
114120 id: Joi .string ().empty (' ' ).required (),
@@ -168,7 +174,12 @@ determining what the last result was on page 9.
168174To use offset cursors, extend your builder class from ` OffsetPaginatedConnectionBuilder ` instead of ` ConnectionBuilder ` :
169175
170176``` ts
171- import { OffsetPaginatedConnectionBuilder , PageInfo , validateParamsUsingSchema } from ' nestjs-graphql-connection' ;
177+ import {
178+ EdgeInputWithCursor ,
179+ OffsetPaginatedConnectionBuilder ,
180+ PageInfo ,
181+ validateParamsUsingSchema ,
182+ } from ' nestjs-graphql-connection' ;
172183
173184export class PersonConnectionBuilder extends OffsetPaginatedConnectionBuilder <
174185 PersonConnection ,
@@ -180,7 +191,7 @@ export class PersonConnectionBuilder extends OffsetPaginatedConnectionBuilder<
180191 return new PersonConnection (fields );
181192 }
182193
183- public createEdge(fields : { node : TestNode ; cursor : string } ): TestEdge {
194+ public createEdge(fields : EdgeInputWithCursor < PersonEdge > ): PersonEdge {
184195 return new PersonEdge (fields );
185196 }
186197
@@ -236,24 +247,32 @@ properties -- such as the date that the friend was added, or the type of relatio
236247this is analogous to having a Many-to-Many relation where the intermediate join table contains additional data columns
237248beyond just the keys of the two joined tables.)
238249
239- In this case your edge type would look like the following example. Notice that we pass a ` { createdAt: Date } ` type
240- argument to ` createEdgeType ` ; this specifies typings for the fields that are allowed to be passed to your edge class's
241- constructor for initialization when doing ` new PersonFriendEdge({ ...fields }) ` .
250+ In this case your edge type would look like the following example. Notice that we also now define a
251+ ` PersonFriendEdgeInterface ` type which we pass as a generic argument to ` createEdgeType ` ; this ensures correct typings
252+ for the fields that are allowed to be passed to your edge class's constructor for initialization when doing
253+ ` new PersonFriendEdge({ ...fields }) ` .
242254
243255``` ts
244256import { Field , GraphQLISODateTime , ObjectType } from ' @nestjs/graphql' ;
245- import { createEdgeType } from ' nestjs-graphql-connection' ;
257+ import { createEdgeType , EdgeInterface } from ' nestjs-graphql-connection' ;
246258import { Person } from ' ./entities' ;
247259
260+ export interface PersonFriendEdgeInterface extends EdgeInterface <Person > {
261+ createdAt: Date ;
262+ }
263+
248264@ObjectType ()
249- export class PersonFriendEdge extends createEdgeType <{ createdAt: Date }>(Person ) {
265+ export class PersonFriendEdge
266+ extends createEdgeType <PersonFriendEdgeInterface >(Person )
267+ implements PersonFriendEdgeInterface
268+ {
250269 @Field (type => GraphQLISODateTime )
251270 public createdAt: Date ;
252271}
253272```
254273
255- ` ConnectionBuilder ` supports overriding the ` createConnection() ` and ` createEdge() ` methods when calling ` build() ` . This
256- enables you to enrich the connection and edges with additional metadata at resolve time .
274+ To achieve this, you can pass an array of partial ` edges ` (instead of ` nodes ` ) to ` build() ` . This enables you to
275+ provide values for any additional fields present on the edges .
257276
258277The following example assumes you have a GraphQL schema that defines a ` friends ` field on your ` Person ` object, which
259278resolves to a ` PersonFriendConnection ` containing the person's friends. In your database you would have a ` friend ` table
@@ -284,19 +303,34 @@ export class PersonResolver {
284303 // Return resolved PersonFriendConnection with edges and pageInfo
285304 return connectionBuilder .build ({
286305 totalEdges ,
287- nodes: friends .map (friend => friend .otherPerson ),
288- createEdge : ({ node , cursor }) => {
289- const friend = friends .find (friend => friend .otherPerson .id === node .id );
290-
291- return new PersonFriendEdge ({ node , cursor , createdAt: friend .createdAt });
292- },
306+ edges: friends .map (friend => ({
307+ node: friend .otherPerson ,
308+ createdAt: friend .createdAt ,
309+ })),
293310 });
294311 }
295312}
296313```
297314
298- Alternatively, you could build the connection result yourself by replacing the ` connectionBuilder.build(...) ` statement
299- with something like the following:
315+ Alternatively, you can override the ` createEdge() ` or ` createConnection() ` methods when calling ` build() ` .
316+
317+ ``` ts
318+ return connectionBuilder .build ({
319+ totalEdges ,
320+ nodes: friends .map (friend => friend .otherPerson ),
321+ createConnection({ edges , pageInfo }) {
322+ return new PersonFriendConnection ({ edges , pageInfo , customField: ' hello-world' });
323+ },
324+ createEdge : ({ node , cursor }) => {
325+ const friend = friends .find (friend => friend .otherPerson .id === node .id );
326+
327+ return new PersonFriendEdge ({ node , cursor , createdAt: friend .createdAt });
328+ },
329+ });
330+ ```
331+
332+ Finally, if the above methods don't meet your needs you can always build the connection result yourself by replacing
333+ ` connectionBuilder.build(...) ` with something like the following:
300334
301335``` ts
302336// Resolve edges with cursor, node, and additional metadata
0 commit comments