Skip to content

Commit

Permalink
fix(ObjectTypeComposer): now accept any ComposeInterfaceType + bunch …
Browse files Browse the repository at this point in the history
…of small type fixes
  • Loading branch information
nodkz committed Apr 4, 2019
1 parent fa6d6a1 commit ecfadec
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 51 deletions.
8 changes: 7 additions & 1 deletion src/EnumTypeComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GraphQLEnumTypeConfig,
GraphQLEnumValueConfigMap,
} from './graphql';
import { TypeAsString } from './TypeMapper';
import { TypeAsString, TypeDefinitionString } from './TypeMapper';
import { SchemaComposer } from './SchemaComposer';
import { Extensions } from './utils/definitions';

Expand All @@ -17,6 +17,12 @@ export type ComposeEnumTypeConfig = GraphQLEnumTypeConfig & {

export type EnumTypeComposeDefinition = TypeAsString | ComposeEnumTypeConfig | GraphQLEnumType;

export type ComposeEnumType =
| EnumTypeComposer<any>
| GraphQLEnumType
| TypeDefinitionString
| TypeAsString;

export type GraphQLEnumTypeExtended = GraphQLEnumType & {
_gqcExtensions?: Extensions;
};
Expand Down
8 changes: 7 additions & 1 deletion src/EnumTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from './graphql';
import { defineEnumValues, defineEnumValuesToConfig } from './utils/configToDefine';
import { graphqlVersion } from './utils/graphqlVersion';
import type { TypeAsString } from './TypeMapper';
import type { TypeAsString, TypeDefinitionString } from './TypeMapper';
import { SchemaComposer } from './SchemaComposer';
import type { Extensions } from './utils/definitions';

Expand All @@ -25,6 +25,12 @@ export type EnumTypeComposeDefinition =
| $ReadOnly<ComposeEnumTypeConfig>
| $ReadOnly<GraphQLEnumType>;

export type ComposeEnumType =
| EnumTypeComposer<any>
| GraphQLEnumType
| TypeDefinitionString
| TypeAsString;

export type GraphQLEnumTypeExtended = GraphQLEnumType & {
_gqcExtensions?: Extensions,
};
Expand Down
8 changes: 7 additions & 1 deletion src/InterfaceTypeComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { EnumTypeComposer } from './EnumTypeComposer';
import { UnionTypeComposer } from './UnionTypeComposer';
import { ScalarTypeComposer } from './ScalarTypeComposer';
import { TypeAsString } from './TypeMapper';
import { TypeAsString, TypeDefinitionString } from './TypeMapper';
import { Thunk, MaybePromise, Extensions } from './utils/definitions';

export type GraphQLInterfaceTypeExtended<TSource, TContext> = GraphQLInterfaceType & {
Expand Down Expand Up @@ -56,6 +56,12 @@ export type InterfaceTypeComposeDefinition<TSource, TContext> =
| TypeAsString
| ComposeInterfaceTypeConfig<TSource, TContext>;

export type ComposeInterfaceType =
| InterfaceTypeComposer<any, any>
| GraphQLInterfaceType
| TypeDefinitionString
| TypeAsString;

/**
* Class that helps to create `GraphQLInterfaceType`s and provide ability to modify them.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/InterfaceTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { InputTypeComposer } from './InputTypeComposer';
import type { EnumTypeComposer } from './EnumTypeComposer';
import type { UnionTypeComposer } from './UnionTypeComposer';
import type { ScalarTypeComposer } from './ScalarTypeComposer';
import type { TypeAsString } from './TypeMapper';
import type { TypeAsString, TypeDefinitionString } from './TypeMapper';
import { SchemaComposer } from './SchemaComposer';
import type {
ComposeFieldConfigMap,
Expand Down Expand Up @@ -71,6 +71,12 @@ export type InterfaceTypeComposeDefinition<TSource, TContext> =
| TypeAsString
| ComposeInterfaceTypeConfig<TSource, TContext>;

export type ComposeInterfaceType =
| InterfaceTypeComposer<any, any>
| GraphQLInterfaceType
| TypeDefinitionString
| TypeAsString;

export class InterfaceTypeComposer<TSource, TContext> {
gqType: GraphQLInterfaceTypeExtended<TSource, TContext>;
schemaComposer: SchemaComposer<TContext>;
Expand Down
20 changes: 12 additions & 8 deletions src/ObjectTypeComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { ScalarTypeComposer } from './ScalarTypeComposer';
import { EnumTypeComposer } from './EnumTypeComposer';
import { InputTypeComposer } from './InputTypeComposer';
import { InterfaceTypeComposer } from './InterfaceTypeComposer';
import { InterfaceTypeComposer, ComposeInterfaceType } from './InterfaceTypeComposer';
import { UnionTypeComposer } from './UnionTypeComposer';
import {
Resolver,
Expand All @@ -29,7 +29,7 @@ import {
ResolverMiddleware,
} from './Resolver';
import { SchemaComposer } from './SchemaComposer';
import { TypeAsString } from './TypeMapper';
import { TypeAsString, TypeDefinitionString } from './TypeMapper';
import { ObjMap, Thunk, Extensions } from './utils/definitions';
import { ProjectionType } from './utils/projection';

Expand All @@ -45,14 +45,14 @@ export type GraphQLObjectTypeExtended<TSource, TContext> = GraphQLObjectType & {
_gqcGetRecordIdFn?: GetRecordIdFn<TSource, TContext>;
_gqcRelations?: RelationThunkMap<TSource, TContext>;
_gqcFields?: ComposeFieldConfigMap<TSource, TContext>;
_gqcInterfaces?: Array<GraphQLInterfaceType | InterfaceTypeComposer<any, TContext>>;
_gqcInterfaces?: ComposeInterfaceType[];
_gqcExtensions?: Extensions;
description: string | null;
};

export type ComposeObjectTypeConfig<TSource, TContext> = {
name: string;
interfaces?: Thunk<GraphQLInterfaceType[] | null>;
interfaces?: Thunk<ComposeInterfaceType[] | null>;
fields?: Thunk<ComposeFieldConfigMap<TSource, TContext>>;
isTypeOf?: GraphQLIsTypeOfFn<TSource, TContext> | null;
description?: string | null;
Expand Down Expand Up @@ -198,6 +198,12 @@ export type ObjectTypeComposeDefinition<TSource, TContext> =
| ComposeObjectTypeConfig<TSource, TContext>
| GraphQLObjectType;

export type ComposeObjectType =
| ObjectTypeComposer<any, any>
| GraphQLObjectType
| TypeDefinitionString
| TypeAsString;

/**
* Main class that gets `GraphQLObjectType` and provide ability to change them.
*/
Expand Down Expand Up @@ -413,11 +419,9 @@ export class ObjectTypeComposer<TSource = any, TContext = any> {
* -----------------------------------------------
*/

public getInterfaces(): Array<InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType>;
public getInterfaces(): ComposeInterfaceType[];

public setInterfaces(
interfaces: Array<InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType>
): this;
public setInterfaces(interfaces: Array<ComposeInterfaceType | GraphQLInterfaceType>): this;

public hasInterface(
iface: string | InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType
Expand Down
47 changes: 30 additions & 17 deletions src/ObjectTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import type {
import { ScalarTypeComposer } from './ScalarTypeComposer';
import { EnumTypeComposer } from './EnumTypeComposer';
import { InputTypeComposer } from './InputTypeComposer';
import type { TypeAsString } from './TypeMapper';
import { InterfaceTypeComposer } from './InterfaceTypeComposer';
import type { TypeAsString, TypeDefinitionString } from './TypeMapper';
import { InterfaceTypeComposer, type ComposeInterfaceType } from './InterfaceTypeComposer';
import { UnionTypeComposer } from './UnionTypeComposer';
import {
Resolver,
Expand All @@ -39,7 +39,11 @@ import {
import { SchemaComposer } from './SchemaComposer';
import { resolveMaybeThunk, upperFirst, inspect } from './utils/misc';
import { isObject, isFunction, isString } from './utils/is';
import { resolveOutputConfigMapAsThunk, resolveOutputConfigAsThunk } from './utils/configAsThunk';
import {
resolveOutputConfigMapAsThunk,
resolveOutputConfigAsThunk,
resolveInterfaceArrayAsThunk,
} from './utils/configAsThunk';
import { defineFieldMap, defineFieldMapToConfig } from './utils/configToDefine';
import { toInputObjectType } from './utils/toInputObjectType';
import { typeByPath } from './utils/typeByPath';
Expand All @@ -60,14 +64,14 @@ export type GraphQLObjectTypeExtended<TSource, TContext> = GraphQLObjectType & {
_gqcGetRecordIdFn?: GetRecordIdFn<TSource, TContext>,
_gqcRelations?: RelationThunkMap<TSource, TContext>,
_gqcFields?: ComposeFieldConfigMap<TSource, TContext>,
_gqcInterfaces?: Array<GraphQLInterfaceType | InterfaceTypeComposer<any, TContext>>,
_gqcInterfaces?: Array<ComposeInterfaceType>,
_gqcExtensions?: Extensions,
description?: ?string,
};

export type ComposeObjectTypeConfig<TSource, TContext> = {
+name: string,
+interfaces?: Thunk<GraphQLInterfaceType[] | null>,
+interfaces?: Thunk<Array<ComposeInterfaceType> | null>,
+fields?: Thunk<ComposeFieldConfigMap<TSource, TContext>>,
+isTypeOf?: ?GraphQLIsTypeOfFn<TSource, TContext>,
+description?: string | null,
Expand Down Expand Up @@ -202,6 +206,12 @@ export type ObjectTypeComposeDefinition<TSource, TContext> =
| ComposeObjectTypeConfig<TSource, TContext>
| GraphQLObjectType;

export type ComposeObjectType =
| ObjectTypeComposer<any, any>
| GraphQLObjectType
| TypeDefinitionString
| TypeAsString;

export class ObjectTypeComposer<TSource, TContext> {
gqType: GraphQLObjectTypeExtended<TSource, TContext>;
schemaComposer: SchemaComposer<TContext>;
Expand Down Expand Up @@ -254,14 +264,20 @@ export class ObjectTypeComposer<TSource, TContext> {
TC = new ObjectTypeComposer(typeDef, sc);
} else if (isObject(typeDef)) {
const fields = typeDef.fields;
const interfaces = typeDef.interfaces;
const type = new GraphQLObjectType({
...(typeDef: any),
fields: isFunction(fields)
? () => resolveOutputConfigMapAsThunk(sc, (fields(): any), typeDef.name)
: () => ({}),
interfaces: isFunction(interfaces)
? () => resolveInterfaceArrayAsThunk(sc, (interfaces: any), typeDef.name)
: [],
});
TC = new ObjectTypeComposer(type, sc);
if (isObject(fields)) TC.addFields(fields);
if (Array.isArray(interfaces)) TC.setInterfaces(interfaces);

TC.gqType._gqcExtensions = typeDef.extensions || {};
} else {
throw new Error(
Expand Down Expand Up @@ -832,34 +848,31 @@ export class ObjectTypeComposer<TSource, TContext> {
// Interface methods
// -----------------------------------------------

getInterfaces(): Array<GraphQLInterfaceType | InterfaceTypeComposer<any, TContext>> {
getInterfaces(): Array<ComposeInterfaceType> {
if (!this.gqType._gqcInterfaces) {
let interfaces: any;
if (graphqlVersion >= 14) {
interfaces = this.gqType._interfaces;
} else {
interfaces = (this.gqType: any)._typeConfig.interfaces;
}
this.gqType._gqcInterfaces = (resolveMaybeThunk(interfaces) || []: any);
this.gqType._gqcInterfaces = (resolveInterfaceArrayAsThunk(
this.schemaComposer,
interfaces,
this.getTypeName()
) || []: any);
}

return this.gqType._gqcInterfaces;
}

setInterfaces(
interfaces: Array<InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType>
interfaces: $ReadOnlyArray<ComposeInterfaceType | GraphQLInterfaceType>
): ObjectTypeComposer<TSource, TContext> {
this.gqType._gqcInterfaces = interfaces;
this.gqType._gqcInterfaces = [...interfaces];
const interfacesThunk = () => {
return interfaces.map(iface => {
if (iface instanceof GraphQLInterfaceType) {
return iface;
} else if (iface instanceof InterfaceTypeComposer) {
return iface.getType();
}
throw new Error(
`For type ${this.getTypeName()} you provide incorrect interface object ${inspect(iface)}`
);
return this.schemaComposer.typeMapper.convertInterfaceType(iface);
});
};

Expand Down
8 changes: 8 additions & 0 deletions src/SchemaComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type ExtraSchemaConfig = {
astNode?: SchemaDefinitionNode | null;
};

type AnyComposeType<TContext> =
| ObjectTypeComposer<any, TContext>
| InputTypeComposer<TContext>
| EnumTypeComposer<TContext>
| InterfaceTypeComposer<any, TContext>
| UnionTypeComposer<any, TContext>
| ScalarTypeComposer<TContext>;

type AnyType<TContext> =
| ObjectTypeComposer<any, TContext>
| InputTypeComposer<TContext>
Expand Down
23 changes: 13 additions & 10 deletions src/TypeMapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
GraphQLType,
GraphQLObjectType,
} from 'graphql';
import { SchemaComposer } from './SchemaComposer';
import { SchemaComposer, AnyComposeType, AnyType } from './SchemaComposer';
import { ComposeInputFieldConfig, ComposeInputFieldConfigMap } from './InputTypeComposer';
import {
ObjectTypeComposer,
ComposeArgumentConfig,
ComposeFieldConfig,
ComposeFieldConfigArgumentMap,
ComposeFieldConfigMap,
ComposeObjectType,
} from './ObjectTypeComposer';
import { TypeDefinitionString, TypeNameString, TypeWrappedString } from './TypeMapper';
import { TypeStorage } from './TypeStorage';
Expand All @@ -39,12 +40,6 @@ export type TypeNameString = string;

export type TypeAsString = TypeDefinitionString | TypeWrappedString | TypeNameString;

export type ComposeObjectType =
| ObjectTypeComposer<any, any>
| GraphQLObjectType
| TypeDefinitionString
| TypeAsString;

/**
* Type storage and type generator from `Schema Definition Language` (`SDL`).
* This is slightly rewritten [buildASTSchema](https://github.com/graphql/graphql-js/blob/master/src/utilities/buildASTSchema.js)
Expand All @@ -58,15 +53,17 @@ declare class TypeMapper<TContext> {

public get(name: string): GraphQLNamedType | void;

public set(name: string, type: GraphQLNamedType): void;
public set(name: string, type: AnyType<any>): void;

public has(name: string): boolean;

public getWrapped(str: TypeWrappedString | TypeNameString): GraphQLType | null;

public createType(str: TypeDefinitionString): GraphQLNamedType | void;
public createType(str: TypeDefinitionString): AnyComposeType<TContext> | void;

public createGraphQLType(str: TypeDefinitionString): GraphQLType | void;

public parseTypesFromString(str: string): TypeStorage<string, GraphQLNamedType>;
public parseTypesFromString(str: string): TypeStorage<string, AnyComposeType<TContext>>;

public parseTypesFromAst(astDocument: DocumentNode): TypeStorage<string, GraphQLNamedType>;

Expand Down Expand Up @@ -108,4 +105,10 @@ declare class TypeMapper<TContext> {
composeFields: ComposeInputFieldConfigMap,
typeName?: string
): GraphQLInputFieldConfigMap;

/**
* -----------------------------------------------
* Internal methods
* -----------------------------------------------
*/
}

0 comments on commit ecfadec

Please sign in to comment.