Skip to content

Commit

Permalink
chore: publish 5.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Sep 2, 2018
1 parent db01e71 commit b58d3e5
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 26 deletions.
6 changes: 1 addition & 5 deletions lib/decorators/resolvers.decorators.ts
Expand Up @@ -30,11 +30,7 @@ export function createPropertyDecorator(
key,
descriptor,
);
ReflectMetadata(RESOLVER_PROPERTY_METADATA, propertyName)(
target,
key,
descriptor,
);
ReflectMetadata(RESOLVER_PROPERTY_METADATA, true)(target, key, descriptor);
};
}

Expand Down
5 changes: 4 additions & 1 deletion lib/graphql.factory.ts
Expand Up @@ -3,6 +3,7 @@ import { gql, makeExecutableSchema } from 'apollo-server-express';
import * as fs from 'fs';
import * as glob from 'glob';
import { MergeInfo } from 'graphql-tools/dist/Interfaces';
import { flatten } from 'lodash';
import { mergeTypes } from 'merge-graphql-schemas';
import { GqlModuleOptions } from './interfaces/gql-module-options.interface';
import { DelegatesExplorerService } from './services/delegates-explorer.service';
Expand Down Expand Up @@ -40,7 +41,9 @@ export class GraphQLFactory {
}

mergeTypesByPaths(...pathsToTypes: string[]): string {
return mergeTypes(...pathsToTypes.map(pattern => this.loadFiles(pattern)));
return mergeTypes(
flatten(pathsToTypes.map(pattern => this.loadFiles(pattern))),
);
}

private loadFiles(pattern: string): any[] {
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces/gql-module-options.interface.ts
Expand Up @@ -8,6 +8,7 @@ export interface GqlModuleOptions
Pick<ServerRegistration, 'onHealthCheck' | 'disableHealthCheck' | 'path'>
> {
typePaths?: string[];
include?: Function[];
installSubscriptionHandlers?: boolean;
}

Expand Down
15 changes: 14 additions & 1 deletion lib/services/base-explorer.service.ts
@@ -1,7 +1,20 @@
import { flattenDeep, groupBy, identity, mapValues } from 'lodash';
import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash';
import { ResolverMetadata } from '../interfaces/resolver-metadata.interface';

export class BaseExplorerService {
getModules(modulesContainer: Map<any, any>, include: Function[]) {
const mapToProviders = (list: any[]) =>
list.map(module => module.components);
if (!include || isEmpty(include)) {
return mapToProviders([...modulesContainer.values()]);
}
return mapToProviders(
[...modulesContainer.values()].filter(({ metatype }) =>
include.some(item => item === metatype),
),
);
}

flatMap<T = ResolverMetadata[]>(
modules: Map<any, any>[],
callback: (instance: any) => T,
Expand Down
11 changes: 8 additions & 3 deletions lib/services/delegates-explorer.service.ts
@@ -1,8 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { MergeInfo } from 'graphql-tools/dist/Interfaces';
import { mapValues } from 'lodash';
import { GRAPHQL_MODULE_OPTIONS } from '../graphql.constants';
import { GqlModuleOptions } from '../interfaces/gql-module-options.interface';
import { ResolverMetadata } from '../interfaces/resolver-metadata.interface';
import { extractMetadata } from '../utils/extract-metadata.util';
import { BaseExplorerService } from './base-explorer.service';
Expand All @@ -12,13 +14,16 @@ export class DelegatesExplorerService extends BaseExplorerService {
constructor(
private readonly modulesContainer: ModulesContainer,
private readonly metadataScanner: MetadataScanner,
@Inject(GRAPHQL_MODULE_OPTIONS)
private readonly gqlOptions: GqlModuleOptions,
) {
super();
}

explore() {
const modules = [...this.modulesContainer.values()].map(
module => module.components,
const modules = this.getModules(
this.modulesContainer,
this.gqlOptions.include || [],
);
const delegates = this.flatMap(modules, instance =>
this.filterDelegates(instance),
Expand Down
27 changes: 21 additions & 6 deletions lib/services/resolvers-explorer.service.ts
@@ -1,10 +1,16 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { isUndefined } from '@nestjs/common/utils/shared.utils';
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { GqlModuleOptions } from '..';
import { Resolvers } from '../enums/resolvers.enum';
import { GqlParamsFactory } from '../factories/params.factory';
import { PARAM_ARGS_METADATA, SUBSCRIPTION_TYPE } from '../graphql.constants';
import {
GRAPHQL_MODULE_OPTIONS,
PARAM_ARGS_METADATA,
SUBSCRIPTION_TYPE,
} from '../graphql.constants';
import { ResolverMetadata } from '../interfaces/resolver-metadata.interface';
import { extractMetadata } from '../utils/extract-metadata.util';
import { BaseExplorerService } from './base-explorer.service';
Expand All @@ -17,13 +23,16 @@ export class ResolversExplorerService extends BaseExplorerService {
private readonly modulesContainer: ModulesContainer,
private readonly metadataScanner: MetadataScanner,
private readonly externalContextCreator: ExternalContextCreator,
@Inject(GRAPHQL_MODULE_OPTIONS)
private readonly gqlOptions: GqlModuleOptions,
) {
super();
}

explore() {
const modules = [...this.modulesContainer.values()].map(
module => module.components,
const modules = this.getModules(
this.modulesContainer,
this.gqlOptions.include || [],
);
const resolvers = this.flatMap(modules, instance =>
this.filterResolvers(instance),
Expand All @@ -36,8 +45,14 @@ export class ResolversExplorerService extends BaseExplorerService {
return undefined;
}
const prototype = Object.getPrototypeOf(instance);
const predicate = (resolverType, isDelegated) =>
isUndefined(resolverType) || isDelegated;
const predicate = (resolverType, isDelegated, isPropertyResolver) =>
isUndefined(resolverType) ||
isDelegated ||
(!isPropertyResolver &&
![Resolvers.MUTATION, Resolvers.QUERY, Resolvers.SUBSCRIPTION].some(
type => type === resolverType,
));

const resolvers = this.metadataScanner.scanFromPrototype(
instance,
prototype,
Expand Down
19 changes: 14 additions & 5 deletions lib/services/scalars-explorer.service.ts
@@ -1,17 +1,26 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
import { SCALAR_NAME_METADATA } from '../graphql.constants';
import { GqlModuleOptions } from '..';
import {
GRAPHQL_MODULE_OPTIONS,
SCALAR_NAME_METADATA,
} from '../graphql.constants';
import { BaseExplorerService } from './base-explorer.service';

@Injectable()
export class ScalarsExplorerService extends BaseExplorerService {
constructor(private readonly modulesContainer: ModulesContainer) {
constructor(
private readonly modulesContainer: ModulesContainer,
@Inject(GRAPHQL_MODULE_OPTIONS)
private readonly gqlOptions: GqlModuleOptions,
) {
super();
}

explore() {
const modules = [...this.modulesContainer.values()].map(
module => module.components,
const modules = this.getModules(
this.modulesContainer,
this.gqlOptions.include || [],
);
return this.flatMap<any>(modules, instance => this.filterScalar(instance));
}
Expand Down
13 changes: 11 additions & 2 deletions lib/utils/extract-metadata.util.ts
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import {
RESOLVER_DELEGATE_METADATA,
RESOLVER_NAME_METADATA,
RESOLVER_PROPERTY_METADATA,
RESOLVER_TYPE_METADATA,
} from '../graphql.constants';
import { ResolverMetadata } from '../interfaces/resolver-metadata.interface';
Expand All @@ -10,19 +11,27 @@ export function extractMetadata(
instance,
prototype,
methodName: string,
filterPredicate: (resolverType: string, isDelegated: boolean) => boolean,
filterPredicate: (
resolverType: string,
isDelegated: boolean,
isPropertyResolver?: boolean,
) => boolean,
): ResolverMetadata {
const callback = prototype[methodName];
const resolverType =
Reflect.getMetadata(RESOLVER_TYPE_METADATA, callback) ||
Reflect.getMetadata(RESOLVER_TYPE_METADATA, instance.constructor);

const isPropertyResolver = Reflect.getMetadata(
RESOLVER_PROPERTY_METADATA,
callback,
);
const resolverName = Reflect.getMetadata(RESOLVER_NAME_METADATA, callback);
const isDelegated = !!Reflect.getMetadata(
RESOLVER_DELEGATE_METADATA,
callback,
);
if (filterPredicate(resolverType, isDelegated)) {
if (filterPredicate(resolverType, isDelegated, isPropertyResolver)) {
return null;
}
return {
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@nestjs/graphql",
"version": "5.0.0",
"version": "5.1.0",
"description":
"Nest - modern, fast, powerful node.js web framework (@graphql)",
"author": "Kamil Mysliwiec",
Expand Down Expand Up @@ -31,8 +31,8 @@
"merge-graphql-schemas": "^1.3.0"
},
"peerDependencies": {
"@nestjs/common": "^5.0.0",
"@nestjs/core": "^5.0.0",
"@nestjs/common": "^5.3.0",
"@nestjs/core": "^5.3.0",
"apollo-server-express": "^2.0.4",
"graphql": "^0.13.2",
"reflect-metadata": "^0.1.12"
Expand Down

0 comments on commit b58d3e5

Please sign in to comment.