-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): Add relation/connection decorators
* Allow specifying relations and connections on DTOs
- Loading branch information
1 parent
cd09abe
commit a75cf96
Showing
7 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/query-graphql/__tests__/decorators/relation.decorator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ObjectType } from '@nestjs/graphql'; | ||
import { Relation, Connection } from '../../src'; | ||
import { getMetadataStorage } from '../../src/metadata'; | ||
|
||
@ObjectType() | ||
class TestRelation {} | ||
|
||
describe('@Relation', () => { | ||
it('should add the relation metadata to the metadata storage', () => { | ||
const relationFn = () => TestRelation; | ||
const relationOpts = { disableRead: true }; | ||
@ObjectType() | ||
@Relation('test', relationFn, relationOpts) | ||
class TestDTO {} | ||
|
||
const relations = getMetadataStorage().getRelations(TestDTO); | ||
expect(relations).toHaveLength(1); | ||
const relation = relations![0]; | ||
expect(relation.name).toBe('test'); | ||
expect(relation.relationTypeFunc).toBe(relationFn); | ||
expect(relation.isConnection).toBe(false); | ||
expect(relation.relationOpts).toBe(relationOpts); | ||
}); | ||
}); | ||
|
||
describe('@Connection', () => { | ||
it('should add the relation metadata to the metadata storage', () => { | ||
const relationFn = () => TestRelation; | ||
const relationOpts = { disableRead: true }; | ||
@ObjectType() | ||
@Connection('test', relationFn, relationOpts) | ||
class TestDTO {} | ||
|
||
const relations = getMetadataStorage().getRelations(TestDTO); | ||
expect(relations).toHaveLength(1); | ||
const relation = relations![0]; | ||
expect(relation.name).toBe('test'); | ||
expect(relation.relationTypeFunc).toBe(relationFn); | ||
expect(relation.isConnection).toBe(true); | ||
expect(relation.relationOpts).toBe(relationOpts); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
packages/query-graphql/__tests__/resolvers/relations/relations.resolver.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ObjectType } from '@nestjs/graphql'; | ||
import { Connection, Relation } from '../../../src/decorators'; | ||
import { FilterableField } from '../../../src/decorators/filterable-field.decorator'; | ||
import * as readRelations from '../../../src/resolvers/relations/read-relations.resolver'; | ||
import * as updateRelations from '../../../src/resolvers/relations/update-relations.resolver'; | ||
import * as removeRelations from '../../../src/resolvers/relations/remove-relations.resolver'; | ||
import { Relatable } from '../../../src'; | ||
import { BaseServiceResolver } from '../../../src/resolvers/resolver.interface'; | ||
|
||
describe('Relatable', () => { | ||
const readMixinSpy = jest.spyOn(readRelations, 'ReadRelationsMixin'); | ||
const updateMixinSpy = jest.spyOn(updateRelations, 'UpdateRelationsMixin'); | ||
const removeMixinSpy = jest.spyOn(removeRelations, 'RemoveRelationsMixin'); | ||
|
||
@ObjectType() | ||
class TestRelation { | ||
@FilterableField() | ||
id!: number; | ||
} | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
it('should call the mixins with the relations derived from decorators', () => { | ||
@ObjectType() | ||
@Relation('testRelation', () => TestRelation) | ||
@Connection('testConnection', () => TestRelation) | ||
class Test {} | ||
|
||
Relatable(Test, {}, {})(BaseServiceResolver); | ||
|
||
const relations = { | ||
one: { testRelation: { DTO: TestRelation } }, | ||
many: { testConnection: { DTO: TestRelation } }, | ||
}; | ||
expect(readMixinSpy).toBeCalledWith(Test, relations); | ||
expect(updateMixinSpy).toBeCalledWith(Test, relations); | ||
expect(removeMixinSpy).toBeCalledWith(Test, relations); | ||
}); | ||
|
||
it('should call the mixins with the relations that are passed in', () => { | ||
@ObjectType() | ||
class Test {} | ||
|
||
Relatable( | ||
Test, | ||
{ | ||
one: { testRelation: { DTO: TestRelation } }, | ||
many: { testConnection: { DTO: TestRelation } }, | ||
}, | ||
{}, | ||
)(BaseServiceResolver); | ||
|
||
const relations = { | ||
one: { testRelation: { DTO: TestRelation } }, | ||
many: { testConnection: { DTO: TestRelation } }, | ||
}; | ||
expect(readMixinSpy).toBeCalledWith(Test, relations); | ||
expect(updateMixinSpy).toBeCalledWith(Test, relations); | ||
expect(removeMixinSpy).toBeCalledWith(Test, relations); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { FilterableField } from './filterable-field.decorator'; | ||
export { ResolverMethodOpts } from './resolver-method.decorator'; | ||
export { Connection, Relation, RelationDecoratorOpts, RelationTypeFunc } from './relation.decorator'; | ||
export * from './resolver-mutation.decorator'; | ||
export * from './resolver-query.decorator'; | ||
export * from './resolver-field.decorator'; |
38 changes: 38 additions & 0 deletions
38
packages/query-graphql/src/decorators/relation.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Class } from '@nestjs-query/core'; | ||
import { getMetadataStorage } from '../metadata'; | ||
import { ResolverRelation } from '../resolvers/relations/relations.interface'; | ||
|
||
export type RelationDecoratorOpts<Relation> = Omit<ResolverRelation<Relation>, 'DTO'>; | ||
export type RelationTypeFunc<Relation> = () => Class<Relation>; | ||
|
||
export function Relation<DTO, Relation>( | ||
name: string, | ||
relationTypeFunction: RelationTypeFunc<Relation>, | ||
options?: RelationDecoratorOpts<Relation>, | ||
) { | ||
return <Cls extends Class<DTO>>(DTOClass: Cls): Cls | void => { | ||
getMetadataStorage().addRelation(DTOClass, name, { | ||
name, | ||
isConnection: false, | ||
relationOpts: options, | ||
relationTypeFunc: relationTypeFunction, | ||
}); | ||
return DTOClass; | ||
}; | ||
} | ||
|
||
export function Connection<DTO, Relation>( | ||
name: string, | ||
relationTypeFunction: RelationTypeFunc<Relation>, | ||
options?: RelationDecoratorOpts<Relation>, | ||
) { | ||
return <Cls extends Class<DTO>>(DTOClass: Cls): Cls | void => { | ||
getMetadataStorage().addRelation(DTOClass, name, { | ||
name, | ||
isConnection: true, | ||
relationOpts: options, | ||
relationTypeFunc: relationTypeFunction, | ||
}); | ||
return DTOClass; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
packages/query-graphql/src/resolvers/relations/relations.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,42 @@ | ||
import { Class } from '@nestjs-query/core'; | ||
import { getMetadataStorage } from '../../metadata'; | ||
import { ServiceResolver } from '../resolver.interface'; | ||
import { ReadRelationsMixin } from './read-relations.resolver'; | ||
import { ReferencesRelationMixin } from './references-relation.resolver'; | ||
import { ReferencesOpts, RelationsOpts } from './relations.interface'; | ||
import { RemoveRelationsMixin } from './remove-relations.resolver'; | ||
import { UpdateRelationsMixin } from './update-relations.resolver'; | ||
|
||
const getRelationsFromMetadata = <DTO>(DTOClass: Class<DTO>): RelationsOpts => { | ||
const relations: RelationsOpts = {}; | ||
const metaRelations = getMetadataStorage().getRelations(DTOClass) ?? []; | ||
metaRelations.forEach((r) => { | ||
const opts = { ...r.relationOpts, DTO: r.relationTypeFunc() }; | ||
if (r.isConnection) { | ||
relations.many = { ...relations.many, [r.name]: opts }; | ||
} else { | ||
relations.one = { ...relations.one, [r.name]: opts }; | ||
} | ||
}); | ||
return relations; | ||
}; | ||
|
||
export const Relatable = <DTO>(DTOClass: Class<DTO>, relations: RelationsOpts, referencesOpts: ReferencesOpts<DTO>) => < | ||
B extends Class<ServiceResolver<DTO>> | ||
>( | ||
Base: B, | ||
): B => { | ||
const metaRelations = getRelationsFromMetadata(DTOClass); | ||
const oneRelations = { ...relations.one, ...(metaRelations.one ?? {}) }; | ||
const manyRelations = { ...relations.many, ...(metaRelations.many ?? {}) }; | ||
const mergedRelations = { one: oneRelations, many: manyRelations }; | ||
return ReferencesRelationMixin( | ||
DTOClass, | ||
referencesOpts, | ||
)( | ||
ReadRelationsMixin( | ||
DTOClass, | ||
relations, | ||
)(UpdateRelationsMixin(DTOClass, relations)(RemoveRelationsMixin(DTOClass, relations)(Base))), | ||
mergedRelations, | ||
)(UpdateRelationsMixin(DTOClass, mergedRelations)(RemoveRelationsMixin(DTOClass, mergedRelations)(Base))), | ||
); | ||
}; |