Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Urigo/graphql-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Mar 14, 2019
2 parents abb1680 + 501ce4e commit 93135db
Show file tree
Hide file tree
Showing 12 changed files with 766 additions and 542 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
ignore:
- gh-pages
docker:
- image: circleci/node:10.15.1-browsers-legacy
- image: circleci/node:10.15.3-browsers-legacy
steps:
- checkout
- restore_cache:
Expand Down
47 changes: 10 additions & 37 deletions docs/introduction/test-your-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const UserModule = new GraphQLModule({
}
type Query {
me: User
userById(id: String!): User
}
`,
Expand All @@ -40,57 +39,31 @@ export const UserModule = new GraphQLModule({
username: user => user.username,
},
Query: {
me: (root, args, { currentUser }) => currentUser,
userById: (root, { id }, injector) =>
injector.get(UsersProvider).getUserById(id),
},
},
});
```

You can mock context and providers by importing the existing module together with resolvers into a new testing module like below;
You can mock providers by overwriting the existing provider definitions;

tests/user.module.spec.ts
```typescript
import { UserModule } from '../modules/user/user.module';
import { execute } from 'graphql';
describe('UserModule', async () => {
it('FieldResolver of Query: me', async () => {
const { schema, context } = new GraphQLModule({
imports: [UserModule],
resolvers: UserModule.resolvers,
context: { currentUser: { id: 'ID', username: 'USERNAME' } }
});
const result = await execute({
schema,
document: gql`
query {
me {
id
username
}
}
`,
contextValue: await context({}),
});
expect(result.errors).toBeFalsy();
expect(result.data['me']['id']).toBe('ID');
expect(result.data['me']['username']).toBe('USERNAME');
});
it('FieldResolver of Query: userById', async () => {
const { schema, context } = new GraphQLModule({
imports: [UserModule],
resolvers: UserModule.resolvers,
providers: [
{
provide: UserProvider,
overwrite: true,
useValue: {
userById: (id: string) => { id, username: 'NAME' }
}
}
]
const { schema, context, injector } = UserModule;

injector.provide({
provide: UserProvider,
overwrite: true,
useValue: {
userById: (id: string) => { id, username: 'USERNAME' }
}
});

const result = await execute({
schema,
document: gql`
Expand Down
1 change: 1 addition & 0 deletions docs/introduction/your-first-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Here is a quick example:
`modules/my-first-module/index.ts`
```typescript
import { GraphQLModule } from '@graphql-modules/core';
import gql from "graphql-tag";

export const MyFirstModule = new GraphQLModule({
typeDefs: gql`
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"@types/graphql": "14.0.7",
"@types/jest": "24.0.1",
"graphql": "14.1.1",
"jest": "24.1.0",
"jest": "24.5.0",
"lerna": "2.11.0",
"reflect-metadata": "0.1.13",
"replace-in-file": "3.4.3",
"replace-in-file": "3.4.4",
"rimraf": "2.6.3",
"ts-jest": "24.0.0",
"typedoc": "0.13.0",
Expand Down
14 changes: 7 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"clean": "rm -rf dist",
"prebuild": "yarn clean",
"build": "tsc",
"build": "tsc -m esnext --outDir dist/esnext && tsc -m commonjs --outDir dist/commonjs",
"test": "jest",
"lint": "tslint -c ../../tslint.json 'src/**/*.ts' --format stylish"
},
Expand All @@ -33,7 +33,7 @@
"dependencies": {
"esm":"3.2.16",
"@graphql-modules/di": "0.4.2",
"graphql-toolkit": "0.0.5",
"graphql-toolkit": "0.2.6",
"graphql-tools": "4.0.4",
"tslib": "1.9.3"
},
Expand All @@ -42,15 +42,15 @@
"apollo-client": "2.4.12",
"apollo-link-schema": "1.1.6",
"graphql-tag": "2.10.1",
"jest": "24.1.0",
"jest": "24.5.0",
"tslint": "5.12.1",
"typescript": "3.3.3"
},
"main": "cjs/index.js",
"module": "dist/index.js",
"typings": "./dist/index.d.ts",
"main": "dist/commonjs/index.js",
"module": "dist/esnext/index.js",
"typings": "dist/esnext/index.d.ts",
"typescript": {
"definition": "./dist/index.d.ts"
"definition": "dist/esnext/index.d.ts"
},
"publishConfig": {
"access": "public"
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/graphql-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IResolvers, SchemaDirectiveVisitor, mergeSchemas, IDirectiveResolvers, makeExecutableSchema, IResolverValidationOptions } from 'graphql-tools';
import { mergeGraphQLSchemas, mergeResolvers, IResolversComposerMapping, composeResolvers , getSchemaDirectiveFromDirectiveResolver } from 'graphql-toolkit';
import { mergeResolvers, IResolversComposerMapping, composeResolvers , getSchemaDirectiveFromDirectiveResolver, mergeTypeDefs } from 'graphql-toolkit';
import { Provider, Injector, ProviderScope, ServiceIdentifier } from '@graphql-modules/di';
import { DocumentNode, GraphQLSchema, parse, GraphQLScalarType } from 'graphql';
import { SchemaNotValidError, DependencyModuleUndefinedError, TypeDefNotFoundError, ModuleConfigRequiredError, IllegalResolverInvocationError, ContextBuilderError } from './errors';
Expand Down Expand Up @@ -405,7 +405,7 @@ export class GraphQLModule<Config = any, Session = any, Context = any> {
}
typeDefsArr = typeDefsArr.concat(this.extraSchemas);
if (typeDefsArr.length) {
this._cache.typeDefs = mergeGraphQLSchemas(typeDefsArr, {
this._cache.typeDefs = mergeTypeDefs(typeDefsArr, {
useSchemaDefinition: false,
});
} else {
Expand Down Expand Up @@ -568,7 +568,7 @@ export class GraphQLModule<Config = any, Session = any, Context = any> {
if (typeof typeDefsDefinitions === 'string') {
typeDefs = parse(typeDefsDefinitions);
} else if (Array.isArray(typeDefsDefinitions)) {
typeDefs = mergeGraphQLSchemas(typeDefsDefinitions, {
typeDefs = mergeTypeDefs(typeDefsDefinitions, {
useSchemaDefinition: false,
});
} else if (typeDefsDefinitions) {
Expand Down
12 changes: 6 additions & 6 deletions packages/di/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"clean": "rm -rf dist",
"prebuild": "yarn clean",
"build": "tsc",
"build": "tsc -m esnext --outDir dist/esnext && tsc -m commonjs --outDir dist/commonjs",
"test": "jest",
"lint": "tslint -c ../../tslint.json 'src/**/*.ts' --format stylish"
},
Expand Down Expand Up @@ -38,19 +38,19 @@
"reflect-metadata": "^0.1.12"
},
"devDependencies": {
"jest": "24.1.0",
"jest": "24.5.0",
"reflect-metadata": "0.1.13",
"tslint": "5.12.1",
"typescript": "3.3.3"
},
"dependencies": {
"tslib": "1.9.3"
},
"main": "cjs/index.js",
"module": "dist/index.js",
"typings": "./dist/index.d.ts",
"main": "dist/commonjs/index.js",
"module": "dist/esnext/index.js",
"typings": "dist/esnext/index.d.ts",
"typescript": {
"definition": "./dist/index.d.ts"
"definition": "dist/esnext/index.d.ts"
},
"publishConfig": {
"access": "public"
Expand Down
24 changes: 21 additions & 3 deletions packages/di/src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Injector {

if (isTypeProvider(provider)) {
const options: ProviderOptions = Reflect.getMetadata(PROVIDER_OPTIONS, provider);
if (options && !options.overwrite && this.has(provider)) {
if (this.has(provider)) {
throw new ProviderAlreadyDefinedError(this._name, provider);
}
this._classMap.set(provider, provider);
Expand All @@ -51,8 +51,26 @@ export class Injector {
return;
}

if (!provider.overwrite && this.has(provider.provide)) {
throw new ProviderAlreadyDefinedError(this._name, provider.provide);
if (this.has(provider.provide)) {
if (!provider.overwrite) {
throw new ProviderAlreadyDefinedError(this._name, provider.provide);
} else {
this._classMap.delete(provider.provide);
this._factoryMap.delete(provider.provide);
this._instanceMap.delete(provider.provide);
this._applicationScopeServiceIdentifiers.splice(
this._applicationScopeServiceIdentifiers.indexOf(provider.provide),
1,
);
this._sessionScopeServiceIdentifiers.splice(
this._applicationScopeServiceIdentifiers.indexOf(provider.provide),
1,
);
this._requestScopeServiceIdentifiers.splice(
this._applicationScopeServiceIdentifiers.indexOf(provider.provide),
1,
);
}
}

if (isValueProvider(provider)) {
Expand Down
25 changes: 24 additions & 1 deletion packages/di/tests/di.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import 'reflect-metadata';
import { Injectable, Injector } from '../src';

describe('Dependency Injection', async () => {
it('should do something', async () => {});
it('clear instances if provider is overwritten', async () => {
@Injectable()
class FooProvider {
foo() {
return 'FOO';
}
}
const injector = new Injector();
injector.provide(FooProvider);
expect(injector.get(FooProvider).foo()).toBe('FOO');
injector.provide({
provide: FooProvider,
overwrite: true,
useValue: {
foo() {
return 'BAR';
},
},
});
expect(injector.get(FooProvider).foo()).toBe('BAR');
});
});
14 changes: 14 additions & 0 deletions packages/di/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"module": "commonjs",
"target": "es2018",
"lib": [
"es2018",
"esnext.asynciterable"
],
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"outDir": "../dist/tests",
}
}
12 changes: 6 additions & 6 deletions packages/graphql-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"clean": "rm -rf dist",
"prebuild": "yarn clean",
"build": "tsc",
"build": "tsc -m esnext --outDir dist/esnext && tsc -m commonjs --outDir dist/commonjs",
"test": "jest",
"lint": "tslint -c ../../tslint.json 'src/**/*.ts' --format stylish"
},
Expand Down Expand Up @@ -35,19 +35,19 @@
]
},
"devDependencies": {
"jest": "24.1.0",
"jest": "24.5.0",
"tslint": "5.12.1",
"typescript": "3.3.3"
},
"dependencies": {
"@graphql-modules/core": "0.4.2",
"tslib": "1.9.3"
},
"main": "cjs/index.js",
"module": "dist/index.js",
"typings": "./dist/index.d.ts",
"main": "dist/commonjs/index.js",
"module": "dist/esnext/index.js",
"typings": "dist/esnext/index.d.ts",
"typescript": {
"definition": "./dist/index.d.ts"
"definition": "dist/esnext/index.d.ts"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit 93135db

Please sign in to comment.