Skip to content

Commit

Permalink
feat(TypeComposer): add setIstypeOf() and getIstypeOf() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Feb 4, 2019
1 parent 7996567 commit be7c4c0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/TypeComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ export class TypeComposer<TSource = any, TContext = any> {
newTypeName: string,
): TypeComposer<TCloneSource, TContext>;

public getIsTypeOf(): GraphQLIsTypeOfFn<any, TContext> | null | void;

public setIsTypeOf(fn: GraphQLIsTypeOfFn<any, any> | null | void): this;

// -----------------------------------------------
// InputType methods
// -----------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions src/TypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,15 @@ export class TypeComposer<TContext> {
return cloned;
}

getIsTypeOf(): ?GraphQLIsTypeOfFn<any, TContext> {
return this.gqType.isTypeOf;
}

setIsTypeOf(fn: ?GraphQLIsTypeOfFn<any, any>): TypeComposer<TContext> {
this.gqType.isTypeOf = fn;
return this;
}

// -----------------------------------------------
// InputType methods
// -----------------------------------------------
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/TypeComposer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GraphQLFloat,
GraphQLBoolean,
GraphQLInterfaceType,
graphql,
} from '../graphql';
import { TypeComposer, Resolver, schemaComposer, InterfaceTypeComposer } from '..';
import { graphqlVersion } from '../utils/graphqlVersion';
Expand Down Expand Up @@ -892,4 +893,51 @@ describe('TypeComposer', () => {
}).toThrow('field should be ObjectType');
});
});

describe('check isTypeOf methods', () => {
it('check methods setIstypeOf() getIstypeOf()', () => {
const tc1 = schemaComposer.createTC('type A { f: Int }');
expect(tc1.getIsTypeOf()).toBeUndefined();
const isTypeOf = () => true;
tc1.setIsTypeOf(isTypeOf);
expect(tc1.getIsTypeOf()).toBe(isTypeOf);
});

it('integration test', async () => {
const tc1 = schemaComposer.createTC('type A { a: Int }');
tc1.setIsTypeOf(source => {
return source && source.kind === 'A';
});
const tc2 = schemaComposer.createTC('type B { b: Int }');
tc2.setIsTypeOf(source => {
return source && source.kind === 'B';
});
schemaComposer.createUnionTC('union MyUnion = A | B');
schemaComposer.Query.addFields({
check: {
type: '[MyUnion]',
resolve: () => [{ kind: 'A', a: 1 }, { kind: 'B', b: 2 }, { kind: 'C', c: 3 }],
},
});
const res = await graphql(
schemaComposer.buildSchema(),
`
query {
check {
__typename
... on A {
a
}
... on B {
b
}
}
}
`
);
expect(res.data).toEqual({
check: [{ __typename: 'A', a: 1 }, { __typename: 'B', b: 2 }, null],
});
});
});
});

0 comments on commit be7c4c0

Please sign in to comment.