Skip to content

Commit

Permalink
feat(TypeScript): add TArgsMap to ComposeFieldConfig, TSource t…
Browse files Browse the repository at this point in the history
…o `InterfaceTC` (#139)

* feat(typedefs): add `TSource` support in `InterfaceTypeComposer`

* refactor(typedefs): remove unused imports in `InterfaceTypeComposer`

* feat(typedefs): add `TArgsMap` support to `ComposeFieldConfig`

* fix(typedefs): optimise `TArgs` usages
  • Loading branch information
mernxl committed Sep 18, 2018
1 parent 88c3d13 commit ed8b185
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 110 deletions.
108 changes: 43 additions & 65 deletions src/InterfaceTypeComposer.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
import {
GraphQLArgumentConfig,
GraphQLFieldConfig,
GraphQLFieldConfigArgumentMap,
GraphQLInputType,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLList,
GraphQLNonNull,
getNamedType,
GraphQLFieldConfig,
GraphQLFieldConfigMap,
GraphQLObjectType,
GraphQLOutputType,
GraphQLInputType,
GraphQLFieldConfigArgumentMap,
GraphQLArgumentConfig,
GraphQLTypeResolver,
GraphQLResolveInfo,
} from './graphql';
import { TypeAsString } from './TypeMapper';
import {
Resolver,
ResolverOpts,
ResolverNextRpCb,
ResolverWrapCb,
} from './Resolver';
import { ProjectionType } from './utils/projection';
import { Thunk } from './utils/definitions';

import { isObject, isString } from './utils/is';
import { resolveMaybeThunk } from './utils/misc';
GraphQLTypeResolver,
} from 'graphql';
import { SchemaComposer } from './SchemaComposer';
import {
TypeComposer,
ComposeFieldConfigMap,
ComposeFieldConfig,
ComposeFieldConfigMap,
TypeComposer,
} from './TypeComposer';
import { TypeAsString } from './TypeMapper';
import { Thunk } from './utils/definitions';

export type GraphQLInterfaceTypeExtended<
TSource,
Expand Down Expand Up @@ -60,88 +48,78 @@ export type ComposeInterfaceTypeConfig<TSource, TContext> = {
description?: string | null;
};

export class InterfaceTypeComposer<TContext> {
export class InterfaceTypeComposer<TSource = any, TContext = any> {
public static schemaComposer: SchemaComposer<any>;
public schemaComposer: SchemaComposer<any>;
public schemaComposer: SchemaComposer<TSource>;

protected gqType: GraphQLInterfaceTypeExtended<any, TContext>;
protected gqType: GraphQLInterfaceTypeExtended<TSource, TContext>;

public constructor(gqType: GraphQLInterfaceType);

public static create<TCtx>(
public static create<TSrc = any, TCtx = any>(
opts:
| TypeAsString
| ComposeInterfaceTypeConfig<any, TCtx>
| ComposeInterfaceTypeConfig<TSrc, TCtx>
| GraphQLInterfaceType,
): InterfaceTypeComposer<TCtx>;
): InterfaceTypeComposer<TSrc, TCtx>;

public static createTemp<TCtx>(
public static createTemp<TSrc = any, TCtx = any>(
opts:
| TypeAsString
| ComposeInterfaceTypeConfig<any, TCtx>
| ComposeInterfaceTypeConfig<TSrc, TCtx>
| GraphQLInterfaceType,
): InterfaceTypeComposer<TCtx>;
): InterfaceTypeComposer<TSrc, TCtx>;

// -----------------------------------------------
// Field methods
// -----------------------------------------------

public hasField(name: string): boolean;

public getFields(): ComposeFieldConfigMap<any, TContext>;
public getFields(): ComposeFieldConfigMap<TSource, TContext>;

public getField(name: string): ComposeFieldConfig<any, TContext>;
public getField(name: string): ComposeFieldConfig<TSource, TContext>;

public getFieldNames(): string[];

public setFields(
fields: ComposeFieldConfigMap<any, TContext>,
): InterfaceTypeComposer<TContext>;
public setFields(fields: ComposeFieldConfigMap<TSource, TContext>): this;

public setField(
name: string,
fieldConfig: ComposeFieldConfig<any, TContext>,
): InterfaceTypeComposer<TContext>;
fieldConfig: ComposeFieldConfig<TSource, TContext>,
): this;

/**
* Add new fields or replace existed in a GraphQL type
*/
public addFields(
newValues: ComposeFieldConfigMap<any, TContext>,
): InterfaceTypeComposer<TContext>;
public addFields(newValues: ComposeFieldConfigMap<TSource, TContext>): this;

public removeField(
nameOrArray: string | string[],
): InterfaceTypeComposer<TContext>;
public removeField(nameOrArray: string | string[]): this;

public removeOtherFields(
fieldNameOrArray: string | string[],
): InterfaceTypeComposer<TContext>;
public removeOtherFields(fieldNameOrArray: string | string[]): this;

public reorderFields(names: string[]): InterfaceTypeComposer<TContext>;
public reorderFields(names: string[]): this;

public extendField(
fieldName: string,
parialFieldConfig: ComposeFieldConfig<any, TContext>,
): InterfaceTypeComposer<TContext>;
parialFieldConfig: ComposeFieldConfig<TSource, TContext>,
): this;

public isFieldNonNull(fieldName: string): boolean;

public getFieldConfig(fieldName: string): GraphQLFieldConfig<any, TContext>;
public getFieldConfig(
fieldName: string,
): GraphQLFieldConfig<TSource, TContext>;

public getFieldType(fieldName: string): GraphQLOutputType;

public getFieldTC<TSource>(
fieldName: string,
): TypeComposer<TSource, TContext>;

public makeFieldNonNull(
fieldNameOrArray: string | string[],
): InterfaceTypeComposer<TContext>;
public makeFieldNonNull(fieldNameOrArray: string | string[]): this;

public makeFieldNullable(
fieldNameOrArray: string | string[],
): InterfaceTypeComposer<TContext>;
public makeFieldNullable(fieldNameOrArray: string | string[]): this;

public deprecateFields(
fields: { [fieldName: string]: string } | string[] | string,
Expand All @@ -167,13 +145,13 @@ export class InterfaceTypeComposer<TContext> {

public getTypeName(): string;

public setTypeName(name: string): InterfaceTypeComposer<TContext>;
public setTypeName(name: string): this;

public getDescription(): string;

public setDescription(description: string): InterfaceTypeComposer<TContext>;
public setDescription(description: string): this;

public clone(newTypeName: string): InterfaceTypeComposer<TContext>;
public clone(newTypeName: string): this;

// -----------------------------------------------
// ResolveType methods
Expand All @@ -183,7 +161,7 @@ export class InterfaceTypeComposer<TContext> {
type: TypeComposer<any, TContext> | GraphQLObjectType,
): boolean;

public getTypeResolvers(): InterfaceTypeResolversMap<any, TContext>;
public getTypeResolvers(): InterfaceTypeResolversMap<TSource, TContext>;

public getTypeResolverCheckFn(
type: TypeComposer<any, TContext> | GraphQLObjectType,
Expand All @@ -195,16 +173,16 @@ export class InterfaceTypeComposer<TContext> {

public setTypeResolvers(
typeResolversMap: InterfaceTypeResolversMap<any, TContext>,
): InterfaceTypeComposer<TContext>;
): this;

public addTypeResolver(
type: TypeComposer<any, TContext> | GraphQLObjectType,
checkFn: InterfaceTypeResolverCheckFn<any, TContext>,
): InterfaceTypeComposer<TContext>;
): this;

public removeTypeResolver(
type: TypeComposer<any, TContext> | GraphQLObjectType,
): InterfaceTypeComposer<TContext>;
): this;

// -----------------------------------------------
// Misc methods
Expand Down
6 changes: 3 additions & 3 deletions src/Resolver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type ResolverSortArgConfig<TSource, TContext> = {
export type ResolverOpts<TSource, TContext, TArgs = any> = {
type?: ComposeOutputType<TSource, TContext>;
resolve?: ResolverRpCb<TSource, TContext, TArgs>;
args?: ComposeFieldConfigArgumentMap;
args?: ComposeFieldConfigArgumentMap<TArgs>;
name?: string;
displayName?: string;
kind?: ResolverKinds;
Expand Down Expand Up @@ -107,7 +107,7 @@ export class Resolver<TSource = any, TContext = any, TArgs = any> {
public schemaComposer: SchemaComposer<TContext>;

public type: ComposeOutputType<TSource, TContext>;
public args: ComposeFieldConfigArgumentMap;
public args: ComposeFieldConfigArgumentMap<TArgs>;
public resolve: ResolverRpCb<TSource, TContext, TArgs>;
public name: string;
public displayName: string | null;
Expand Down Expand Up @@ -141,7 +141,7 @@ export class Resolver<TSource = any, TContext = any, TArgs = any> {

public getArgTC(argName: string): InputTypeComposer;

public getArgs(): ComposeFieldConfigArgumentMap;
public getArgs(): ComposeFieldConfigArgumentMap<TArgs>;

public getArgNames(): string[];

Expand Down

0 comments on commit ed8b185

Please sign in to comment.