Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TypeComposer, InputTypeComposer): Allow to set type as function …
…in field configs More details here #49
- Loading branch information
Showing
6 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect } from 'chai'; | ||
import { GraphQLString, GraphQLObjectType } from 'graphql'; | ||
import { unwrapFieldsType, wrapFieldsType } from '../typeAsFn'; | ||
|
||
|
||
describe('typeAsFn', () => { | ||
describe('unwrapFieldsType()', () => { | ||
it('should unwrap types from functions', () => { | ||
const fieldMap = { | ||
f1: { | ||
type: GraphQLString, | ||
}, | ||
f2: { | ||
type: new GraphQLObjectType({ | ||
name: 'MyType', | ||
fields: { | ||
f11: { type: GraphQLString }, | ||
}, | ||
}), | ||
}, | ||
f3: { | ||
type: () => GraphQLString, | ||
}, | ||
}; | ||
const unwrapped = unwrapFieldsType(fieldMap); | ||
expect(unwrapped.f1.type).to.equal(GraphQLString); | ||
expect(unwrapped.f2.type).to.instanceOf(GraphQLObjectType); | ||
expect(unwrapped.f3.type).to.equal(GraphQLString); | ||
expect(unwrapped.f3._typeFn).to.be.ok; | ||
expect(unwrapped.f3._typeFn()).to.equal(GraphQLString); | ||
}); | ||
}); | ||
|
||
describe('wrapFieldsType()', () => { | ||
it('should set _typeFn to type', () => { | ||
const unwrapped = { | ||
f1: { | ||
type: GraphQLString, | ||
}, | ||
f2: { | ||
type: new GraphQLObjectType({ | ||
name: 'MyType', | ||
fields: { | ||
f11: { type: GraphQLString }, | ||
}, | ||
}), | ||
}, | ||
f3: { | ||
type: GraphQLString, | ||
_typeFn: () => GraphQLString, | ||
}, | ||
}; | ||
const wrapped = wrapFieldsType(unwrapped); | ||
expect(wrapped.f1.type).to.equal(GraphQLString); | ||
expect(wrapped.f2.type).to.instanceOf(GraphQLObjectType); | ||
expect(wrapped.f3.type).to.be.ok; | ||
expect(wrapped.f3.type()).to.equal(GraphQLString); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* @flow */ | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import type { | ||
GraphQLInputFieldMap, | ||
GraphQLFieldMap, | ||
} from 'graphql/type/definition'; | ||
import { isFunction } from './is'; | ||
|
||
export type FieldMaps = GraphQLInputFieldMap | GraphQLFieldMap<*, *>; | ||
|
||
export function unwrapFieldsType<T: FieldMaps>( | ||
fieldMap: T | ||
): T { | ||
Object.keys(fieldMap).forEach(key => { | ||
if (isFunction(fieldMap[key].type)) { | ||
// $FlowFixMe | ||
fieldMap[key]._typeFn = fieldMap[key].type; | ||
// $FlowFixMe | ||
fieldMap[key].type = fieldMap[key].type(); | ||
} | ||
}); | ||
return fieldMap; | ||
} | ||
|
||
export function wrapFieldsType<T: FieldMaps>( | ||
fieldMap: T | ||
): T { | ||
Object.keys(fieldMap).forEach(key => { | ||
if (fieldMap[key]._typeFn) { | ||
// $FlowFixMe | ||
fieldMap[key].type = fieldMap[key]._typeFn; | ||
} | ||
}); | ||
return fieldMap; | ||
} |