Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 2 additions & 18 deletions src/ObjectParser.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -48,22 +46,8 @@ export default class ObjectParser {
}

if (typeOf === 'function') {
return this.getFieldConfigFromFunction(value);
return value;
}

return 'JSON';
}

static getFieldConfigFromFunction(value: () => any): ComposeFieldConfig<any, any> {
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`.'
);
}
}
4 changes: 2 additions & 2 deletions src/__fixtures__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
55 changes: 5 additions & 50 deletions src/__tests__/ObjectParser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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 });
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/__snapshots__/ObjectParser-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Object {
"type": "String",
},
"created": Object {
"_fieldAsThunk": [Function],
"args": Array [],
"isDeprecated": false,
"name": "created",
Expand Down Expand Up @@ -47,6 +48,7 @@ Object {
"type": "String",
},
"height": Object {
"_fieldAsThunk": [Function],
"args": Array [],
"isDeprecated": false,
"name": "height",
Expand All @@ -60,6 +62,7 @@ Object {
"type": "PeopleType_Homeworld",
},
"mass": Object {
"_fieldAsThunk": [Function],
"args": Array [],
"isDeprecated": false,
"name": "mass",
Expand Down Expand Up @@ -97,6 +100,7 @@ Object {
"type": "String",
},
"population": Object {
"_fieldAsThunk": [Function],
"args": Array [],
"isDeprecated": false,
"name": "population",
Expand Down