Skip to content

Commit

Permalink
feat(ResolveParams): Added findManyResolveParams and `countResolveP…
Browse files Browse the repository at this point in the history
…arams` to the top `resolveParam

It allows to get params from sub resolvers for debug purposes
  • Loading branch information
nodkz committed Oct 13, 2016
1 parent b7b769c commit d40d54e
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 186 deletions.
35 changes: 18 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@
"graphql-compose": ">=1.0.0"
},
"devDependencies": {
"babel-cli": "6.14.0",
"babel-core": "6.14.0",
"babel-eslint": "6.1.2",
"babel-cli": "6.16.0",
"babel-core": "6.17.0",
"babel-eslint": "7.0.0",
"babel-plugin-syntax-async-functions": "6.13.0",
"babel-plugin-transform-class-properties": "6.11.5",
"babel-plugin-transform-class-properties": "6.16.0",
"babel-plugin-transform-flow-strip-types": "6.14.0",
"babel-plugin-transform-object-rest-spread": "6.8.0",
"babel-plugin-transform-regenerator": "6.14.0",
"babel-plugin-transform-object-rest-spread": "6.16.0",
"babel-plugin-transform-regenerator": "6.16.1",
"babel-plugin-transform-runtime": "6.15.0",
"babel-preset-es2015": "6.14.0",
"babel-preset-es2015": "6.16.0",
"chai": "3.5.0",
"chai-as-promised": "5.3.0",
"chai-as-promised": "6.0.0",
"chai-spies": "0.7.1",
"cz-conventional-changelog": "1.2.0",
"eslint": "3.4.0",
"eslint-config-airbnb": "10.0.1",
"eslint-plugin-flowtype": "2.15.0",
"eslint-plugin-import": "1.14.0",
"eslint-plugin-jsx-a11y": "2.2.1",
"eslint-plugin-react": "6.2.0",
"flow-bin": "0.32.0",
"eslint": "3.7.1",
"eslint-config-airbnb": "11.0.0",
"eslint-plugin-flowtype": "2.20.0",
"eslint-plugin-import": "1.16.0",
"eslint-plugin-jsx-a11y": "2.2.3",
"eslint-plugin-react": "6.4.1",
"flow-bin": "0.33.0",
"ghooks": "1.3.2",
"mocha": "3.0.2",
"nyc": "8.1.0",
"mocha": "3.1.2",
"nyc": "8.3.1",
"rimraf": "2.5.4",
"sane": "1.4.1",
"semantic-release": "4.3.5"
Expand Down Expand Up @@ -86,6 +86,7 @@
"test": "babel-node ./node_modules/.bin/_mocha --compilers js:babel-core/register --reporter dot --require ./resources/mocha-bootload src/**/__tests__/**/*-test.js",
"watch": "babel-node ./resources/watch.js",
"link": "npm link graphql && npm link graphql-compose && npm link",
"unlink": "npm unlink graphql && npm unlink graphql-compose && npm install graphql graphql-compose",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
}
}
3 changes: 3 additions & 0 deletions src/__mocks__/userTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const filterArgConfig = {
gender: {
type: GraphQLString,
},
age: {
type: GraphQLInt,
},
_operators: {
type: new GraphQLInputObjectType({
name: 'OperatorsFilterUserInput',
Expand Down
73 changes: 66 additions & 7 deletions src/__tests__/composeWithConnection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { TypeComposer } from 'graphql-compose';
import { composeWithConnection } from '../composeWithConnection';
import { userTypeComposer, sortOptions } from '../__mocks__/userTypeComposer';
import { rootQueryTypeComposer } from '../__mocks__/rootQueryTypeComposer';
import { rootQueryTypeComposer as rootQueryTC } from '../__mocks__/rootQueryTypeComposer';


describe('composeWithRelay', () => {
Expand All @@ -34,11 +34,11 @@ describe('composeWithRelay', () => {
});

it('should apply first sort ID_ASC by default', async () => {
rootQueryTypeComposer.addField('userConnection',
rootQueryTC.addField('userConnection',
userTypeComposer.getResolver('connection').getFieldConfig()
);
const schema = new GraphQLSchema({
query: rootQueryTypeComposer.getType(),
query: rootQueryTC.getType(),
});
const query = `{
userConnection(last: 3) {
Expand Down Expand Up @@ -86,11 +86,11 @@ describe('composeWithRelay', () => {
});

it('should able to change `sort` on AGE_ID_DESC', async () => {
rootQueryTypeComposer.addField('userConnection',
rootQueryTC.addField('userConnection',
userTypeComposer.getResolver('connection').getFieldConfig()
);
const schema = new GraphQLSchema({
query: rootQueryTypeComposer.getType(),
query: rootQueryTC.getType(),
});
const query = `{
userConnection(first: 3, sort: AGE_ID_DESC) {
Expand Down Expand Up @@ -140,11 +140,11 @@ describe('composeWithRelay', () => {

describe('fragments fields projection of graphql-compose', () => {
it('should return object', async () => {
rootQueryTypeComposer.addField('userConnection',
rootQueryTC.addField('userConnection',
userTypeComposer.getResolver('connection').getFieldConfig()
);
const schema = new GraphQLSchema({
query: rootQueryTypeComposer.getType(),
query: rootQueryTC.getType(),
});
const query = `{
userConnection(first: 1) {
Expand Down Expand Up @@ -201,4 +201,63 @@ describe('composeWithRelay', () => {
});
});
});

it('should pass `countResolveParams` to top resolverParams', async () => {
let topResolveParams;

rootQueryTC.addField('userConnection',
userTypeComposer
.getResolver('connection')
.wrapResolve((next) => (rp) => {
const result = next(rp);
topResolveParams = rp;
return result;
})
.getFieldConfig()
);
const schema = new GraphQLSchema({
query: rootQueryTC.getType(),
});
const query = `{
userConnection(first: 1, filter: { age: 45 }) {
count
}
}`;
const result = await graphql(schema, query);
expect(topResolveParams).has.property('countResolveParams');
expect(topResolveParams.countResolveParams)
.to.contain.all.keys(['source', 'args', 'context', 'info', 'projection']);
expect(topResolveParams.countResolveParams.args)
.deep.equal({ filter: { age: 45 } });
});


it('should pass `findManyResolveParams` to top resolverParams', async () => {
let topResolveParams;

rootQueryTC.addField('userConnection',
userTypeComposer
.getResolver('connection')
.wrapResolve((next) => (rp) => {
const result = next(rp);
topResolveParams = rp;
return result;
})
.getFieldConfig()
);
const schema = new GraphQLSchema({
query: rootQueryTC.getType(),
});
const query = `{
userConnection(first: 1, filter: { age: 45 }) {
count
}
}`;
const result = await graphql(schema, query);
expect(topResolveParams).has.property('findManyResolveParams');
expect(topResolveParams.findManyResolveParams)
.to.contain.all.keys(['source', 'args', 'context', 'info', 'projection']);
expect(topResolveParams.findManyResolveParams.args)
.deep.equal({ filter: { age: 45 }, limit: 2, sort: { id: 1 } });
});
});
18 changes: 15 additions & 3 deletions src/connectionResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,21 @@ export function prepareConnectionResolver(
throw new Error('Argument `last` should be non-negative number.');
}

// pass count ResolveParams to top resolver
resolveParams.countResolveParams = {
...findManyParams,
args: {
filter: Object.assign({}, findManyParams.args.filter),
},
};
if (projection.count) {
countPromise = countResolve(findManyParams);
countPromise = countResolve(resolveParams.countResolveParams);
} else if (!first && last) {
countPromise = countResolve(findManyParams);
countPromise = countResolve(resolveParams.countResolveParams);
} else {
countPromise = Promise.resolve(0);
// count resolver not called, so remove it from top params
delete resolveParams.countResolveParams;
}

if (!first && last) {
Expand All @@ -136,6 +145,9 @@ export function prepareConnectionResolver(
findManyParams.args.skip = skip;
}

// pass findMany ResolveParams to top resolver
resolveParams.findManyResolveParams = Object.assign({}, findManyParams);

const filterDataForCursor = (record) => {
const result = {};
sortOptions.uniqueFields.forEach(fieldName => {
Expand All @@ -144,7 +156,7 @@ export function prepareConnectionResolver(
return result;
};

return Promise.all([findManyResolve(findManyParams), countPromise])
return Promise.all([findManyResolve(resolveParams.findManyResolveParams), countPromise])
.then(([recordList, count]) => {
const edges = [];
// transform record to object { cursor, node }
Expand Down
Loading

0 comments on commit d40d54e

Please sign in to comment.