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 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/__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 }); diff --git a/src/__tests__/ObjectParser-test.js b/src/__tests__/ObjectParser-test.js index 111d7a5..ef23474 100644 --- a/src/__tests__/ObjectParser-test.js +++ b/src/__tests__/ObjectParser-test.js @@ -43,14 +43,13 @@ describe('ObjectParser', () => { }); }); - it('process function', () => { - const spy = jest.spyOn(OP, 'getFieldConfigFromFunction'); - const valueAsFn = () => 'String'; - OP.getFieldConfig(valueAsFn); - expect(spy).toHaveBeenCalledWith(valueAsFn); + it('function', () => { + const valueAsFn = () => 'abracadabra'; + const res = OP.getFieldConfig(valueAsFn); + expect(res).toBe(valueAsFn); }); - it('process object', () => { + it('object', () => { const spy = jest.spyOn(OP, 'createTC'); const valueAsObj = { a: 123 }; OP.getFieldConfig(valueAsObj, { @@ -61,50 +60,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", 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