Skip to content

Commit

Permalink
feat(gatsby): Allow all dateformat options as directive args (#16335)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and DSchau committed Aug 3, 2019
1 parent c24a994 commit 1a02b85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ describe(`GraphQL field extensions`, () => {
)
const directive = schema.getDirective(`dateformat`)
expect(directive).toBeDefined()
expect(directive.args).toHaveLength(2)
expect(directive.args).toHaveLength(4)
})

it(`shows error message when extension is already defined`, async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/schema/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ const builtInFieldExtensions = {
args: {
formatString: `String`,
locale: `String`,
fromNow: `Boolean`,
difference: `String`,
},
extend(args, fieldConfig) {
return getDateResolver(args, fieldConfig)
Expand Down
20 changes: 8 additions & 12 deletions packages/gatsby/src/schema/types/date.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
const moment = require(`moment`)
const {
GraphQLString,
GraphQLBoolean,
GraphQLScalarType,
Kind,
defaultFieldResolver,
} = require(`graphql`)
const { GraphQLScalarType, Kind, defaultFieldResolver } = require(`graphql`)
const { oneLine } = require(`common-tags`)

const ISO_8601_FORMAT = [
Expand Down Expand Up @@ -218,12 +212,12 @@ const formatDate = ({

const getDateResolver = (defaults, prevFieldConfig) => {
const resolver = prevFieldConfig.resolve || defaultFieldResolver
const { locale, formatString } = defaults
const { locale, formatString, fromNow, difference } = defaults
return {
args: {
...prevFieldConfig.args,
formatString: {
type: GraphQLString,
type: `String`,
description: oneLine`
Format the date using Moment.js' date tokens, e.g.
\`date(formatString: "YYYY MMMM DD")\`.
Expand All @@ -232,20 +226,22 @@ const getDateResolver = (defaults, prevFieldConfig) => {
defaultValue: formatString,
},
fromNow: {
type: GraphQLBoolean,
type: `Boolean`,
description: oneLine`
Returns a string generated with Moment.js' \`fromNow\` function`,
defaultValue: fromNow,
},
difference: {
type: GraphQLString,
type: `String`,
description: oneLine`
Returns the difference between this date and the current time.
Defaults to "milliseconds" but you can also pass in as the
measurement "years", "months", "weeks", "days", "hours", "minutes",
and "seconds".`,
defaultValue: difference,
},
locale: {
type: GraphQLString,
type: `String`,
description: oneLine`
Configures the locale Moment.js will use to format the date.`,
defaultValue: locale,
Expand Down

0 comments on commit 1a02b85

Please sign in to comment.