Skip to content

Commit 3387986

Browse files
borodayevnodkz
authored andcommitted
fix: solve hoisting problem by removing getFieldConfigFromFunction
1 parent b49c1a2 commit 3387986

File tree

3 files changed

+6
-69
lines changed

3 files changed

+6
-69
lines changed

src/ObjectParser.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* @flow */
22

3-
import { graphql, TypeComposer, upperFirst, type ComposeFieldConfig } from 'graphql-compose';
4-
5-
const { isOutputType } = graphql;
3+
import { TypeComposer, upperFirst, type ComposeFieldConfig } from 'graphql-compose';
64

75
type GetValueOpts = {
86
typeName: string,
@@ -48,22 +46,8 @@ export default class ObjectParser {
4846
}
4947

5048
if (typeOf === 'function') {
51-
return this.getFieldConfigFromFunction(value);
49+
return value;
5250
}
53-
5451
return 'JSON';
5552
}
56-
57-
static getFieldConfigFromFunction(value: () => any): ComposeFieldConfig<any, any> {
58-
const fc = value();
59-
60-
if (typeof fc === 'string') return fc;
61-
if (isOutputType(fc)) return fc;
62-
if (fc instanceof TypeComposer) return fc;
63-
if (fc && typeof fc === 'object' && fc.type) return fc;
64-
65-
throw new Error(
66-
'Your type function should return: `string`, `GraphQLOutputType`, `TypeComposer`, `FieldConfig`.'
67-
);
68-
}
6953
}

src/__tests__/ObjectParser-test.js

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ describe('ObjectParser', () => {
4343
});
4444
});
4545

46-
it('process function', () => {
47-
const spy = jest.spyOn(OP, 'getFieldConfigFromFunction');
48-
const valueAsFn = () => 'String';
49-
OP.getFieldConfig(valueAsFn);
50-
expect(spy).toHaveBeenCalledWith(valueAsFn);
51-
});
52-
5346
it('process object', () => {
5447
const spy = jest.spyOn(OP, 'createTC');
5548
const valueAsObj = { a: 123 };
@@ -61,50 +54,6 @@ describe('ObjectParser', () => {
6154
});
6255
});
6356

64-
describe('getFieldConfigFromFunction()', () => {
65-
it('accept type as string', () => {
66-
const fn = () => 'Int';
67-
expect(OP.getFieldConfigFromFunction(fn)).toEqual('Int');
68-
});
69-
70-
it('accept GraphQLOutputType', () => {
71-
const fn = () => graphql.GraphQLBoolean;
72-
expect(OP.getFieldConfigFromFunction(fn)).toEqual(graphql.GraphQLBoolean);
73-
74-
const fn2 = () =>
75-
new graphql.GraphQLObjectType({
76-
name: 'MyType',
77-
fields: () => ({
78-
field1: { type: graphql.GraphQLFloat },
79-
}),
80-
});
81-
expect(OP.getFieldConfigFromFunction(fn2)).toBeInstanceOf(graphql.GraphQLObjectType);
82-
});
83-
84-
it('accept TypeComposer', () => {
85-
const fn = () =>
86-
TypeComposer.create(`
87-
type MyOtherType {
88-
f1: Int!
89-
}
90-
`);
91-
expect(OP.getFieldConfigFromFunction(fn)).toBeInstanceOf(TypeComposer);
92-
});
93-
94-
it('accept FieldConfig', () => {
95-
const fn = () => ({
96-
type: 'String',
97-
args: { a1: 'Int' },
98-
resolve: 123,
99-
});
100-
expect(OP.getFieldConfigFromFunction(fn)).toEqual({
101-
type: 'String',
102-
args: { a1: 'Int' },
103-
resolve: 123,
104-
});
105-
});
106-
});
107-
10857
describe('createTC()', () => {
10958
it('return TypeComposer', () => {
11059
const tc = OP.createTC('MyType', { a: 1 });

src/__tests__/__snapshots__/ObjectParser-test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Object {
1111
"type": "String",
1212
},
1313
"created": Object {
14+
"_fieldAsThunk": [Function],
1415
"args": Array [],
1516
"isDeprecated": false,
1617
"name": "created",
@@ -47,6 +48,7 @@ Object {
4748
"type": "String",
4849
},
4950
"height": Object {
51+
"_fieldAsThunk": [Function],
5052
"args": Array [],
5153
"isDeprecated": false,
5254
"name": "height",
@@ -60,6 +62,7 @@ Object {
6062
"type": "PeopleType_Homeworld",
6163
},
6264
"mass": Object {
65+
"_fieldAsThunk": [Function],
6366
"args": Array [],
6467
"isDeprecated": false,
6568
"name": "mass",
@@ -97,6 +100,7 @@ Object {
97100
"type": "String",
98101
},
99102
"population": Object {
103+
"_fieldAsThunk": [Function],
100104
"args": Array [],
101105
"isDeprecated": false,
102106
"name": "population",

0 commit comments

Comments
 (0)