Skip to content

Commit

Permalink
refactor(field): separate date resolve fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Aug 1, 2015
1 parent 2602415 commit 5731f50
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ from 'graphql/type';
import {getProjection} from './projection';

/**
* TODO: return with field name
*
* @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
* @param {Object} types
Expand Down Expand Up @@ -45,14 +57,8 @@ function getField(field, types, models, model) {
// Date
else if (field.instance === 'Date') {
graphQLfield.type = GraphQLString;
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
if (isDate(modelInstance[fieldASTs.name.value])) {
return modelInstance[fieldASTs.name.value].toISOString();
}

// not a date
return modelInstance[fieldASTs.name.value];
};
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) =>
resolveDateType(modelInstance[fieldASTs.name.value]);
}

// Boolean
Expand Down Expand Up @@ -93,16 +99,8 @@ function getField(field, types, models, model) {
graphQLfield.type = new GraphQLList(GraphQLBoolean);
} else if (field.caster.instance === 'Date') {
graphQLfield.type = new GraphQLList(GraphQLString);
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
return modelInstance[fieldASTs.name.value].map((value) => {
if (isDate(value)) {
return value.toISOString();
}

// not a date
return modelInstance[fieldASTs.name.value];
});
};
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) =>
modelInstance[fieldASTs.name.value].map(resolveDateType);
}
}
}
Expand Down

0 comments on commit 5731f50

Please sign in to comment.