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

Support ES6 and JavaScript #1446

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion packages/graphql-modules/src/di/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function Injectable(options?: ProviderOptions): ClassDecorator {
ensureReflect();

const params: Type<any>[] = (
Reflect.getMetadata('design:paramtypes', target) || []
Reflect.getMetadata('design:paramtypes', target) ||
(target as any).parameters || // ES6
[]
).map((param: any) => (isType(param) ? param : null));

const existingMeta = readInjectableMetadata(target as any);
Expand All @@ -36,6 +38,7 @@ export function Injectable(options?: ProviderOptions): ClassDecorator {
}),
options: {
...(existingMeta?.options || {}),
...((target as any).options || {}), // ES6
...(options || {}),
},
};
Expand Down Expand Up @@ -71,6 +74,8 @@ export function Inject(
type,
optional: false,
};

return target;
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/graphql-modules/src/di/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ export function ensureInjectableMetadata(type: Type<any>) {
(type as any)[INJECTABLE] = meta;
}
}

export function hasInjectableMetadata(type: Type<any>) {
return !!(type as any)[INJECTABLE];
}
13 changes: 12 additions & 1 deletion packages/graphql-modules/src/di/resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import {
import { invalidProviderError, noAnnotationError } from './errors';
import { Key } from './registry';
import { resolveForwardRef } from './forward-ref';
import { readInjectableMetadata, InjectableParamMetadata } from './metadata';
import {
readInjectableMetadata,
InjectableParamMetadata,
hasInjectableMetadata,
} from './metadata';
import { ReflectiveInjector } from './injector';
import { Injectable } from './decorators';

export type NormalizedProvider<T = any> =
| ValueProvider<T>
Expand Down Expand Up @@ -122,6 +127,12 @@ function resolveFactory(provider: NormalizedProvider): ResolvedFactory {
if (isClassProvider(provider)) {
const useClass = resolveForwardRef(provider.useClass);

// Support ES6 (no TypeScript)
// Sets params coming from static parameters getter
if (!hasInjectableMetadata(useClass)) {
Injectable(provider)(useClass);
}

factoryFn = makeFactory(useClass);
resolvedDeps = dependenciesFor(useClass);
executionContextIn = executionContextInFor(useClass);
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-modules/tests/di-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { makeExecutableSchema } from '@graphql-tools/schema';
import { parse } from 'graphql';
import { stringify } from '../src/di/utils';

test('No Injectable error', () => {
test.skip('No Injectable error', () => {
class NoAnnotations {
// @ts-ignore
constructor(dep: any) {}
Expand Down
132 changes: 132 additions & 0 deletions packages/graphql-modules/tests/di-es6.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'reflect-metadata';
import {
ReflectiveInjector,
forwardRef,
Scope,
ExecutionContext,
} from '../src/di';
import { OnDestroy } from '../src/shared/di';

test('take params from a static parameters getter', () => {
const spies = {
http: jest.fn(),
service: jest.fn(),
};

class Http {
constructor() {
spies.http();
}
}

class Service {
static get parameters() {
return [Http];
}

constructor(http: any) {
spies.service(http);
}
}

const providers = ReflectiveInjector.resolve([Http, Service]);
const injector = ReflectiveInjector.createFromResolved({
name: 'root',
providers,
});

expect(injector.get(Service)).toBeInstanceOf(Service);
expect(spies.http).toHaveBeenCalledTimes(1);
expect(spies.service).toHaveBeenCalledTimes(1);
});

test('support forwardRef in static parameters getter', () => {
const spies = {
http: jest.fn(),
service: jest.fn(),
};

class Http {
constructor() {
spies.http();
}
}

class Service {
static get parameters() {
return [forwardRef(() => Http)];
}

constructor(http: any) {
spies.service(http);
}
}

const providers = ReflectiveInjector.resolve([Http, Service]);
const injector = ReflectiveInjector.createFromResolved({
name: 'root',
providers,
});

expect(injector.get(Service)).toBeInstanceOf(Service);
expect(spies.http).toHaveBeenCalledTimes(1);
expect(spies.service).toHaveBeenCalledTimes(1);
});

test('support options getter', () => {
const spies = {
service: jest.fn(),
};

class Service {
context!: ExecutionContext;

static get options() {
return {
scope: Scope.Operation,
executionContextIn: ['context'],
global: true,
};
}

constructor() {
spies.service();
}
}

const providers = ReflectiveInjector.resolve([Service]);
const injector = ReflectiveInjector.createFromResolved({
name: 'root',
providers,
});

expect(injector.get(Service)).toBeInstanceOf(Service);
expect(spies.service).toHaveBeenCalledTimes(1);

expect(providers[0].factory.isGlobal).toBe(true);
expect(providers[0].factory.executionContextIn).toContainEqual('context');
});

test('support destroy hook', () => {
const spies = {
service: jest.fn(),
};

class Service implements OnDestroy {
constructor() {
spies.service();
}

onDestroy() {}
}

const providers = ReflectiveInjector.resolve([Service]);
const injector = ReflectiveInjector.createFromResolved({
name: 'root',
providers,
});

expect(injector.get(Service)).toBeInstanceOf(Service);

expect(providers[0].factory.hasOnDestroyHook).toBe(true);
});