Skip to content

Commit b6363be

Browse files
committed
feat: expose toInputType() method which converts AnyComposeType to InputType. Scalars, Enums, Inputs will be returned without changes. Object, Interfaces – converted to Input. Unions – throw an Error. Type may be wrapped with modifiers NonNull, List.
1 parent ce19f46 commit b6363be

File tree

9 files changed

+242
-157
lines changed

9 files changed

+242
-157
lines changed

src/InterfaceTypeComposer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import type {
4141
DirectiveArgs,
4242
ExtensionsDirective,
4343
} from './utils/definitions';
44-
import { toInputObjectType } from './utils/toInputObjectType';
44+
import { toInputObjectType } from './utils/toInputType';
4545
import { typeByPath, type TypeInPath } from './utils/typeByPath';
4646
import {
4747
getComposeTypeName,

src/ObjectTypeComposer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
convertObjectFieldMapToConfig,
4141
convertInterfaceArrayAsThunk,
4242
} from './utils/configToDefine';
43-
import { toInputObjectType } from './utils/toInputObjectType';
43+
import { toInputObjectType } from './utils/toInputType';
4444
import { typeByPath, type TypeInPath } from './utils/typeByPath';
4545
import {
4646
getComposeTypeName,

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export { GraphQLDate, GraphQLBuffer, GraphQLJSON, GraphQLJSONObject } from './ty
3030

3131
// Utils
3232
export { getProjectionFromAST, getFlatProjectionFromAST } from './utils/projection';
33-
export { toInputObjectType, ConvertInputObjectFieldOpts } from './utils/toInputObjectType';
33+
export { toInputType, toInputObjectType, convertInputObjectField } from './utils/toInputType';
3434
export * from './utils/misc';
3535
export * from './utils/typeHelpers';
3636
export * from './utils/is';

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { SchemaComposer } from './SchemaComposer';
55

66
export { graphql };
77

8-
type TContext = any;
9-
10-
const schemaComposer = new SchemaComposer<TContext>();
8+
const schemaComposer = new SchemaComposer<any>();
119
const sc = schemaComposer;
1210
export {
1311
SchemaComposer, // SchemaComposer class
@@ -34,7 +32,7 @@ export { GraphQLDate, GraphQLBuffer, GraphQLJSON, GraphQLJSONObject } from './ty
3432

3533
// Utils
3634
export { getProjectionFromAST, getFlatProjectionFromAST } from './utils/projection';
37-
export { toInputObjectType, convertInputObjectField } from './utils/toInputObjectType';
35+
export { toInputType, toInputObjectType, convertInputObjectField } from './utils/toInputType';
3836
export * from './utils/misc';
3937
export * from './utils/typeHelpers';
4038
export * from './utils/is';

src/utils/__tests__/toInputObjectType-test.js renamed to src/utils/__tests__/toInputType-test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,55 @@ import { schemaComposer as sc } from '../..';
1111
import { ObjectTypeComposer } from '../../ObjectTypeComposer';
1212
import { InputTypeComposer } from '../../InputTypeComposer';
1313
import { InterfaceTypeComposer } from '../../InterfaceTypeComposer';
14-
import { toInputObjectType } from '../toInputObjectType';
14+
import { toInputObjectType, toInputType } from '../toInputType';
15+
16+
describe('toInputType()', () => {
17+
beforeEach(() => {
18+
sc.clear();
19+
});
20+
21+
it('should return Scalar, Enum, Input types unchanged', () => {
22+
const stc = sc.createScalarTC('MyScalar');
23+
const itc1 = toInputType(stc);
24+
expect(itc1).toBe(stc);
25+
26+
const etc = sc.createEnumTC('MyEnum');
27+
const itc2 = toInputType(etc);
28+
expect(itc2).toBe(etc);
29+
30+
const itc = sc.createInputTC('MyInput');
31+
const itc3 = toInputType(itc);
32+
expect(itc3).toBe(itc);
33+
});
34+
35+
it('should return wrapped Scalar, Enum Input types unchanged', () => {
36+
const stc = sc.createScalarTC('MyScalar').List.NonNull;
37+
const itc1 = toInputType(stc);
38+
expect(itc1.getTypeName()).toBe('[MyScalar]!');
39+
expect((itc1: any).ofType.ofType).toBe((stc: any).ofType.ofType);
40+
41+
const etc = sc.createEnumTC('MyEnum').NonNull;
42+
const itc2 = toInputType(etc);
43+
expect(itc2.getTypeName()).toBe('MyEnum!');
44+
expect((itc2: any).ofType).toBe((etc: any).ofType);
45+
46+
const itc = sc.createScalarTC('MyInput').NonNull.List.NonNull;
47+
const itc3 = toInputType(itc);
48+
expect(itc3.getTypeName()).toBe('[MyInput!]!');
49+
expect((itc3: any).ofType.ofType.ofType).toBe((itc: any).ofType.ofType.ofType);
50+
});
51+
52+
it('should convert wrapped Object type to new Input type', () => {
53+
const otc = sc.createObjectTC({
54+
name: 'MyObject',
55+
fields: { a: 'Int', b: 'Float!' },
56+
}).NonNull;
57+
const itc = toInputType(otc);
58+
expect(itc.getTypeName()).toBe('MyObjectInput!');
59+
expect((itc: any).ofType.getFieldTypeName('a')).toBe('Int');
60+
expect((itc: any).ofType.getFieldTypeName('b')).toBe('Float!');
61+
});
62+
});
1563

1664
describe('toInputObjectType()', () => {
1765
let PersonTC: ObjectTypeComposer<any, any>;

src/utils/toInputObjectType.d.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/utils/toInputObjectType.js

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/utils/toInputType.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { InputTypeComposer } from '../InputTypeComposer';
2+
import { ObjectTypeComposer } from '../ObjectTypeComposer';
3+
import { InterfaceTypeComposer } from '../InterfaceTypeComposer';
4+
import { SchemaComposer } from '../SchemaComposer';
5+
import { AnyTypeComposer, ComposeOutputType, ComposeInputType } from './typeHelpers';
6+
7+
export interface ToInputTypeOpts {
8+
/** If ObjectType or Interface received then will be used `${prefix}ObjectTypeName` as name for new Input type */
9+
prefix?: string;
10+
/** If ObjectType or Interface received then will be used `ObjectTypeName${suffix}` as name for new Input type */
11+
postfix?: string;
12+
/** When Union type is met then Error will be throw. This option helps to return provided fallbackType instead of Error. */
13+
fallbackType?: ComposeInputType;
14+
}
15+
16+
/**
17+
* Convert any TypeComposer to InputType.
18+
*
19+
* Type may be wrapped with modifiers NonNull, List.
20+
*
21+
* Scalars and Input types returned without changes.
22+
*
23+
* ObjectTypeComposer & InterfaceTypeComposer converted to InputTypeComposer
24+
* under name `{ObjectTypeName}Input`. Prefix & suffix can be overridden via opts.
25+
*
26+
* UnionTypeComposer throws Error, if opts?.fallbackType is not provided.
27+
*/
28+
export function toInputType(anyTC: AnyTypeComposer<any>, opts?: ToInputTypeOpts): ComposeInputType;
29+
30+
/**
31+
* Convert ObjectTypeComposer or InterfaceTypeComposer to InputTypeComposer.
32+
* Also will be converted all Object types which are used for fields.
33+
*/
34+
export function toInputObjectType<TContext>(
35+
tc: ObjectTypeComposer<any, TContext> | InterfaceTypeComposer<any, TContext>,
36+
opts?: ToInputTypeOpts
37+
): InputTypeComposer<TContext>;
38+
39+
export function convertInputObjectField(
40+
field: ComposeOutputType<any>,
41+
opts: ToInputTypeOpts
42+
): ComposeInputType | null;

0 commit comments

Comments
 (0)