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 #46 from indigotech/feature/interface-decorator
Feature - @InterfaceType decorator
- Loading branch information
Showing
22 changed files
with
264 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Concats two arrays | ||
* @param itemsA first array argument | ||
* @param itemsB second array argument | ||
*/ | ||
export const concat = (itemsA: any[], itemsB: any[]) => itemsA.concat(itemsB); | ||
|
||
/** | ||
* Executes a flatMap modifier function to each elements of the array | ||
* @param λ the flatMap function | ||
* @param collection the array to apply the flatMap funtion to | ||
*/ | ||
export const flatMap = (λ: (item: any) => any, collection: any[]) => collection.map(λ).reduce(concat, []); | ||
|
||
/** | ||
* Flattens an array with nested arrays | ||
* @param collection the array argument | ||
*/ | ||
export const flatten = (collection: any[]) => flatMap(items => { | ||
if (items.constructor === Array) { | ||
return flatMap(item => item, items); | ||
} else { | ||
return items; | ||
} | ||
}, collection); |
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,19 @@ | ||
import { getMetadataArgsStorage } from '../metadata-builder'; | ||
import { InterfaceOption } from '../index'; | ||
|
||
/** | ||
* Interface Type. | ||
* See [GraphQL Documentation - Interfaces]{@link http://graphql.org/learn/schema/#interfaces} | ||
* | ||
* @param option Options for an Interface definition | ||
*/ | ||
export function InterfaceType<T>(option: InterfaceOption<T>) { | ||
return function (target: any) { | ||
getMetadataArgsStorage().interfaces.push({ | ||
target: target, | ||
name: target.name, | ||
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
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,5 @@ | ||
import { Argument } from './argument'; | ||
|
||
export interface InterfaceTypeArg extends Argument { | ||
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,11 @@ | ||
import { Option } from './option'; | ||
|
||
/** | ||
* Arguments for an {@link InterfaceType} on graphql schema | ||
*/ | ||
export interface InterfaceOption<T> extends Option { | ||
/** | ||
* 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
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,5 @@ | ||
import { Metadata } from './metadata'; | ||
|
||
export interface InterfaceTypeMetadata extends Metadata { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { Metadata } from './metadata'; | ||
import { InterfaceTypeMetadata } from './interface.metadata'; | ||
|
||
export interface ObjectTypeMetadata extends Metadata { | ||
isInput: boolean; | ||
interfaces: InterfaceTypeMetadata[]; | ||
} |
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,32 @@ | ||
import * as graphql from 'graphql'; | ||
|
||
import { getMetadataBuilder } from '../metadata-builder'; | ||
import { objectTypeFactory } from './object.type-factory'; | ||
import { fieldTypeFactory } from './field.type-factory'; | ||
|
||
let interfaceTypeCache: { [key: string]: any } = {}; | ||
|
||
export function interfaceTypeFactory(target: any): graphql.GraphQLInterfaceType | undefined { | ||
return getMetadataBuilder().buildInterfaceTypeMetadata(target) | ||
.map(metadata => { | ||
if (!interfaceTypeCache[metadata.name]) { | ||
interfaceTypeCache[metadata.name] = new graphql.GraphQLInterfaceType({ | ||
description: metadata.description, | ||
name: metadata.name, | ||
resolveType: metadata.resolver, | ||
fields: getMetadataBuilder() | ||
.buildFieldMetadata(target.prototype) | ||
.map(field => ({ | ||
metadata: field, | ||
type: fieldTypeFactory(target, field), | ||
})) | ||
.reduce((fields, field) => { | ||
fields[field.metadata.name] = field.type; | ||
return fields; | ||
} , {} as { [key: string]: any}), | ||
}); | ||
} | ||
return interfaceTypeCache[metadata.name]; | ||
}) | ||
.find((_, index) => index === 0); | ||
} |
Oops, something went wrong.