import { GraphQLObjectType , GraphQLObjectTypeConfig , GraphQLFieldConfig , GraphQLField , } from 'graphql/type' ; import { GraphQLDirectiveValue , } from './directives' ; import invariant from '../jsutils/invariant' ; function resolveThunk < T > ( thunk ) { return typeof thunk === 'function' ? thunk ( ) : thunk ; } export interface GraphQLObjectTypeConfigExt < TSource , TContext > extends GraphQLObjectTypeConfig < TSource , TContext > { directives ? : Thunk < Array < GraphQLDirectiveValue > > ; [ prop ] : mixed ; } export interface GraphQLFieldConfigMapExt < TSource , TContext > { [ fieldName ] : GraphQLFieldConfigExt < TSource , TContext > ; } export interface GraphQLFieldConfigExt < TSource , TContext > extends GraphQLFieldConfig < TSource , TContext > { directives : Array < GraphQLDirectiveValue > ; [ prop ] : mixed ; } export interface GraphQLFieldExt < TSource , TContext > extends GraphQLField < TSource , TContext > { directives : Array < GraphQLDirectiveValue > ; } skip export class GraphQLObjectTypeExt extends GraphQLObjectType { private _typeConfig : GraphQLObjectTypeConfigExt < mixed , mixed > ; private _directives : Array < GraphQLDirectiveValue > ; constructor ( config ) { super ( config ) ; } get config ( ) : GraphQLObjectTypeConfigExt < mixed , mixed > { return this . _typeConfig ; } public getDirectives ( ) { return this . _directives || ( this . _directives = defineDirectives ( this , this . _typeConfig . directives ) ) ; } public extendConfig ( extra = { } ) { this . _typeConfig = { ... this . _typeConfig , ... extra , } ; } } function defineDirectives ( type , directivesThunk ? ) { if ( ! directivesThunk ) { return [ ] ; } const directives = resolveThunk ( directivesThunk ) ; if ( ! directives ) { return [ ] ; } invariant ( Array . isArray ( directives ) , `${ type . name } directives must be an Array or a function which returns ` + 'an Array.' ) ; directives . forEach ( directive => { invariant ( directive instanceof GraphQLDirectiveValue , `${ type . name } expects directive values, it cannot ` + `have directives: ${ String ( directive ) }.` ) ; } ) ; return directives ; }