Skip to content

Commit

Permalink
refactor(package): refresh code
Browse files Browse the repository at this point in the history
  • Loading branch information
I committed Jul 26, 2016
1 parent 33c66e2 commit a5b1298
Show file tree
Hide file tree
Showing 26 changed files with 185 additions and 174 deletions.
1 change: 0 additions & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
no-shadow: 0
no-param-reassign: 0
comma-dangle: 0
object-curly-spacing: 0
no-underscore-dangle: 0
import/no-extraneous-dependencies: 0
no-prototype-builtins: 0
Expand Down
3 changes: 3 additions & 0 deletions example/.eslint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rules:
no-console: 0
4 changes: 2 additions & 2 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import koa from 'koa';
import parser from 'koa-bodyparser';
import mongoose from 'mongoose';
import graffiti from '@risingstack/graffiti';
import {getSchema} from '../src';
import { getSchema } from '../src';

import User from './user';

Expand All @@ -22,7 +22,7 @@ const hooks = {
}
}
};
const schema = getSchema([User], {hooks});
const schema = getSchema([User], { hooks });

// set up example server
const app = koa();
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"start": "babel-node app.js"
},
"dependencies": {
"@risingstack/graffiti": "^3.0.3",
"@risingstack/graffiti": "3.1.0",
"@risingstack/graffiti-mongoose": "../",
"babel-cli": "6.11.4",
"babel-runtime": "6.9.2",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@
"chai-subset": "1.3.0",
"eslint": "3.1.1",
"eslint-config-airbnb-base": "5.0.0",
"eslint-plugin-import": "1.11.1",
"eslint-plugin-import": "1.12.0",
"graphql": "0.6.2",
"mocha": "2.5.3",
"mongoose": "4.5.7",
"objectid": "3.2.1",
"pre-commit": "1.1.3",
"sinon": "1.17.4",
"sinon": "1.17.5",
"sinon-chai": "2.8.0"
},
"pre-commit": [
Expand Down
32 changes: 16 additions & 16 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-unused-expressions */

import {expect} from 'chai';
import {spy} from 'sinon';
import { expect } from 'chai';
import { spy } from 'sinon';

import {getSchema, graphql} from './';
import { getSchema, graphql } from './';
import User from '../fixture/user';

describe('e2e', () => {
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('e2e', () => {
post: spy()
}
};
schema = getSchema([User], {hooks});
schema = getSchema([User], { hooks });
});

beforeEach(async function BeforeEach() {
Expand Down Expand Up @@ -262,14 +262,14 @@ describe('e2e', () => {
}
}`);

const {id, users} = result.data.viewer;
const { id, users } = result.data.viewer;
expect(id).to.be.ok;
expect(users.count).to.be.equal(3);

expect(users.edges).to.containSubset([
{node: {name: 'Mother'}},
{node: {name: 'Foo'}},
{node: {name: 'Bar'}}
{ node: { name: 'Mother' } },
{ node: { name: 'Foo' } },
{ node: { name: 'Bar' } }
]);
});

Expand All @@ -287,11 +287,11 @@ describe('e2e', () => {
}
}`);

const {users} = result.data.viewer;
const { users } = result.data.viewer;
expect(users.count).to.be.eql(1);

expect(users.edges).to.containSubset([
{node: {name: 'Foo'}}
{ node: { name: 'Foo' } }
]);
});

Expand Down Expand Up @@ -431,7 +431,7 @@ describe('e2e', () => {
`);

const node = result.data.addUser.changedUserEdge.node;
const {id} = node;
const { id } = node;
expect(typeof node._id).to.be.equal('string');
expect(node.name).to.be.equal('Test User');

Expand Down Expand Up @@ -530,7 +530,7 @@ describe('e2e', () => {
}
}
`);
const {id} = result.data.addUser.changedUserEdge.node;
const { id } = result.data.addUser.changedUserEdge.node;

result = await graphql(schema, `
mutation deleteUserMutation {
Expand All @@ -555,7 +555,7 @@ describe('e2e', () => {

describe('hooks', () => {
it('should call viewer hooks on a viewer query', async () => {
const {pre, post} = hooks.viewer;
const { pre, post } = hooks.viewer;
pre.reset();
post.reset();

Expand All @@ -573,7 +573,7 @@ describe('e2e', () => {
});

it('should call singular hooks on a singular query', async () => {
const {pre, post} = hooks.singular;
const { pre, post } = hooks.singular;
pre.reset();
post.reset();

Expand All @@ -589,7 +589,7 @@ describe('e2e', () => {
});

it('should call plural hooks on a plural query', async () => {
const {pre, post} = hooks.plural;
const { pre, post } = hooks.plural;
pre.reset();
post.reset();

Expand All @@ -605,7 +605,7 @@ describe('e2e', () => {
});

it('should call mutation hooks on a mutation', async () => {
const {pre, post} = hooks.mutation;
const { pre, post } = hooks.mutation;
pre.reset();
post.reset();

Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {graphql} from 'graphql';
import {getTypes} from './type';
import {getSchema} from './schema';
import {getModels} from './model';
import { graphql } from 'graphql';
import { getTypes } from './type';
import { getSchema } from './schema';
import { getModels } from './model';

function _getTypes(mongooseModels) {
const graffitiModels = getModels(mongooseModels);
Expand Down
2 changes: 1 addition & 1 deletion src/model/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './model';
export {default} from './model';
export { default } from './model';
45 changes: 21 additions & 24 deletions src/model/model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduce, reduceRight, merge} from 'lodash';
import { reduce, reduceRight, merge } from 'lodash';
import mongoose from 'mongoose';

const embeddedModels = {};
Expand Down Expand Up @@ -42,7 +42,7 @@ function getField(schemaPath) {
instance,
options
} = schemaPath.caster;
const {ref} = options || {};
const { ref } = options || {};

field.subtype = instance;

Expand All @@ -63,47 +63,44 @@ function getField(schemaPath) {
* @return {Object} field
*/
function extractPath(schemaPath) {
const subs = schemaPath.path.split('.');
const subNames = schemaPath.path.split('.');

return reduceRight(subs, (field, sub, key) => {
return reduceRight(subNames, (fields, name, key) => {
const obj = {};

if (schemaPath instanceof mongoose.Schema.Types.DocumentArray) {
const subSchemaPaths = schemaPath.schema.paths;
const fields = extractPaths(subSchemaPaths, {name: sub}); // eslint-disable-line no-use-before-define
obj[sub] = {
name: sub,
const fields = extractPaths(subSchemaPaths, { name }); // eslint-disable-line no-use-before-define
obj[name] = {
name,
fields,
nonNull: false,
type: 'Array',
subtype: 'Object',
fields
subtype: 'Object'
};
} else if (schemaPath instanceof mongoose.Schema.Types.Embedded) {
schemaPath.modelName = schemaPath.schema.options.graphqlTypeName || sub;
schemaPath.modelName = schemaPath.schema.options.graphqlTypeName || name;
// embedded model must be unique Instance
const embeddedModel = embeddedModels.hasOwnProperty(schemaPath.modelName)
? embeddedModels[schemaPath.modelName]
: getModel(schemaPath); // eslint-disable-line no-use-before-define

embeddedModels[schemaPath.modelName] = embeddedModel;
obj[sub] = {
obj[name] = {
...getField(schemaPath),
embeddedModel
};
} else if (key === (subs.length - 1)) {
obj[sub] = getField(schemaPath);
} else if (key === subNames.length - 1) {
obj[name] = getField(schemaPath);
} else {
obj[sub] = {
name: sub,
obj[name] = {
name,
fields,
nonNull: false,
type: 'Object',
fields: field
type: 'Object'
};
}

subNames.pop();

return obj;
}, {});
}
Expand Down Expand Up @@ -131,7 +128,7 @@ function getModel(model) {
const schemaPaths = model.schema.paths;
const name = model.modelName;

const fields = extractPaths(schemaPaths, {name});
const fields = extractPaths(schemaPaths, { name });

return {
name,
Expand All @@ -148,10 +145,10 @@ function getModel(model) {
function getModels(mongooseModels) {
return mongooseModels
.map(getModel)
.reduce((models, model) => {
models[model.name] = model;
return models;
}, {});
.reduce((models, model) => ({
...models,
[model.name]: model
}), {});
}

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/model/model.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import {extractPath, extractPaths} from './';
import { expect } from 'chai';
import { extractPath, extractPaths } from './';

describe('model', () => {
it('should extract tree chunk from path', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/query/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './query';
export {default} from './query';
export { default } from './query';
12 changes: 7 additions & 5 deletions src/query/projection/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function getFieldList(context, fieldASTs) {
function getFieldList(context, fieldASTs) {
if (!context) {
return {};
}
Expand All @@ -15,22 +15,22 @@ export default function getFieldList(context, fieldASTs) {
// get all selectionSets
const selections = asts.reduce((selections, source) => {
if (source.selectionSet) {
selections.push(...source.selectionSet.selections);
return selections.concat(source.selectionSet.selections);
}

return selections;
}, []);

// return fields
return selections.reduce((list, ast) => {
const {name, kind} = ast;
const { name, kind } = ast;

switch (kind) {
case 'Field':
list[name.value] = true;
return {
...list,
...getFieldList(context, ast)
...getFieldList(context, ast),
[name.value]: true
};
case 'InlineFragment':
return {
Expand All @@ -47,3 +47,5 @@ export default function getFieldList(context, fieldASTs) {
}
}, {});
}

export default getFieldList;
8 changes: 4 additions & 4 deletions src/query/projection/projection.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {expect} from 'chai';
import { expect } from 'chai';
import getFieldList from './';

describe('projection', () => {
it('should return an object of fields (\'Field\' fragment)', () => {
const context = {
fieldASTs: {
kind: 'Field',
name: {value: 'foo'},
name: { value: 'foo' },
selectionSet: {
selections: [{
kind: 'Field',
name: {value: 'bar'},
name: { value: 'bar' },
selectionSet: {
selections: [{
kind: 'Field',
name: {value: 'baz'}
name: { value: 'baz' }
}]
}
}]
Expand Down
Loading

0 comments on commit a5b1298

Please sign in to comment.