Skip to content

Commit

Permalink
chore(package): update graphql-js to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Sep 23, 2015
1 parent b5b670a commit 0cef077
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ORM"
],
"dependencies": {
"graphql": "0.2.2",
"graphql": "^0.4.4",
"lodash": "^3.10.0"
},
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions src/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error'
import { Kind } from 'graphql/language'

export default new GraphQLScalarType({
name: 'Date',
serialize(value) {
if (!(value instanceof Date)) {
throw new Error("Field error: value is not an instance of Date")
}
if (isNaN(value.getTime())) {
throw new Error("Field error: value is an invalid Date")
}
return value.toJSON()
},
parseValue(value) {
this.serialize(value);
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING ) {
throw new GraphQLError("Query error: Can only parse strings to dates but got a: " + ast.kind, [ast])
}
let result = new Date(ast.value);
if (isNaN(result.getTime())) {
throw new GraphQLError("Query error: Invalid date", [ast])
}
if (ast.value != result.toJSON()) {
throw new GraphQLError("Query error: Invalid date format, only accepts: YYYY-MM-DDTHH:MM:SS.SSSZ", [ast])
}
return result
}
});
39 changes: 14 additions & 25 deletions src/field.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import {isDate} from 'lodash';
import {reduce, clone, isDate} from 'lodash';

import {
GraphQLString,
GraphQLFloat,
GraphQLBoolean,
GraphQLList
GraphQLList,
GraphQLObjectType
}
from 'graphql/type';
import GraphQLDate from './date';

import {getProjection} from './projection';

/**
* @method resolveDateType
* @param {Date} value
* @return {String}
*/
function resolveDateType (value) {
if (isDate(value)) {
return value.toISOString();
}

// not a date
return value;
}

/**
* @method getField
* @param {Object} field
Expand All @@ -50,7 +38,7 @@ function getField(field, types, models, model) {
graphQLfield.description += ` and reference to "${field.ref}" model`;

graphQLfield.type = types[field.ref];
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
graphQLfield.resolve = (modelInstance, params, {source, fieldASTs}) => {
var projections = getProjection(fieldASTs);

return models[field.ref].model.findOne({
Expand All @@ -77,9 +65,7 @@ function getField(field, types, models, model) {

// Date
else if (field.instance === 'Date') {
graphQLfield.type = GraphQLString;
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) =>
resolveDateType(modelInstance[fieldASTs.name.value]);
graphQLfield.type = GraphQLDate;
}

// Boolean
Expand All @@ -98,7 +84,7 @@ function getField(field, types, models, model) {
graphQLfield.description += ` and reference to "${field.caster.ref}" model`;

graphQLfield.type = new GraphQLList(types[field.caster.ref]);
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
graphQLfield.resolve = (modelInstance, params, {source, fieldASTs}) => {
var projections = getProjection(fieldASTs);
return models[field.caster.ref].model.find({
_id: {
Expand All @@ -124,16 +110,19 @@ function getField(field, types, models, model) {
} else if (field.caster.instance === 'Boolean') {
graphQLfield.type = new GraphQLList(GraphQLBoolean);
} else if (field.caster.instance === 'Date') {
graphQLfield.type = new GraphQLList(GraphQLString);
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) =>
modelInstance[fieldASTs.name.value].map(resolveDateType);
graphQLfield.type = new GraphQLList(GraphQLDate);
}
}
}

// Object
else if (field.instance === 'Object') {
// TODO: implement
var fieldCopy = clone(field, true);
fieldCopy.fields = reduce(fieldCopy.caster.fields, (result, subfield, key) => {
result[key] = getField(subfield, types, models, model);
return result;
}, {});
graphQLfield.type = new GraphQLObjectType(fieldCopy);
}

return graphQLfield;
Expand Down
53 changes: 19 additions & 34 deletions src/field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GraphQLBoolean,
GraphQLList
} from 'graphql/type';
import GraphQLDate from './date';

import User from '../fixture/user';
import {getField} from './field';
Expand Down Expand Up @@ -36,9 +37,11 @@ describe('field', () => {

var result = yield field.resolve({
_id: user._id.toString()
}, undefined, undefined, {
name: {
value: '_id'
}, undefined, {
fieldASTs: {
name: {
value: '_id'
}
}
});

Expand Down Expand Up @@ -125,21 +128,11 @@ describe('field', () => {
name: 'Bar'
});

var result = field.resolve({
value: new Date(1438273470215)
}, undefined, undefined, {
name: {
value: 'value',
name: 'foo',
description: '"foo" field of the "Bar" model with type "Date"'
}
});

expect(field).to.containSubset({
type: GraphQLString
expect(field).to.be.eql({
name: 'foo',
description: '"foo" field of the "Bar" model with type "Date"',
type: GraphQLDate
});

expect(result).to.be.equal('2015-07-30T16:24:30.215Z');
});

it('should resolve Array of ObjectId with ref properly', function* () {
Expand All @@ -161,7 +154,7 @@ describe('field', () => {
ref: 'User'
}
}, {
User: 'foo'
User: GraphQLString
}, {
User: {
model: User
Expand All @@ -172,9 +165,11 @@ describe('field', () => {

var result = yield field.resolve({
value: [users[0]._id, users[1]._id]
}, undefined, undefined, {
name: {
value: 'value'
}, undefined, {
fieldASTs: {
name: {
value: 'value'
}
}
});

Expand All @@ -189,7 +184,7 @@ describe('field', () => {
expect(result).to.be.eql(users);

expect(field).to.containSubset({
type: new GraphQLList('foo'),
type: new GraphQLList(GraphQLString),
name: 'value',
description: '"value" field of the "User" model with type "Array"' +
' of "ObjectID" and reference to "User" model'
Expand Down Expand Up @@ -279,18 +274,8 @@ describe('field', () => {
name: 'Bar'
});

var result = field.resolve({
value: [new Date(0), new Date(1438273470215)]
}, undefined, undefined, {
name: {
value: 'value'
}
});

expect(result).to.be.eql(['1970-01-01T00:00:00.000Z', '2015-07-30T16:24:30.215Z']);

expect(field).to.containSubset({
type: new GraphQLList(GraphQLString),
expect(field).to.be.eql({
type: new GraphQLList(GraphQLDate),
name: 'foo',
description: '"foo" field of the "Bar" model with type "Array" of "Date"'
});
Expand Down
2 changes: 1 addition & 1 deletion src/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {find} from 'lodash';
* @return {Object} projection
*/
function getProjection(fieldASTs) {
const {selections} = fieldASTs.selectionSet;
const {selections} = Array.isArray(fieldASTs) ? fieldASTs[0].selectionSet : fieldASTs.selectionSet;

/*
* FIXME: there is no way currently to get the required fields from "FragmentSpread"
Expand Down
17 changes: 6 additions & 11 deletions src/query.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {reduce} from 'lodash';

import {
GraphQLString,
GraphQLList
} from 'graphql/type';

import {getProjection} from './projection';
import {getField} from './field';
import { GraphQLString, GraphQLList } from 'graphql/type';
import { reduce } from 'lodash';
import { getField } from './field';
import { getProjection } from './projection';

/**
* @method getArgs
Expand Down Expand Up @@ -68,7 +63,7 @@ function getRootFields (types, models) {
fields[singularName] = {
type: type,
args: singularArgs,
resolve: (root, args, source, fieldASTs) => {
resolve: (root, args, {source, fieldASTs}) => {
var projections = getProjection(fieldASTs);

var filter = reduce(args, (args, arg, argName) => {
Expand All @@ -89,7 +84,7 @@ function getRootFields (types, models) {
fields[pluralName] = {
type: new GraphQLList(type),
args: pluralArgs,
resolve: (root, args, source, fieldASTs) => {
resolve: (root, args, {source, fieldASTs}) => {
var projections = getProjection(fieldASTs);

var filter = reduce(args, (args, arg, argName) => {
Expand Down
1 change: 1 addition & 0 deletions src/type.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('type', () => {
description: '"Foo" type'
},
name: 'Foo',
isTypeOf: undefined,
description: '"Foo" type'
}
});
Expand Down

0 comments on commit 0cef077

Please sign in to comment.