Skip to content

Commit 76e8306

Browse files
committed
docs: use interface for edge types with custom fields
1 parent 81510c7 commit 76e8306

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,25 @@ properties -- such as the date that the friend was added, or the type of relatio
247247
this is analogous to having a Many-to-Many relation where the intermediate join table contains additional data columns
248248
beyond just the keys of the two joined tables.)
249249

250-
In this case your edge type would look like the following example. Notice that we pass a `{ createdAt: Date }` type
251-
argument to `createEdgeType`; this specifies typings for the fields that are allowed to be passed to your edge class's
252-
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 })`.
253254

254255
```ts
255256
import { Field, GraphQLISODateTime, ObjectType } from '@nestjs/graphql';
256-
import { createEdgeType } from 'nestjs-graphql-connection';
257+
import { createEdgeType, EdgeInterface } from 'nestjs-graphql-connection';
257258
import { Person } from './entities';
258259

260+
export interface PersonFriendEdgeInterface extends EdgeInterface<Person> {
261+
createdAt: Date;
262+
}
263+
259264
@ObjectType()
260-
export class PersonFriendEdge extends createEdgeType<{ createdAt: Date }>(Person) {
265+
export class PersonFriendEdge
266+
extends createEdgeType<PersonFriendEdgeInterface>(Person)
267+
implements PersonFriendEdgeInterface
268+
{
261269
@Field(type => GraphQLISODateTime)
262270
public createdAt: Date;
263271
}

src/builder/ConnectionBuilder.spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
import Joi from 'joi';
22
import { Cursor } from '../cursor/Cursor';
33
import { validateParamsUsingSchema } from '../cursor/validateParamsUsingSchema';
4-
import { ConnectionArgs, createConnectionType, createEdgeType, PageInfo } from '../type';
4+
import {
5+
ConnectionArgs,
6+
ConnectionInterface,
7+
createConnectionType,
8+
createEdgeType,
9+
EdgeInterface,
10+
PageInfo,
11+
} from '../type';
512
import { ConnectionBuilder, EdgeInputWithCursor } from './ConnectionBuilder';
613

714
class TestNode {
815
id: string;
916
name: string;
1017
}
1118

12-
class TestEdge extends createEdgeType<{ customEdgeField?: number }>(TestNode) {
19+
interface TestEdgeInterface extends EdgeInterface<TestNode> {
20+
customEdgeField?: number;
21+
}
22+
23+
class TestEdge extends createEdgeType<TestEdgeInterface>(TestNode) implements TestEdgeInterface {
1324
public customEdgeField?: number;
1425
}
1526

16-
class TestConnection extends createConnectionType<{ customConnectionField?: number }>(TestEdge) {
27+
interface TestConnectionInterface extends ConnectionInterface<TestEdge> {
28+
customConnectionField?: number;
29+
}
30+
31+
class TestConnection
32+
extends createConnectionType<TestConnectionInterface>(TestEdge)
33+
implements TestConnectionInterface
34+
{
1735
public customConnectionField?: number;
1836
}
1937

0 commit comments

Comments
 (0)