Skip to content

Commit

Permalink
fix(TypeComposer): hasInterface() now checks interface presence by name
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Feb 7, 2019
1 parent d7366e0 commit caa39e7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/TypeComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export class TypeComposer<TSource = any, TContext = any> {
): this;

public hasInterface(
interfaceObj: InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType,
iface: string | InterfaceTypeComposer<any, TContext> | GraphQLInterfaceType,
): boolean;

public addInterface(
Expand Down
7 changes: 5 additions & 2 deletions src/TypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { resolveOutputConfigMapAsThunk, resolveOutputConfigAsThunk } from './uti
import { defineFieldMap, defineFieldMapToConfig } from './utils/configToDefine';
import { toInputObjectType } from './utils/toInputObjectType';
import { typeByPath } from './utils/typeByPath';
import { getComposeTypeName } from './utils/typeHelpers';
// import { deprecate } from './utils/debug';
import type { ProjectionType } from './utils/projection';
import type { ObjMap, Thunk } from './utils/definitions';
Expand Down Expand Up @@ -819,8 +820,10 @@ export class TypeComposer<TContext> {
return this;
}

hasInterface(interfaceObj: GraphQLInterfaceType | InterfaceTypeComposer<TContext>): boolean {
return this.getInterfaces().indexOf(interfaceObj) > -1;
hasInterface(iface: string | GraphQLInterfaceType | InterfaceTypeComposer<TContext>): boolean {
const nameAsString = getComposeTypeName(iface);
const ifaces = this.getInterfaces();
return !!ifaces.find(i => getComposeTypeName(i) === nameAsString);
}

addInterface(
Expand Down
4 changes: 3 additions & 1 deletion src/UnionTypeComposer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export class UnionTypeComposer<TSource = any, TContext = any> {
// Union Types methods
// -----------------------------------------------

public hasType(name: string): boolean;
public hasType(
name: string | GraphQLObjectType | TypeComposer<TContext>,
): boolean;

public getTypes(): ComposeTypesArray;

Expand Down
3 changes: 1 addition & 2 deletions src/UnionTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { TypeAsString, ComposeObjectType } from './TypeMapper';
import type { SchemaComposer } from './SchemaComposer';
import type { Thunk } from './utils/definitions';
import { resolveTypeArrayAsThunk } from './utils/configAsThunk';
// import { typeByPath } from './utils/typeByPath';
import { getGraphQLType, getComposeTypeName } from './utils/typeHelpers';
import { graphqlVersion } from './utils/graphqlVersion';

Expand Down Expand Up @@ -126,7 +125,7 @@ export class UnionTypeComposer<TContext> {
// Union Types methods
// -----------------------------------------------

hasType(name: mixed): boolean {
hasType(name: string | GraphQLObjectType | TypeComposer<TContext>): boolean {
const nameAsString = getComposeTypeName(name);
return this.getTypeNames().includes(nameAsString);
}
Expand Down
23 changes: 20 additions & 3 deletions src/__tests__/TypeComposer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,23 @@ describe('TypeComposer', () => {
expect(tc.hasInterface(iface)).toBe(true);
});

it('hasInterface() should work by name or ITC', () => {
const MyIface = new GraphQLInterfaceType({
name: 'MyIface',
description: '',
fields: () => ({ id: { type: GraphQLInt } }),
resolveType: () => {},
});
tc.addInterface(MyIface);
expect(tc.hasInterface('MyIface123')).toBeFalsy();
expect(tc.hasInterface('MyIface')).toBeTruthy();
expect(tc.hasInterface(MyIface)).toBeTruthy();
expect(tc.hasInterface(InterfaceTypeComposer.create(MyIface))).toBeTruthy();

tc.addInterface(InterfaceTypeComposer.create('MyIface123'));
expect(tc.hasInterface('MyIface123')).toBeTruthy();
});

it('addInterface()', () => {
tc.addInterface(iface);
expect(tc.getInterfaces()).toEqual(expect.arrayContaining([iface]));
Expand Down Expand Up @@ -831,9 +848,9 @@ describe('TypeComposer', () => {

expect(tc.addRelation('user', ({}: any))).toBe(tc);

expect(tc.setInterfaces(([1, 2]: any))).toBe(tc);
expect(tc.addInterface((1: any))).toBe(tc);
expect(tc.removeInterface((2: any))).toBe(tc);
expect(tc.setInterfaces((['A', 'B']: any))).toBe(tc);
expect(tc.addInterface(('A': any))).toBe(tc);
expect(tc.removeInterface(('A': any))).toBe(tc);

expect(tc.setResolver('myResolver', new Resolver({ name: 'myResolver' }))).toBe(tc);
expect(tc.addResolver(new Resolver({ name: 'myResolver' }))).toBe(tc);
Expand Down

0 comments on commit caa39e7

Please sign in to comment.