Skip to content

Commit

Permalink
fix(TypeComposer): Fixed option name is projection for fieldConfig …
Browse files Browse the repository at this point in the history
…in methods addField, addFields

If you add `newField` via `TypeComposer`, you may provide option `projection: { field1: true,

field2: true }` to require this fields from source/database. So when you request in query only this

`newField`, then `source resolver` will request this 3 fields { newField: true, field1: true,

field2: true } from DB. After returned data  will be passed to `resolve(source, args, context,

info)` method of your `newField` in `source`.
  • Loading branch information
nodkz committed Sep 13, 2016
1 parent 8fb873d commit 1b26bf4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
77 changes: 52 additions & 25 deletions src/__tests__/typeComposer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,62 @@ import TypeComposer from '../typeComposer';


describe('TypeComposer', () => {
describe('should has `getFields` method', () => {
it('can read fields', () => {
const objectType = new GraphQLObjectType({
name: 'Readable',
fields: {
field1: { type: GraphQLString },
field2: { type: GraphQLString },
},
});

const composer = new TypeComposer(objectType);
const fieldNames = Object.keys(composer.getFields());
expect(fieldNames).to.have.members(['field1', 'field2']);
let objectType;

beforeEach(() => {
objectType = new GraphQLObjectType({
name: 'Readable',
fields: {
field1: { type: GraphQLString },
field2: { type: GraphQLString },
},
});
});


it('should has `getFields` method', () => {
const tc = new TypeComposer(objectType);
const fieldNames = Object.keys(tc.getFields());
expect(fieldNames).to.have.members(['field1', 'field2']);
});


it('should has `addFields` method', () => {
const tc = new TypeComposer(objectType);
tc.addField('field3', { type: GraphQLString });
const fieldNames = Object.keys(objectType.getFields());
expect(fieldNames).to.include('field3');
});


it('should add projection via addField and addFields', () => {
const tc = new TypeComposer(objectType);
tc.addField('field3', {
type: GraphQLString,
projection: { field1: true, field2: true },
});
tc.addFields({
field4: { type: GraphQLString },
field5: { type: GraphQLString, projection: { field4: true } },
});

expect(tc.getProjectionMapper()).to.deep.equal({
field3: { field1: true, field2: true },
field5: { field4: true },
})
});

it('should has `addFields` method', () => {
const objectType = new GraphQLObjectType({
name: 'Writeable',
fields: {
field1: { type: GraphQLString },
field2: { type: GraphQLString },
},
});

const composer = new TypeComposer(objectType);
composer.addField('field3', { type: GraphQLString });
const fieldNames = Object.keys(objectType.getFields());
expect(fieldNames).to.include('field3');
it('should clone projection for fields', () => {
const tc = new TypeComposer(objectType);
tc.addField('field3', {
type: GraphQLString,
projection: { field1: true, field2: true },
});

const tc2 = tc.clone('newObject');
expect(tc2.getProjectionMapper()).to.deep.equal({
field3: { field1: true, field2: true },
})
});
});
8 changes: 4 additions & 4 deletions src/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export type GraphQLOutputType = _GraphQLOutputType;
export type InputObjectField = _InputObjectField;
export type GraphQLInputObjectType = _GraphQLInputObjectType;
export type GraphQLFieldConfigArgumentMap = _GraphQLFieldConfigArgumentMap;
export type GraphQLFieldResolveFn = _GraphQLFieldResolveFn<any>;
export type GraphQLFieldResolveFn = _GraphQLFieldResolveFn<*>;
export type GraphQLResolveInfo = _GraphQLResolveInfo;
export type GraphQLArgumentConfig = _GraphQLArgumentConfig;
export type GraphQLNamedType = _GraphQLNamedType;
export type GraphQLFieldConfig = _GraphQLFieldConfig<any>;
export type GraphQLFieldConfigMap = _GraphQLFieldConfigMap<any>;
export type GraphQLFieldConfig = _GraphQLFieldConfig<*>;
export type GraphQLFieldConfigMap = _GraphQLFieldConfigMap<*>;
export type GraphQLFieldConfigMapThunk = () => GraphQLFieldConfigMap;
export type ResolveParams = {
source: mixed,
Expand Down Expand Up @@ -92,7 +92,7 @@ export type ResolverKinds = 'query' | 'mutation' | 'subscription';
export type ResolverMWArgsFn = (args: GraphQLFieldConfigArgumentMap) => GraphQLFieldConfigArgumentMap;
export type ResolverMWArgs = (next: ResolverMWArgsFn) => ResolverMWArgsFn;

export type ResolverMWResolveFn = (resolveParams: ResolveParams) => Promise<any>;
export type ResolverMWResolveFn = (resolveParams: ResolveParams) => Promise<*>;
export type ResolverMWResolve = (next: ResolverMWResolveFn) => ResolverMWResolveFn;

export type ResolverMWOutputTypeFn = (outputType: GraphQLOutputType) => GraphQLOutputType;
Expand Down
5 changes: 3 additions & 2 deletions src/typeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export default class TypeComposer {

// if field has a projection option, then add it to projection mapper
Object.keys(newFields).forEach(name => {
if (newFields[name]._gqcProjection) {
if (newFields[name].projection) {
// $FlowFixMe
const projection: ProjectionType = newFields[name]._gqcProjection;
const projection: ProjectionType = newFields[name].projection;
this.addProjectionMapper(name, projection);
}
})
Expand Down Expand Up @@ -282,6 +282,7 @@ export default class TypeComposer {
fields: newFields,
})
);
cloned.gqType._gqcProjectionMapper = this.gqType._gqcProjectionMapper;

cloned.setDescription(this.getDescription());
try {
Expand Down

0 comments on commit 1b26bf4

Please sign in to comment.