-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move infering date field to seperate file to improve code readability
- Loading branch information
Showing
2 changed files
with
107 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const moment = require(`moment`) | ||
const { GraphQLString, GraphQLBoolean } = require(`graphql`) | ||
const _ = require(`lodash`) | ||
const { oneLine } = require(`common-tags`) | ||
|
||
const ISO_8601_FORMAT = [ | ||
`YYYY`, | ||
`YYYY-MM`, | ||
`YYYY-MM-DD`, | ||
`YYYYMMDD`, | ||
`YYYY-MM-DDTHHZ`, | ||
`YYYY-MM-DDTHH:mmZ`, | ||
`YYYY-MM-DDTHHmmZ`, | ||
`YYYY-MM-DDTHH:mm:ssZ`, | ||
`YYYY-MM-DDTHHmmssZ`, | ||
`YYYY-MM-DDTHH:mm:ss.SSSZ`, | ||
`YYYY-MM-DDTHHmmss.SSSZ`, | ||
`YYYY-[W]WW`, | ||
`YYYY[W]WW`, | ||
`YYYY-[W]WW-E`, | ||
`YYYY[W]WWE`, | ||
`YYYY-DDDD`, | ||
`YYYYDDDD`, | ||
] | ||
|
||
const DateFieldDef = { | ||
type: GraphQLString, | ||
args: { | ||
formatString: { | ||
type: GraphQLString, | ||
description: oneLine` | ||
Format the date using Moment.js' date tokens e.g. | ||
"date(formatString: "YYYY MMMM DD)" | ||
See https://momentjs.com/docs/#/displaying/format/ | ||
for documentation for different tokens`, | ||
}, | ||
fromNow: { | ||
type: GraphQLBoolean, | ||
description: oneLine` | ||
Returns a string generated with Moment.js' fromNow function`, | ||
}, | ||
difference: { | ||
type: GraphQLString, | ||
description: oneLine` | ||
Returns the difference between this date and the current time. | ||
Defaults to miliseconds but you can also pass in as the | ||
measurement years, months, weeks, days, hours, minutes, | ||
and seconds.`, | ||
}, | ||
locale: { | ||
type: GraphQLString, | ||
description: oneLine` | ||
Configures the locale Moment.js will use to format the date.`, | ||
}, | ||
}, | ||
} | ||
|
||
// Check if this is a date. | ||
// All the allowed ISO 8601 date-time formats used. | ||
function shouldInfer(value) { | ||
const momentDate = moment.utc(value, ISO_8601_FORMAT, true) | ||
return momentDate.isValid() && typeof exampleValue !== `number` | ||
} | ||
|
||
function createType(fieldName) { | ||
return { | ||
...DateFieldDef, | ||
resolve(object, args) { | ||
let date | ||
if (object[fieldName]) { | ||
date = JSON.parse(JSON.stringify(object[fieldName])) | ||
} else { | ||
return null | ||
} | ||
return formatDate(date, args) | ||
}, | ||
} | ||
} | ||
|
||
function formatDate(date, args) { | ||
if (_.isPlainObject(args)) { | ||
const { fromNow, difference, formatString, locale = `en` } = args | ||
if (formatString) { | ||
return moment | ||
.utc(date, ISO_8601_FORMAT, true) | ||
.locale(locale) | ||
.format(formatString) | ||
} else if (fromNow) { | ||
return moment | ||
.utc(date, ISO_8601_FORMAT, true) | ||
.locale(locale) | ||
.fromNow() | ||
} else if (difference) { | ||
return moment().diff( | ||
moment.utc(date, ISO_8601_FORMAT, true).locale(locale), | ||
difference | ||
) | ||
} | ||
} | ||
|
||
return date | ||
} | ||
|
||
module.exports = { shouldInfer, createType } |