Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { GraphQLNonNull } from 'graphql'; | ||
import { ListComposer } from './ListComposer'; | ||
import { AnyTypeComposer, NamedTypeComposer } from './utils/typeHelpers'; | ||
|
||
export class NonNullComposer<T extends AnyTypeComposer<any>> { | ||
public ofType: T; | ||
|
||
constructor(type: T); | ||
|
||
public getType(): GraphQLNonNull<any>; | ||
|
||
public getTypeName(): string; | ||
|
||
public getUnwrappedTC(): NamedTypeComposer<any>; | ||
|
||
public getTypePlural(): ListComposer<NonNullComposer<T>>; | ||
|
||
public getTypeNonNull(): NonNullComposer<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* @flow strict */ | ||
|
||
import invariant from 'graphql/jsutils/invariant'; | ||
import { GraphQLNonNull } from './graphql'; | ||
import { | ||
isNamedTypeComposer, | ||
type AnyTypeComposer, | ||
type NamedTypeComposer, | ||
} from './utils/typeHelpers'; | ||
import { ListComposer } from './ListComposer'; | ||
|
||
export class NonNullComposer<+T: AnyTypeComposer<any>> { | ||
+ofType: T; | ||
|
||
constructor(type: T): NonNullComposer<T> { | ||
invariant( | ||
!(type instanceof NonNullComposer), | ||
'You provide NonNull value to NonNullCOmposer constructor. Nesting NonNull is not allowed.' | ||
); | ||
this.ofType = type; | ||
|
||
// alive proper Flow type casting in autosuggestions for class with Generics | ||
/* :: return this; */ | ||
} | ||
|
||
getType(): GraphQLNonNull<any> { | ||
return new GraphQLNonNull(this.ofType.getType()); | ||
} | ||
|
||
getTypeName(): string { | ||
return `${this.ofType.getTypeName()}!`; | ||
} | ||
|
||
getUnwrappedTC(): NamedTypeComposer<any> { | ||
let tc = this; | ||
while (!isNamedTypeComposer(tc)) { | ||
tc = tc.ofType; | ||
} | ||
return tc; | ||
} | ||
|
||
getTypePlural(): ListComposer<NonNullComposer<T>> { | ||
return new ListComposer(this); | ||
} | ||
|
||
getTypeNonNull(): NonNullComposer<T> { | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* @flow strict */ | ||
|
||
import { schemaComposer } from '..'; | ||
import { GraphQLNonNull } from '../graphql'; | ||
import { NonNullComposer } from '../NonNullComposer'; | ||
import { ListComposer } from '../ListComposer'; | ||
import { ObjectTypeComposer } from '../ObjectTypeComposer'; | ||
|
||
beforeEach(() => { | ||
schemaComposer.clear(); | ||
}); | ||
|
||
describe('NonNullComposer', () => { | ||
let tc: NonNullComposer<any>; | ||
beforeEach(() => { | ||
tc = new NonNullComposer( | ||
schemaComposer.createTC(` | ||
type User { | ||
name: String | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
it('no nesting NonNull', () => { | ||
expect(() => { | ||
// eslint-disable-next-line no-new | ||
new NonNullComposer((tc: any)); | ||
}).toThrow('Nesting NonNull is not allowed'); | ||
}); | ||
|
||
it('getType()', () => { | ||
const type: any = tc.getType(); | ||
expect(type).toBeInstanceOf(GraphQLNonNull); | ||
expect(type.ofType.name).toBe('User'); | ||
}); | ||
|
||
it('getTypeName()', () => { | ||
expect(tc.getTypeName()).toBe('User!'); | ||
}); | ||
|
||
it('getTypePlural() should return wrapped type with ListComposer', () => { | ||
const tc2 = tc.getTypePlural(); | ||
expect(tc2).toBeInstanceOf(ListComposer); | ||
expect(tc2.ofType).toBe(tc); | ||
expect(tc2.getTypeName()).toBe('[User!]'); | ||
}); | ||
|
||
it('getTypeNonNull() should return wrapped type with NonNullComposer', () => { | ||
expect(tc.getTypeNonNull()).toBeInstanceOf(NonNullComposer); | ||
// should return itself | ||
expect(tc.getTypeNonNull()).toBe(tc); | ||
}); | ||
|
||
it('getUnwrappedTC() should return NamedTypeComposer', () => { | ||
const UserTC1 = tc.getUnwrappedTC(); | ||
expect(UserTC1).toBeInstanceOf(ObjectTypeComposer); | ||
expect(UserTC1.getTypeName()).toBe('User'); | ||
|
||
// should unwrap deeply wrapped Types | ||
const tc2: any = schemaComposer.typeMapper.convertSDLWrappedTypeName('[User!]!'); | ||
const UserTC2 = tc2.getUnwrappedTC(); | ||
expect(UserTC2.getTypeName()).toBe('User'); | ||
expect(UserTC1).toBe(UserTC2); | ||
}); | ||
}); |