Skip to content

Commit

Permalink
Merge pull request #166 from Urigo/bug/interface-injector
Browse files Browse the repository at this point in the history
WIP: __resolveType not getting `injector`
  • Loading branch information
dotansimha committed Nov 6, 2018
2 parents 18c2cd3 + ab8e7ee commit ef146af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 53 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/graphql-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,15 @@ export class GraphQLModule<Config = any, Request = any, Context = any> {
for (const prop in resolvers[type]) {
const resolver = typeResolvers[prop];
if (typeof resolver === 'function') {
typeResolvers[prop] = (root: any, args: any, context: any) => {
return resolver.call(typeResolvers, root, args, { injector, ...context });
};
if (prop !== '__resolveType') {
typeResolvers[prop] = (root: any, args: any, context: any, info: any) => {
return resolver.call(typeResolvers, root, args, { injector, ...context }, info);
};
} else {
typeResolvers[prop] = (root: any, context: any, info: any) => {
return resolver.call(typeResolvers, root, { injector, ...context }, info);
};
}
}
}
}
Expand Down
71 changes: 21 additions & 50 deletions packages/core/tests/graphql-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,79 +510,50 @@ describe('GraphQLModule', () => {
expect(contextValue.counter).toBe(0);
});

it.skip('should call resolvers composition in correct order with correct context - test nested types', async () => {
// We have 2 wrappers here: addUser - adds user to the context. checkRoles - depend on the user and check that he has roles.
// We wraps `Query.foo` with `addUser` and `checkRoles` and it works.
// Then, the `MyType` type has a field, and this field is wrapped only with `checkRoles`. Is should work because `MyType.f` resolver
// MUST get called after `Query.foo` resolver.

const addUser = next => (root, args, context, info) => {
context.user = {
id: 1,
name: 'Dotan',
roles: ['A', 'B'],
};

return next(root, args, context, info);
};

let hasUserInQueryRoot = false;
let hasUserInTypeField = false;

const checkRoles = next => (root, args, context, info) => {
// This should be true. When it's called with `addUser` before, it works.
// When we call `checkRoles` without `addUser` before - it's missing.

if (info.fieldName === 'foo') {
console.log(`Query.foo context.user value = `, context.user);
hasUserInQueryRoot = !!context.user;
} else if (info.fieldName === 'f') {
console.log(`MyType.f context.user value = `, context.user);
hasUserInTypeField = !!context.user;
}

if (context.user.roles.length > 0) {
return next(root, args, context, info);
}

return null;
};
it('should inject context correctly into `__resolveType`', async () => {
let hasInjector = false;

const { schema, context } = new GraphQLModule({
typeDefs: `
type Query {
foo: MyType
something: MyBase
}
type MyType {
f: String
interface MyBase {
id: String
}
type MyType implements MyBase {
id: String
}
`,
resolvers: {
Query: {
foo: (root, args, context, info) => {
return { someValue: context.user.id };
something: () => {
return { someValue: 1 };
},
},
MyBase: {
__resolveType: (obj, context) => {
hasInjector = !!context.injector;

return 'MyType';
},
},
MyType: {
f: v => v.someValue,
id: o => o.someValue,
},
},
resolversComposition: {
'Query.foo': [addUser, checkRoles],
'MyType.f': [checkRoles],
},
});
const contextValue = await context({ req: {} });

await execute({
schema,
document: gql`query { foo { f } }`,
document: gql`query { something { id } }`,
contextValue,
});

expect(hasUserInQueryRoot).toBeTruthy();
expect(hasUserInTypeField).toBeTruthy();
expect(hasInjector).toBeTruthy();
});
});
});

0 comments on commit ef146af

Please sign in to comment.