Skip to content

Commit

Permalink
fix(Resolver): If for outputType provided another Resolver, will be g…
Browse files Browse the repository at this point in the history
…et its outputType
  • Loading branch information
nodkz committed Nov 3, 2016
1 parent 8974d86 commit 0b8deae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/__tests__/resolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Resolver', () => {

it('should convert type as string to GraphQLType in outputType', () => {
const myResolver = new Resolver({
name: 'customResolver',
name: 'myResolver',
outputType: 'String!',
});
expect(myResolver.outputType).instanceof(GraphQLNonNull);
Expand All @@ -71,7 +71,7 @@ describe('Resolver', () => {

it('should convert type definition to GraphQLType in outputType', () => {
const myResolver = new Resolver({
name: 'customResolver',
name: 'myResolver',
outputType: `
type SomeType {
name: String
Expand All @@ -82,6 +82,44 @@ describe('Resolver', () => {
expect(myResolver.outputType.name).equal('SomeType');
});

it('should accept TypeComposer for outputType', () => {
const typeTC = TypeComposer.create('type SomeType22 { test: String }');
const myResolver = new Resolver({
name: 'myResolver',
outputType: typeTC,
});
expect(myResolver.outputType).instanceof(GraphQLObjectType);
expect(myResolver.outputType.name).equal('SomeType22');
});

it('should throw error on InputTypeComposer for outputType', () => {
const someInputITC = InputTypeComposer.create('input SomeInputType { add: String }');
expect(() => {
new Resolver({
name: 'myResolver',
outputType: someInputITC,
});
}).to.throw('InputTypeComposer');
});

it('should accept Resolver for outputType', () => {
const someOtherResolver = new Resolver({
name: 'someOtherResolver',
outputType: `
type SomeType {
name: String
}
`,
});

const myResolver = new Resolver({
name: 'myResolver',
outputType: someOtherResolver,
});
expect(myResolver.outputType).instanceof(GraphQLObjectType);
expect(myResolver.outputType.name).equal('SomeType');
});

it('should have wrapOutputType() method', () => {
const newResolver = resolver.wrapOutputType((prevOutputType) => { // eslint-disable-line
return 'String';
Expand Down
9 changes: 9 additions & 0 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ export default class Resolver {
return;
}

if (gqType instanceof Resolver) {
this.outputType = gqType.getOutputType();
return;
}

if (gqType instanceof InputTypeComposer) {
throw new Error('You provide InputTypeComposer as OutputType for Resolver.outputType. It may by ScalarType or OutputObjectType.');
}

if (isString(gqType)) {
// $FlowFixMe
if (gqType.indexOf('{') === -1) {
Expand Down

0 comments on commit 0b8deae

Please sign in to comment.