forked from Quramy/graphql-decorator
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from indigotech/feature/union
Feature - Union Type
- Loading branch information
Showing
16 changed files
with
153 additions
and
7 deletions.
There are no files selected for viewing
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
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 @@ | ||
export * from './union-type.decorator'; |
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,17 @@ | ||
import { MetadataStorage } from '../metadata-storage'; | ||
import { UnionOption } from '../metadata'; | ||
|
||
/** | ||
* Union Type. ref: http://graphql.org/learn/schema/#union-types | ||
* @param option Options for a Union Type | ||
*/ | ||
export function UnionType<T>(option: UnionOption<T>) { | ||
return function (target: any) { | ||
MetadataStorage.addUnionMetadata({ | ||
name: target.name, | ||
types: option.types, | ||
resolver: option.resolver, | ||
description: option.description, | ||
}); | ||
} as Function; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './metadata-storage'; |
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,18 @@ | ||
import { UnionTypeMetadata } from '../metadata/types/union.metadata'; | ||
|
||
const unionMetadata: UnionTypeMetadata[] = []; | ||
|
||
const MetadataStorage = { | ||
|
||
addUnionMetadata: function(metadata: UnionTypeMetadata) { | ||
unionMetadata.push(metadata); | ||
}, | ||
getUnionMetadata: function(): UnionTypeMetadata[] { | ||
return unionMetadata; | ||
}, | ||
containsUnionMetadata: function(name: string) { | ||
return unionMetadata.some(metadata => metadata.name === name); | ||
}, | ||
}; | ||
|
||
export { MetadataStorage }; |
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,2 @@ | ||
export * from './options'; | ||
export * from './types'; |
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 @@ | ||
export * from './union.option'; |
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,19 @@ | ||
/** | ||
* Arguments for a Union type on graphql schema | ||
*/ | ||
export interface UnionOption<T> { | ||
/** | ||
* (Optional) Description | ||
*/ | ||
description?: string; | ||
|
||
/** | ||
* Concrete object types | ||
*/ | ||
types: any[]; | ||
|
||
/** | ||
* Resolver function to inform schema what type should be returned based on the value provided | ||
*/ | ||
resolver: (obj: T, context: any, info: any) => Promise<string> | string | null; | ||
} |
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 @@ | ||
export * from './union.metadata'; |
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,6 @@ | ||
export interface UnionTypeMetadata { | ||
name: string; | ||
description?: string; | ||
types: any[]; | ||
resolver: (obj: any, context: any, info: any) => Promise<string> | string | null; | ||
} |
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
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 @@ | ||
export * from './union.type-factory'; |
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,20 @@ | ||
import * as graphql from 'graphql'; | ||
import { MetadataStorage } from '../metadata-storage'; | ||
import { UnionTypeMetadata } from '../metadata'; | ||
import { objectTypeFactory } from '../object_type_factory'; | ||
|
||
export function unionTypeFactory(name: string, isInput: boolean): graphql.GraphQLUnionType | undefined { | ||
return MetadataStorage.getUnionMetadata() | ||
.filter(union => union.name === name) | ||
.map(union => { | ||
return new graphql.GraphQLUnionType({ | ||
description: union.description, | ||
name: union.name, | ||
resolveType: union.resolver, | ||
types: union.types | ||
.map(type => objectTypeFactory(type, isInput)) | ||
.filter(_ => _), //filter null values | ||
}); | ||
}) | ||
.find((_, index) => index === 0); | ||
} |