Skip to content

Commit 81510c7

Browse files
committed
docs: show how to pass edges to build()
1 parent 7d9a681 commit 81510c7

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ export class PersonFriendEdge extends createEdgeType<{ createdAt: Date }>(Person
263263
}
264264
```
265265

266-
`ConnectionBuilder` supports overriding the `createConnection()` and `createEdge()` methods when calling `build()`. This
267-
enables you to enrich the connection and edges with additional metadata at resolve time.
266+
To achieve this, you can pass an array of partial `edges` (instead of `nodes`) to `build()`. This enables you to
267+
provide values for any additional fields present on the edges.
268268

269269
The following example assumes you have a GraphQL schema that defines a `friends` field on your `Person` object, which
270270
resolves to a `PersonFriendConnection` containing the person's friends. In your database you would have a `friend` table
@@ -295,19 +295,34 @@ export class PersonResolver {
295295
// Return resolved PersonFriendConnection with edges and pageInfo
296296
return connectionBuilder.build({
297297
totalEdges,
298-
nodes: friends.map(friend => friend.otherPerson),
299-
createEdge: ({ node, cursor }) => {
300-
const friend = friends.find(friend => friend.otherPerson.id === node.id);
301-
302-
return new PersonFriendEdge({ node, cursor, createdAt: friend.createdAt });
303-
},
298+
edges: friends.map(friend => ({
299+
node: friend.otherPerson,
300+
createdAt: friend.createdAt,
301+
})),
304302
});
305303
}
306304
}
307305
```
308306

309-
Alternatively, you could build the connection result yourself by replacing the `connectionBuilder.build(...)` statement
310-
with something like the following:
307+
Alternatively, you can override the `createEdge()` or `createConnection()` methods when calling `build()`.
308+
309+
```ts
310+
return connectionBuilder.build({
311+
totalEdges,
312+
nodes: friends.map(friend => friend.otherPerson),
313+
createConnection({ edges, pageInfo }) {
314+
return new PersonFriendConnection({ edges, pageInfo, customField: 'hello-world' });
315+
},
316+
createEdge: ({ node, cursor }) => {
317+
const friend = friends.find(friend => friend.otherPerson.id === node.id);
318+
319+
return new PersonFriendEdge({ node, cursor, createdAt: friend.createdAt });
320+
},
321+
});
322+
```
323+
324+
Finally, if the above methods don't meet your needs you can always build the connection result yourself by replacing
325+
`connectionBuilder.build(...)` with something like the following:
311326

312327
```ts
313328
// Resolve edges with cursor, node, and additional metadata

0 commit comments

Comments
 (0)