From 01661ba0506682761a1bbc0bebde806b46292d43 Mon Sep 17 00:00:00 2001 From: frankast Date: Tue, 7 Nov 2017 19:01:06 +0600 Subject: [PATCH 1/5] docs: rename PersonTC to StarshipTC --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ac7b67..3c3361b 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Moreover, `graphql-compose` allows you to pass pre-defined resolvers of other ty const restApiResponse = { name: 'Anakin Skywalker', starships: () => - PeopleTC.getResolver('findByUrlList') // get some standard resolver + StarshipTC.getResolver('findByUrlList') // get some standard resolver .wrapResolve(next => rp => { // wrap with additional logic const starshipsUrls = rp.source.starships; rp.args.urls = starshipsUrls; // populate `urls` arg from source From 60458a6e42c0563df46a4add20b2ecb663713802 Mon Sep 17 00:00:00 2001 From: frankast Date: Tue, 7 Nov 2017 19:30:00 +0600 Subject: [PATCH 2/5] refactor: rename composeWithRest-test to composeWithJson-test --- .../{composeWithRest-test.js => composeWithJson-test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/__tests__/{composeWithRest-test.js => composeWithJson-test.js} (100%) diff --git a/src/__tests__/composeWithRest-test.js b/src/__tests__/composeWithJson-test.js similarity index 100% rename from src/__tests__/composeWithRest-test.js rename to src/__tests__/composeWithJson-test.js From f5157e29d42e47a763cc8b32b0a715cc744131dc Mon Sep 17 00:00:00 2001 From: frankast Date: Tue, 7 Nov 2017 19:31:35 +0600 Subject: [PATCH 3/5] refactor: disable eslint for console.log --- src/__fixtures__/app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/__fixtures__/app.js b/src/__fixtures__/app.js index ee20426..c5acbd2 100644 --- a/src/__fixtures__/app.js +++ b/src/__fixtures__/app.js @@ -17,6 +17,6 @@ app.use( ); app.listen(PORT, () => { - console.log(`App running on port ${PORT}`); - console.log(`Open http://localhost:${PORT}/graphql`); + console.log(`App running on port ${PORT}`); //eslint-disable-line + console.log(`Open http://localhost:${PORT}/graphql`); //eslint-disable-line }); From 97ac6f1b1484836a63dbbdaa54a548f09865f48e Mon Sep 17 00:00:00 2001 From: frankast Date: Tue, 7 Nov 2017 19:34:10 +0600 Subject: [PATCH 4/5] fix: solve hoisting problem by removing getFieldConfigFromFunction --- src/ObjectParser.js | 20 +------- src/__tests__/ObjectParser-test.js | 51 ------------------- .../__snapshots__/ObjectParser-test.js.snap | 4 ++ 3 files changed, 6 insertions(+), 69 deletions(-) diff --git a/src/ObjectParser.js b/src/ObjectParser.js index f450427..06e6d70 100644 --- a/src/ObjectParser.js +++ b/src/ObjectParser.js @@ -1,8 +1,6 @@ /* @flow */ -import { graphql, TypeComposer, upperFirst, type ComposeFieldConfig } from 'graphql-compose'; - -const { isOutputType } = graphql; +import { TypeComposer, upperFirst, type ComposeFieldConfig } from 'graphql-compose'; type GetValueOpts = { typeName: string, @@ -48,22 +46,8 @@ export default class ObjectParser { } if (typeOf === 'function') { - return this.getFieldConfigFromFunction(value); + return value; } - return 'JSON'; } - - static getFieldConfigFromFunction(value: () => any): ComposeFieldConfig { - const fc = value(); - - if (typeof fc === 'string') return fc; - if (isOutputType(fc)) return fc; - if (fc instanceof TypeComposer) return fc; - if (fc && typeof fc === 'object' && fc.type) return fc; - - throw new Error( - 'Your type function should return: `string`, `GraphQLOutputType`, `TypeComposer`, `FieldConfig`.' - ); - } } diff --git a/src/__tests__/ObjectParser-test.js b/src/__tests__/ObjectParser-test.js index 111d7a5..5e5b9c7 100644 --- a/src/__tests__/ObjectParser-test.js +++ b/src/__tests__/ObjectParser-test.js @@ -43,13 +43,6 @@ describe('ObjectParser', () => { }); }); - it('process function', () => { - const spy = jest.spyOn(OP, 'getFieldConfigFromFunction'); - const valueAsFn = () => 'String'; - OP.getFieldConfig(valueAsFn); - expect(spy).toHaveBeenCalledWith(valueAsFn); - }); - it('process object', () => { const spy = jest.spyOn(OP, 'createTC'); const valueAsObj = { a: 123 }; @@ -61,50 +54,6 @@ describe('ObjectParser', () => { }); }); - describe('getFieldConfigFromFunction()', () => { - it('accept type as string', () => { - const fn = () => 'Int'; - expect(OP.getFieldConfigFromFunction(fn)).toEqual('Int'); - }); - - it('accept GraphQLOutputType', () => { - const fn = () => graphql.GraphQLBoolean; - expect(OP.getFieldConfigFromFunction(fn)).toEqual(graphql.GraphQLBoolean); - - const fn2 = () => - new graphql.GraphQLObjectType({ - name: 'MyType', - fields: () => ({ - field1: { type: graphql.GraphQLFloat }, - }), - }); - expect(OP.getFieldConfigFromFunction(fn2)).toBeInstanceOf(graphql.GraphQLObjectType); - }); - - it('accept TypeComposer', () => { - const fn = () => - TypeComposer.create(` - type MyOtherType { - f1: Int! - } - `); - expect(OP.getFieldConfigFromFunction(fn)).toBeInstanceOf(TypeComposer); - }); - - it('accept FieldConfig', () => { - const fn = () => ({ - type: 'String', - args: { a1: 'Int' }, - resolve: 123, - }); - expect(OP.getFieldConfigFromFunction(fn)).toEqual({ - type: 'String', - args: { a1: 'Int' }, - resolve: 123, - }); - }); - }); - describe('createTC()', () => { it('return TypeComposer', () => { const tc = OP.createTC('MyType', { a: 1 }); diff --git a/src/__tests__/__snapshots__/ObjectParser-test.js.snap b/src/__tests__/__snapshots__/ObjectParser-test.js.snap index 496aa36..97edfe1 100644 --- a/src/__tests__/__snapshots__/ObjectParser-test.js.snap +++ b/src/__tests__/__snapshots__/ObjectParser-test.js.snap @@ -11,6 +11,7 @@ Object { "type": "String", }, "created": Object { + "_fieldAsThunk": [Function], "args": Array [], "isDeprecated": false, "name": "created", @@ -47,6 +48,7 @@ Object { "type": "String", }, "height": Object { + "_fieldAsThunk": [Function], "args": Array [], "isDeprecated": false, "name": "height", @@ -60,6 +62,7 @@ Object { "type": "PeopleType_Homeworld", }, "mass": Object { + "_fieldAsThunk": [Function], "args": Array [], "isDeprecated": false, "name": "mass", @@ -97,6 +100,7 @@ Object { "type": "String", }, "population": Object { + "_fieldAsThunk": [Function], "args": Array [], "isDeprecated": false, "name": "population", From 38ea34f1a1be74206cb9b55859cb2e3dca19bd9b Mon Sep 17 00:00:00 2001 From: frankast Date: Tue, 7 Nov 2017 20:12:26 +0600 Subject: [PATCH 5/5] test: check if FC is a function --- src/__tests__/ObjectParser-test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/__tests__/ObjectParser-test.js b/src/__tests__/ObjectParser-test.js index 5e5b9c7..ef23474 100644 --- a/src/__tests__/ObjectParser-test.js +++ b/src/__tests__/ObjectParser-test.js @@ -43,7 +43,13 @@ describe('ObjectParser', () => { }); }); - it('process object', () => { + it('function', () => { + const valueAsFn = () => 'abracadabra'; + const res = OP.getFieldConfig(valueAsFn); + expect(res).toBe(valueAsFn); + }); + + it('object', () => { const spy = jest.spyOn(OP, 'createTC'); const valueAsObj = { a: 123 }; OP.getFieldConfig(valueAsObj, {