Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved generic typings for makeExecutableSchema #662

Merged
merged 4 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### vNEXT

* When concatenating errors maintain a reference to the original for use downstream [Issue #480](https://github.com/apollographql/graphql-tools/issues/480) [PR #637](https://github.com/apollographql/graphql-tools/pull/637)

* Improve generic typings for several resolver-related interfaces [PR #662](https://github.com/apollographql/graphql-tools/pull/662)

### v2.21.0

Expand Down
40 changes: 20 additions & 20 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export interface IResolverValidationOptions {
allowResolversNotInSchema?: boolean;
}

export interface IResolverOptions {
resolve?: IFieldResolver<any, any>;
subscribe?: IFieldResolver<any, any>;
__resolveType?: GraphQLTypeResolver<any, any>;
__isTypeOf?: GraphQLIsTypeOfFn<any, any>;
export interface IResolverOptions<TSource = any, TContext = any> {
resolve?: IFieldResolver<TSource, TContext>;
subscribe?: IFieldResolver<TSource, TContext>;
__resolveType?: GraphQLTypeResolver<TSource, TContext>;
__isTypeOf?: GraphQLIsTypeOfFn<TSource, TContext>;
}

export type MergeInfo = {
Expand All @@ -47,37 +47,37 @@ export type IFieldResolver<TSource, TContext> = (

export type ITypedef = (() => ITypedef[]) | string | DocumentNode;
export type ITypeDefinitions = ITypedef | ITypedef[];
export type IResolverObject = {
[key: string]: IFieldResolver<any, any> | IResolverOptions;
export type IResolverObject<TSource = any, TContext = any> = {
[key: string]: IFieldResolver<TSource, TContext> | IResolverOptions;
};
export type IEnumResolver = { [key: string]: string | number };
export interface IResolvers {
export interface IResolvers<TSource = any, TContext = any> {
[key: string]:
| (() => any)
| IResolverObject
| IResolverObject<TSource, TContext>
| GraphQLScalarType
| IEnumResolver;
}
export interface ILogger {
log: (message: string | Error) => void;
}

export interface IConnectorCls {
new (context?: any): any;
export interface IConnectorCls<TContext = any> {
new (context?: TContext): any;
}
export type IConnectorFn = (context?: any) => any;
export type IConnector = IConnectorCls | IConnectorFn;
export type IConnectorFn<TContext = any> = (context?: TContext) => any;
export type IConnector<TContext = any> = IConnectorCls<TContext> | IConnectorFn<TContext>;

export type IConnectors = { [key: string]: IConnector };
export type IConnectors<TContext = any> = { [key: string]: IConnector<TContext> };

export interface IExecutableSchemaDefinition {
export interface IExecutableSchemaDefinition<TContext = any> {
typeDefs: ITypeDefinitions;
resolvers?: IResolvers | Array<IResolvers>;
connectors?: IConnectors;
resolvers?: IResolvers<any, TContext> | Array<IResolvers<any, TContext>>;
connectors?: IConnectors<TContext>;
logger?: ILogger;
allowUndefinedInResolve?: boolean;
resolverValidationOptions?: IResolverValidationOptions;
directiveResolvers?: IDirectiveResolvers<any, any>;
directiveResolvers?: IDirectiveResolvers<any, TContext>;
parseOptions?: GraphQLParseOptions;
}

Expand All @@ -88,15 +88,15 @@ export type IFieldIteratorFn = (
) => void;

export type NextResolverFn = () => Promise<any>;
export type DirectiveResolverFn<TSource, TContext> = (
export type DirectiveResolverFn<TSource = any, TContext = any> = (
next: NextResolverFn,
source: TSource,
args: { [argName: string]: any },
context: TContext,
info: GraphQLResolveInfo,
) => any;

export interface IDirectiveResolvers<TSource, TContext> {
export interface IDirectiveResolvers<TSource = any, TContext = any> {
[directiveName: string]: DirectiveResolverFn<TSource, TContext>;
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Interfaces';
export * from './schemaGenerator';
export * from './mock';
export * from './stitching';
4 changes: 2 additions & 2 deletions src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function _generateSchema(
return schema;
}

function makeExecutableSchema({
function makeExecutableSchema<TContext = any>({
typeDefs,
resolvers = {},
connectors,
Expand All @@ -120,7 +120,7 @@ function makeExecutableSchema({
resolverValidationOptions = {},
directiveResolvers = null,
parseOptions = {},
}: IExecutableSchemaDefinition) {
}: IExecutableSchemaDefinition<TContext>) {
const jsSchema = _generateSchema(
typeDefs,
resolvers,
Expand Down