From 4697ab040bba2afa0ed37216d59c3e47e03b3f4a Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Fri, 20 Mar 2020 18:01:07 +0900 Subject: [PATCH 1/4] chore(gatsby): migrate date to TypeScript --- .../gatsby/src/schema/extensions/index.js | 2 +- .../src/schema/infer/add-inferred-fields.js | 55 +++++++++--------- .../src/schema/infer/inference-metadata.ts | 4 +- packages/gatsby/src/schema/schema-composer.js | 2 +- .../gatsby/src/schema/types/__tests__/date.js | 35 +++++------ .../schema/types/__tests__/sort-and-filter.js | 2 +- .../src/schema/types/{date.js => date.ts} | 58 ++++++++++++++----- packages/gatsby/src/schema/types/filter.js | 2 +- 8 files changed, 95 insertions(+), 65 deletions(-) rename packages/gatsby/src/schema/types/{date.js => date.ts} (82%) diff --git a/packages/gatsby/src/schema/extensions/index.js b/packages/gatsby/src/schema/extensions/index.js index a5a8133b95767..5653b9f084056 100644 --- a/packages/gatsby/src/schema/extensions/index.js +++ b/packages/gatsby/src/schema/extensions/index.js @@ -6,7 +6,7 @@ const { } = require(`graphql`) const { link, fileByPath } = require(`../resolvers`) -const { getDateResolver } = require(`../types/date`) +import { getDateResolver } from "../types/date" import type { GraphQLFieldConfigArgumentMap, GraphQLFieldConfig } from "graphql" import type { ComposeFieldConfig, ComposeOutputType } from "graphql-compose" diff --git a/packages/gatsby/src/schema/infer/add-inferred-fields.js b/packages/gatsby/src/schema/infer/add-inferred-fields.js index bf72a248f5e17..766a91c16353b 100644 --- a/packages/gatsby/src/schema/infer/add-inferred-fields.js +++ b/packages/gatsby/src/schema/infer/add-inferred-fields.js @@ -5,7 +5,7 @@ const invariant = require(`invariant`) const report = require(`gatsby-cli/lib/reporter`) import { isFile } from "./is-file" -const { isDate } = require(`../types/date`) +import { isDate } from "../types/date" const { addDerivedType } = require(`../types/derived-types`) import { is32BitInteger } from "../../utils/is-32-bit-integer" const { getNode, getNodes } = require(`../../redux/nodes`) @@ -15,7 +15,7 @@ const addInferredFields = ({ typeComposer, exampleValue, typeMapping, - parentSpan, + parentSpan }) => { const config = getInferenceConfig({ typeComposer, @@ -23,8 +23,8 @@ const addInferredFields = ({ shouldAddFields: true, shouldAddDefaultResolvers: typeComposer.hasExtension(`infer`) ? false - : true, - }, + : true + } }) addInferredFieldsImpl({ schemaComposer, @@ -33,12 +33,12 @@ const addInferredFields = ({ prefix: typeComposer.getTypeName(), unsanitizedFieldPath: [typeComposer.getTypeName()], typeMapping, - config, + config }) } module.exports = { - addInferredFields, + addInferredFields } const addInferredFieldsImpl = ({ @@ -48,7 +48,7 @@ const addInferredFieldsImpl = ({ typeMapping, prefix, unsanitizedFieldPath, - config, + config }) => { const fields = [] Object.keys(exampleObject).forEach(unsanitizedKey => { @@ -56,7 +56,7 @@ const addInferredFieldsImpl = ({ fields.push({ key, unsanitizedKey, - exampleValue: exampleObject[unsanitizedKey], + exampleValue: exampleObject[unsanitizedKey] }) }) @@ -85,7 +85,7 @@ const addInferredFieldsImpl = ({ prefix, unsanitizedFieldPath, typeMapping, - config, + config }) if (!fieldConfig) return @@ -146,7 +146,7 @@ const getFieldConfig = ({ unsanitizedKey, unsanitizedFieldPath, typeMapping, - config, + config }) => { const selector = `${prefix}.${key}` unsanitizedFieldPath.push(unsanitizedKey) @@ -167,7 +167,7 @@ const getFieldConfig = ({ fieldConfig = getFieldConfigFromFieldNameConvention({ schemaComposer, value: exampleValue, - key: unsanitizedKey, + key: unsanitizedKey }) arrays = arrays + (value.multiple ? 1 : 0) } else { @@ -180,7 +180,7 @@ const getFieldConfig = ({ unsanitizedFieldPath, typeMapping, config, - arrays, + arrays }) } @@ -193,8 +193,8 @@ const getFieldConfig = ({ ...fieldConfig, extensions: { ...(fieldConfig.extensions || {}), - proxy: { from: unsanitizedKey }, - }, + proxy: { from: unsanitizedKey } + } } } @@ -236,8 +236,8 @@ const getFieldConfigFromMapping = ({ typeMapping, selector }) => { return { type, extensions: { - link: { by: path.join(`.`) || `id` }, - }, + link: { by: path.join(`.`) || `id` } + } } } @@ -245,7 +245,7 @@ const getFieldConfigFromMapping = ({ typeMapping, selector }) => { const getFieldConfigFromFieldNameConvention = ({ schemaComposer, value, - key, + key }) => { const path = key.split(`___NODE___`)[1] // Allow linking by nested fields, e.g. `author___NODE___contact___email` @@ -287,8 +287,8 @@ const getFieldConfigFromFieldNameConvention = ({ return { type, extensions: { - link: { by: foreignKey || `id`, from: key }, - }, + link: { by: foreignKey || `id`, from: key } + } } } @@ -301,7 +301,7 @@ const getSimpleFieldConfig = ({ unsanitizedFieldPath, typeMapping, config, - arrays, + arrays }) => { switch (typeof value) { case `boolean`: @@ -367,7 +367,7 @@ const getSimpleFieldConfig = ({ ) addDerivedType({ typeComposer, - derivedTypeName: fieldTypeComposer.getTypeName(), + derivedTypeName: fieldTypeComposer.getTypeName() }) } } @@ -376,7 +376,7 @@ const getSimpleFieldConfig = ({ // with directive/extension, or inherited from the parent type. const inferenceConfig = getInferenceConfig({ typeComposer: fieldTypeComposer, - defaults: config, + defaults: config }) return { @@ -387,8 +387,8 @@ const getSimpleFieldConfig = ({ typeMapping, prefix: selector, unsanitizedFieldPath, - config: inferenceConfig, - }), + config: inferenceConfig + }) } } } @@ -397,7 +397,10 @@ const getSimpleFieldConfig = ({ const createTypeName = selector => { const keys = selector.split(`.`) - const suffix = keys.slice(1).map(_.upperFirst).join(``) + const suffix = keys + .slice(1) + .map(_.upperFirst) + .join(``) return `${keys[0]}${suffix}` } @@ -438,6 +441,6 @@ const getInferenceConfig = ({ typeComposer, defaults }) => { : defaults.shouldAddFields, shouldAddDefaultResolvers: typeComposer.hasExtension(`addDefaultResolvers`) ? typeComposer.getExtension(`addDefaultResolvers`) - : defaults.shouldAddDefaultResolvers, + : defaults.shouldAddDefaultResolvers } } diff --git a/packages/gatsby/src/schema/infer/inference-metadata.ts b/packages/gatsby/src/schema/infer/inference-metadata.ts index 3e29b1112bb03..f6ce9627b41e2 100644 --- a/packages/gatsby/src/schema/infer/inference-metadata.ts +++ b/packages/gatsby/src/schema/infer/inference-metadata.ts @@ -509,7 +509,7 @@ const initialMetadata = (state?: object): ITypeMetadata => { total: 0, ignoredFields: undefined, fieldMap: {}, - ...state, + ...state } } @@ -522,5 +522,5 @@ export { isEmpty, hasNodes, haveEqualFields, - initialMetadata, + initialMetadata } diff --git a/packages/gatsby/src/schema/schema-composer.js b/packages/gatsby/src/schema/schema-composer.js index 730aebc8226f5..653d76141399d 100644 --- a/packages/gatsby/src/schema/schema-composer.js +++ b/packages/gatsby/src/schema/schema-composer.js @@ -1,6 +1,6 @@ const { SchemaComposer, GraphQLJSON } = require(`graphql-compose`) const { getNodeInterface } = require(`./types/node-interface`) -const { GraphQLDate } = require(`./types/date`) +import { GraphQLDate } from "./types/date" const { addDirectives } = require(`./extensions`) const createSchemaComposer = ({ fieldExtensions } = {}) => { diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index 4156bf5588d9e..97be5420b52cf 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -3,7 +3,8 @@ const { actions } = require(`../../../redux/actions`) const { build } = require(`../..`) const withResolverContext = require(`../../context`) const { defaultResolver } = require(`../../resolvers`) -const { isDate, looksLikeADate } = require(`../date`) +import { isDate, looksLikeADate } from "../date" +require(`../../../db/__tests__/fixtures/ensure-loki`)() jest.mock(`gatsby-cli/lib/reporter`, () => { return { @@ -15,15 +16,15 @@ jest.mock(`gatsby-cli/lib/reporter`, () => { return { start: jest.fn(), setStatus: jest.fn(), - end: jest.fn(), + end: jest.fn() } }, phantomActivity: () => { return { start: jest.fn(), - end: jest.fn(), + end: jest.fn() } - }, + } } }) @@ -56,7 +57,7 @@ describe(`isDate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00`, + `2012-11-12T00:00:00+01:00` ])(`should return true for valid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -78,7 +79,7 @@ describe(`isDate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z`, + `1970-01-01 00 Z` ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -130,7 +131,7 @@ describe(`isDate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball`, + `2012-04-01T00:basketball` ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeFalsy() }) @@ -163,7 +164,7 @@ describe(`looksLikeADate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00`, + `2012-11-12T00:00:00+01:00` ])(`should return true for valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -185,7 +186,7 @@ describe(`looksLikeADate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z`, + `1970-01-01 00 Z` ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -213,7 +214,7 @@ describe(`looksLikeADate`, () => { `2010-01-01T23:60`, `2010-01-01T23:59:60`, `2010-01-40T23:60+00:00`, - `2010-01-40T23:59:60+00:00`, + `2010-01-40T23:59:60+00:00` ])(`should return true for some valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -235,7 +236,7 @@ describe(`looksLikeADate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball`, + `2012-04-01T00:basketball` ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeFalsy() }) @@ -301,8 +302,8 @@ describe(`dateResolver`, () => { invalidDate14: ``, invalidDate15: ` `, invalidDate16: `2012-04-01T00:basketball`, - defaultFormatDate: `2010-01-30T23:59:59.999-07:00`, - }, + defaultFormatDate: `2010-01-30T23:59:59.999-07:00` + } ] nodes.forEach(node => actions.createNode(node, { name: `test` })(store.dispatch) @@ -403,7 +404,7 @@ describe(`dateResolver`, () => { `2018-01-29T23:25:16.019345Z`, // Seems to not require nanosecond definition to not fail `2018-01-29T23:25:16.019345123+02:00`, - `2018-01-29T23:25:16.019345123Z`, + `2018-01-29T23:25:16.019345123Z` ])(`should return "Jan 29, 2018": %s`, async dateString => { const schema = await buildTestSchema() const fields = schema.getType(`Test`).getFields() @@ -413,7 +414,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date`, + fieldName: `date` } ) ).toEqual(`Jan 29, 2018`) @@ -426,7 +427,7 @@ describe(`dateResolver`, () => { `2010-01-01T24:01`, `2010-01-01T23:60`, `2010-01-01T23:59:60`, - `2010-01-40T23:59:59.9999`, + `2010-01-40T23:59:59.9999` // Combine with above statement once we figure out why it passes // `2018-08-31T23:25:16.01234567899993+02:00`, ])(`should return "Invalid Date": %s`, async dateString => { @@ -438,7 +439,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date`, + fieldName: `date` } ) ).toEqual(`Invalid date`) diff --git a/packages/gatsby/src/schema/types/__tests__/sort-and-filter.js b/packages/gatsby/src/schema/types/__tests__/sort-and-filter.js index 22204388cc674..f1e138501cf2a 100644 --- a/packages/gatsby/src/schema/types/__tests__/sort-and-filter.js +++ b/packages/gatsby/src/schema/types/__tests__/sort-and-filter.js @@ -18,7 +18,7 @@ const { GraphQLInputObjectType, Kind, } = require(`graphql`) -const { GraphQLDate } = require(`../date`) +import { GraphQLDate } from "../date" const { GraphQLJSON } = require(`graphql-compose`) const getInferredFields = fields => { diff --git a/packages/gatsby/src/schema/types/date.js b/packages/gatsby/src/schema/types/date.ts similarity index 82% rename from packages/gatsby/src/schema/types/date.js rename to packages/gatsby/src/schema/types/date.ts index c815b87b7a144..5fedcf7c363aa 100644 --- a/packages/gatsby/src/schema/types/date.js +++ b/packages/gatsby/src/schema/types/date.ts @@ -1,6 +1,6 @@ -const moment = require(`moment`) -const { GraphQLScalarType, Kind } = require(`graphql`) -const { oneLine } = require(`common-tags`) +import moment, { MomentInput, unitOfTime, LocaleSpecifier } from "moment" +import { GraphQLScalarType, Kind, GraphQLFieldConfig } from "graphql" +import { oneLine } from "common-tags" const ISO_8601_FORMAT = [ `YYYY`, @@ -79,14 +79,14 @@ const ISO_8601_FORMAT = [ `YYYYDDDD`, ] -const GraphQLDate = new GraphQLScalarType({ +export const GraphQLDate = new GraphQLScalarType({ name: `Date`, description: oneLine` A date string, such as 2007-12-03, compliant with the ISO 8601 standard for representation of dates and times using the Gregorian calendar.`, serialize: String, parseValue: String, - parseLiteral(ast) { + parseLiteral(ast): string | undefined { return ast.kind === Kind.STRING ? ast.value : undefined }, }) @@ -109,20 +109,22 @@ const momentFormattingRegexes = { ".": `\\.`, Z: `(Z|[+-]\\d\\d(?::?\\d\\d)?)`, } -const ISO_8601_FORMAT_AS_REGEX = ISO_8601_FORMAT.map(format => +const ISO_8601_FORMAT_AS_REGEX = ISO_8601_FORMAT.map(format => { + const matchedFormat = format.match(momentFormattingTokens) + if (matchedFormat === null) return `` // convert ISO string to a map of momentTokens ([YYYY, MM, DD]) - [...format.match(momentFormattingTokens)] + return [...matchedFormat] .map(token => // see if the token (YYYY or ss) is found, else we just return the value momentFormattingRegexes[token] ? momentFormattingRegexes[token] : token ) .join(``) -).join(`|`) +}).join(`|`) // calculate all lengths of the formats, if a string is longer or smaller it can't be valid const ISO_8601_FORMAT_LENGTHS = [ ...new Set( - ISO_8601_FORMAT.reduce((acc, val) => { + ISO_8601_FORMAT.reduce((acc: number[], val: string) => { if (!val.endsWith(`Z`)) { return acc.concat(val.length) } @@ -154,7 +156,7 @@ const looksLikeDateEndRegex = /(\d|Z)$/ * @param {*} value * @return {boolean} */ -function looksLikeADate(value) { +export function looksLikeADate(value?: string): boolean { // quick check if value does not look like a date if ( !value || @@ -178,18 +180,25 @@ function looksLikeADate(value) { * @param {*} value * @return {boolean} */ -function isDate(value) { +export function isDate(value: MomentInput): boolean { const momentDate = moment.utc(value, ISO_8601_FORMAT, true) return typeof value !== `number` && momentDate.isValid() } +interface IFormatDateArgs { + date?: Date + fromNow?: boolean + formatString?: string + difference?: unitOfTime.Diff + locale?: LocaleSpecifier +} const formatDate = ({ date, fromNow, difference, formatString, locale = `en`, -}) => { +}: IFormatDateArgs): string | number => { const normalizedDate = JSON.parse(JSON.stringify(date)) if (formatString) { return moment @@ -210,7 +219,26 @@ const formatDate = ({ return normalizedDate } -const getDateResolver = (options = {}, fieldConfig) => { +interface IDateResolverOption { + locale?: string + formatString?: string + fromNow?: string + difference?: string + from?: string + fromNode?: { type: string; defaultValue: boolean } +} +type DateResolverFieldConfig = GraphQLFieldConfig +type DateResolver = ( + source: any, + args: any, + context: any, + info: any +) => Promise + +export const getDateResolver = ( + options: IDateResolverOption = {}, + fieldConfig: DateResolverFieldConfig +): { args: Record; resolve: DateResolver } => { const { locale, formatString, fromNow, difference } = options return { args: { @@ -246,7 +274,7 @@ const getDateResolver = (options = {}, fieldConfig) => { defaultValue: locale, }, }, - async resolve(source, args, context, info) { + async resolve(source, args, context, info): ReturnType { const resolver = fieldConfig.resolve || context.defaultFieldResolver const date = await resolver(source, args, context, { ...info, @@ -261,5 +289,3 @@ const getDateResolver = (options = {}, fieldConfig) => { }, } } - -module.exports = { GraphQLDate, getDateResolver, isDate, looksLikeADate } diff --git a/packages/gatsby/src/schema/types/filter.js b/packages/gatsby/src/schema/types/filter.js index bc0cb52f52f24..ae1f23b23ef17 100644 --- a/packages/gatsby/src/schema/types/filter.js +++ b/packages/gatsby/src/schema/types/filter.js @@ -9,7 +9,7 @@ const { const { addDerivedType } = require(`./derived-types`) const { InputTypeComposer } = require(`graphql-compose`) const { GraphQLJSON } = require(`graphql-compose`) -const { GraphQLDate } = require(`./date`) +import { GraphQLDate } from "./date" const SEARCHABLE_ENUM = { SEARCHABLE: `SEARCHABLE`, From f324f2f6d4639d53c1ada4b2709f53c3ca0f5b7f Mon Sep 17 00:00:00 2001 From: gatsbybot Date: Tue, 30 Jun 2020 14:36:35 +0000 Subject: [PATCH 2/4] chore: format --- .../src/schema/infer/inference-metadata.ts | 4 +-- .../gatsby/src/schema/types/__tests__/date.js | 32 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/gatsby/src/schema/infer/inference-metadata.ts b/packages/gatsby/src/schema/infer/inference-metadata.ts index f6ce9627b41e2..3e29b1112bb03 100644 --- a/packages/gatsby/src/schema/infer/inference-metadata.ts +++ b/packages/gatsby/src/schema/infer/inference-metadata.ts @@ -509,7 +509,7 @@ const initialMetadata = (state?: object): ITypeMetadata => { total: 0, ignoredFields: undefined, fieldMap: {}, - ...state + ...state, } } @@ -522,5 +522,5 @@ export { isEmpty, hasNodes, haveEqualFields, - initialMetadata + initialMetadata, } diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index 97be5420b52cf..ed849a25c0286 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -16,15 +16,15 @@ jest.mock(`gatsby-cli/lib/reporter`, () => { return { start: jest.fn(), setStatus: jest.fn(), - end: jest.fn() + end: jest.fn(), } }, phantomActivity: () => { return { start: jest.fn(), - end: jest.fn() + end: jest.fn(), } - } + }, } }) @@ -57,7 +57,7 @@ describe(`isDate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00` + `2012-11-12T00:00:00+01:00`, ])(`should return true for valid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -79,7 +79,7 @@ describe(`isDate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z` + `1970-01-01 00 Z`, ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -131,7 +131,7 @@ describe(`isDate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball` + `2012-04-01T00:basketball`, ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeFalsy() }) @@ -164,7 +164,7 @@ describe(`looksLikeADate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00` + `2012-11-12T00:00:00+01:00`, ])(`should return true for valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -186,7 +186,7 @@ describe(`looksLikeADate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z` + `1970-01-01 00 Z`, ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -214,7 +214,7 @@ describe(`looksLikeADate`, () => { `2010-01-01T23:60`, `2010-01-01T23:59:60`, `2010-01-40T23:60+00:00`, - `2010-01-40T23:59:60+00:00` + `2010-01-40T23:59:60+00:00`, ])(`should return true for some valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -236,7 +236,7 @@ describe(`looksLikeADate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball` + `2012-04-01T00:basketball`, ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeFalsy() }) @@ -302,8 +302,8 @@ describe(`dateResolver`, () => { invalidDate14: ``, invalidDate15: ` `, invalidDate16: `2012-04-01T00:basketball`, - defaultFormatDate: `2010-01-30T23:59:59.999-07:00` - } + defaultFormatDate: `2010-01-30T23:59:59.999-07:00`, + }, ] nodes.forEach(node => actions.createNode(node, { name: `test` })(store.dispatch) @@ -404,7 +404,7 @@ describe(`dateResolver`, () => { `2018-01-29T23:25:16.019345Z`, // Seems to not require nanosecond definition to not fail `2018-01-29T23:25:16.019345123+02:00`, - `2018-01-29T23:25:16.019345123Z` + `2018-01-29T23:25:16.019345123Z`, ])(`should return "Jan 29, 2018": %s`, async dateString => { const schema = await buildTestSchema() const fields = schema.getType(`Test`).getFields() @@ -414,7 +414,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date` + fieldName: `date`, } ) ).toEqual(`Jan 29, 2018`) @@ -427,7 +427,7 @@ describe(`dateResolver`, () => { `2010-01-01T24:01`, `2010-01-01T23:60`, `2010-01-01T23:59:60`, - `2010-01-40T23:59:59.9999` + `2010-01-40T23:59:59.9999`, // Combine with above statement once we figure out why it passes // `2018-08-31T23:25:16.01234567899993+02:00`, ])(`should return "Invalid Date": %s`, async dateString => { @@ -439,7 +439,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date` + fieldName: `date`, } ) ).toEqual(`Invalid date`) From 0221560de41a4655ae1fc4cc92d2ec7bc7f90a0c Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Wed, 15 Jul 2020 14:15:25 +0900 Subject: [PATCH 3/4] fix pointer out --- .../gatsby/src/schema/types/__tests__/date.js | 33 +++++---- packages/gatsby/src/schema/types/date.ts | 70 +++++++++---------- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index ed849a25c0286..87ff48ac08749 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -4,7 +4,6 @@ const { build } = require(`../..`) const withResolverContext = require(`../../context`) const { defaultResolver } = require(`../../resolvers`) import { isDate, looksLikeADate } from "../date" -require(`../../../db/__tests__/fixtures/ensure-loki`)() jest.mock(`gatsby-cli/lib/reporter`, () => { return { @@ -16,15 +15,15 @@ jest.mock(`gatsby-cli/lib/reporter`, () => { return { start: jest.fn(), setStatus: jest.fn(), - end: jest.fn(), + end: jest.fn() } }, phantomActivity: () => { return { start: jest.fn(), - end: jest.fn(), + end: jest.fn() } - }, + } } }) @@ -57,7 +56,7 @@ describe(`isDate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00`, + `2012-11-12T00:00:00+01:00` ])(`should return true for valid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -79,7 +78,7 @@ describe(`isDate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z`, + `1970-01-01 00 Z` ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -131,7 +130,7 @@ describe(`isDate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball`, + `2012-04-01T00:basketball` ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeFalsy() }) @@ -164,7 +163,7 @@ describe(`looksLikeADate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00`, + `2012-11-12T00:00:00+01:00` ])(`should return true for valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -186,7 +185,7 @@ describe(`looksLikeADate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z`, + `1970-01-01 00 Z` ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -214,7 +213,7 @@ describe(`looksLikeADate`, () => { `2010-01-01T23:60`, `2010-01-01T23:59:60`, `2010-01-40T23:60+00:00`, - `2010-01-40T23:59:60+00:00`, + `2010-01-40T23:59:60+00:00` ])(`should return true for some valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -236,7 +235,7 @@ describe(`looksLikeADate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball`, + `2012-04-01T00:basketball` ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeFalsy() }) @@ -302,8 +301,8 @@ describe(`dateResolver`, () => { invalidDate14: ``, invalidDate15: ` `, invalidDate16: `2012-04-01T00:basketball`, - defaultFormatDate: `2010-01-30T23:59:59.999-07:00`, - }, + defaultFormatDate: `2010-01-30T23:59:59.999-07:00` + } ] nodes.forEach(node => actions.createNode(node, { name: `test` })(store.dispatch) @@ -404,7 +403,7 @@ describe(`dateResolver`, () => { `2018-01-29T23:25:16.019345Z`, // Seems to not require nanosecond definition to not fail `2018-01-29T23:25:16.019345123+02:00`, - `2018-01-29T23:25:16.019345123Z`, + `2018-01-29T23:25:16.019345123Z` ])(`should return "Jan 29, 2018": %s`, async dateString => { const schema = await buildTestSchema() const fields = schema.getType(`Test`).getFields() @@ -414,7 +413,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date`, + fieldName: `date` } ) ).toEqual(`Jan 29, 2018`) @@ -427,7 +426,7 @@ describe(`dateResolver`, () => { `2010-01-01T24:01`, `2010-01-01T23:60`, `2010-01-01T23:59:60`, - `2010-01-40T23:59:59.9999`, + `2010-01-40T23:59:59.9999` // Combine with above statement once we figure out why it passes // `2018-08-31T23:25:16.01234567899993+02:00`, ])(`should return "Invalid Date": %s`, async dateString => { @@ -439,7 +438,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date`, + fieldName: `date` } ) ).toEqual(`Invalid date`) diff --git a/packages/gatsby/src/schema/types/date.ts b/packages/gatsby/src/schema/types/date.ts index 5fedcf7c363aa..f652a6d066ec5 100644 --- a/packages/gatsby/src/schema/types/date.ts +++ b/packages/gatsby/src/schema/types/date.ts @@ -2,6 +2,29 @@ import moment, { MomentInput, unitOfTime, LocaleSpecifier } from "moment" import { GraphQLScalarType, Kind, GraphQLFieldConfig } from "graphql" import { oneLine } from "common-tags" +interface IFormatDateArgs { + date?: Date + fromNow?: boolean + formatString?: string + difference?: unitOfTime.Diff + locale?: LocaleSpecifier +} +interface IDateResolverOption { + locale?: string + formatString?: string + fromNow?: string + difference?: string + from?: string + fromNode?: { type: string; defaultValue: boolean } +} +type DateResolverFieldConfig = GraphQLFieldConfig +type DateResolver = ( + source: any, + args: any, + context: any, + info: any +) => Promise + const ISO_8601_FORMAT = [ `YYYY`, `YYYY-MM`, @@ -76,7 +99,7 @@ const ISO_8601_FORMAT = [ `YYYY-[W]WW-E`, `YYYY[W]WWE`, `YYYY-DDDD`, - `YYYYDDDD`, + `YYYYDDDD` ] export const GraphQLDate = new GraphQLScalarType({ @@ -88,7 +111,7 @@ export const GraphQLDate = new GraphQLScalarType({ parseValue: String, parseLiteral(ast): string | undefined { return ast.kind === Kind.STRING ? ast.value : undefined - }, + } }) const momentFormattingTokens = /(\[[^[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Qo?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g @@ -107,7 +130,7 @@ const momentFormattingRegexes = { WW: `\\d{2}`, "[W]": `W`, ".": `\\.`, - Z: `(Z|[+-]\\d\\d(?::?\\d\\d)?)`, + Z: `(Z|[+-]\\d\\d(?::?\\d\\d)?)` } const ISO_8601_FORMAT_AS_REGEX = ISO_8601_FORMAT.map(format => { const matchedFormat = format.match(momentFormattingTokens) @@ -132,7 +155,7 @@ const ISO_8601_FORMAT_LENGTHS = [ // we add count of +01 & +01:00 return acc.concat([val.length, val.length + 3, val.length + 5]) }, []) - ), + ) ] // lets imagine these formats: YYYY-MM-DDTHH & YYYY-MM-DD HHmmss.SSSSSS Z @@ -185,19 +208,12 @@ export function isDate(value: MomentInput): boolean { return typeof value !== `number` && momentDate.isValid() } -interface IFormatDateArgs { - date?: Date - fromNow?: boolean - formatString?: string - difference?: unitOfTime.Diff - locale?: LocaleSpecifier -} const formatDate = ({ date, fromNow, difference, formatString, - locale = `en`, + locale = `en` }: IFormatDateArgs): string | number => { const normalizedDate = JSON.parse(JSON.stringify(date)) if (formatString) { @@ -219,22 +235,6 @@ const formatDate = ({ return normalizedDate } -interface IDateResolverOption { - locale?: string - formatString?: string - fromNow?: string - difference?: string - from?: string - fromNode?: { type: string; defaultValue: boolean } -} -type DateResolverFieldConfig = GraphQLFieldConfig -type DateResolver = ( - source: any, - args: any, - context: any, - info: any -) => Promise - export const getDateResolver = ( options: IDateResolverOption = {}, fieldConfig: DateResolverFieldConfig @@ -250,13 +250,13 @@ export const getDateResolver = ( \`date(formatString: "YYYY MMMM DD")\`. See https://momentjs.com/docs/#/displaying/format/ for documentation for different tokens.`, - defaultValue: formatString, + defaultValue: formatString }, fromNow: { type: `Boolean`, description: oneLine` Returns a string generated with Moment.js' \`fromNow\` function`, - defaultValue: fromNow, + defaultValue: fromNow }, difference: { type: `String`, @@ -265,27 +265,27 @@ export const getDateResolver = ( Defaults to "milliseconds" but you can also pass in as the measurement "years", "months", "weeks", "days", "hours", "minutes", and "seconds".`, - defaultValue: difference, + defaultValue: difference }, locale: { type: `String`, description: oneLine` Configures the locale Moment.js will use to format the date.`, - defaultValue: locale, - }, + defaultValue: locale + } }, async resolve(source, args, context, info): ReturnType { const resolver = fieldConfig.resolve || context.defaultFieldResolver const date = await resolver(source, args, context, { ...info, from: options.from || info.from, - fromNode: options.from ? options.fromNode : info.fromNode, + fromNode: options.from ? options.fromNode : info.fromNode }) if (date == null) return null return Array.isArray(date) ? date.map(d => formatDate({ date: d, ...args })) : formatDate({ date, ...args }) - }, + } } } From 8dc8af94519868fceb754cbda616d58312a171b8 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Wed, 15 Jul 2020 15:00:49 +0900 Subject: [PATCH 4/4] fix format --- .../src/schema/infer/add-inferred-fields.js | 53 +++++++++---------- .../gatsby/src/schema/types/__tests__/date.js | 32 +++++------ packages/gatsby/src/schema/types/date.ts | 24 ++++----- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/packages/gatsby/src/schema/infer/add-inferred-fields.js b/packages/gatsby/src/schema/infer/add-inferred-fields.js index 766a91c16353b..c8a008e9ffe79 100644 --- a/packages/gatsby/src/schema/infer/add-inferred-fields.js +++ b/packages/gatsby/src/schema/infer/add-inferred-fields.js @@ -15,7 +15,7 @@ const addInferredFields = ({ typeComposer, exampleValue, typeMapping, - parentSpan + parentSpan, }) => { const config = getInferenceConfig({ typeComposer, @@ -23,8 +23,8 @@ const addInferredFields = ({ shouldAddFields: true, shouldAddDefaultResolvers: typeComposer.hasExtension(`infer`) ? false - : true - } + : true, + }, }) addInferredFieldsImpl({ schemaComposer, @@ -33,12 +33,12 @@ const addInferredFields = ({ prefix: typeComposer.getTypeName(), unsanitizedFieldPath: [typeComposer.getTypeName()], typeMapping, - config + config, }) } module.exports = { - addInferredFields + addInferredFields, } const addInferredFieldsImpl = ({ @@ -48,7 +48,7 @@ const addInferredFieldsImpl = ({ typeMapping, prefix, unsanitizedFieldPath, - config + config, }) => { const fields = [] Object.keys(exampleObject).forEach(unsanitizedKey => { @@ -56,7 +56,7 @@ const addInferredFieldsImpl = ({ fields.push({ key, unsanitizedKey, - exampleValue: exampleObject[unsanitizedKey] + exampleValue: exampleObject[unsanitizedKey], }) }) @@ -85,7 +85,7 @@ const addInferredFieldsImpl = ({ prefix, unsanitizedFieldPath, typeMapping, - config + config, }) if (!fieldConfig) return @@ -146,7 +146,7 @@ const getFieldConfig = ({ unsanitizedKey, unsanitizedFieldPath, typeMapping, - config + config, }) => { const selector = `${prefix}.${key}` unsanitizedFieldPath.push(unsanitizedKey) @@ -167,7 +167,7 @@ const getFieldConfig = ({ fieldConfig = getFieldConfigFromFieldNameConvention({ schemaComposer, value: exampleValue, - key: unsanitizedKey + key: unsanitizedKey, }) arrays = arrays + (value.multiple ? 1 : 0) } else { @@ -180,7 +180,7 @@ const getFieldConfig = ({ unsanitizedFieldPath, typeMapping, config, - arrays + arrays, }) } @@ -193,8 +193,8 @@ const getFieldConfig = ({ ...fieldConfig, extensions: { ...(fieldConfig.extensions || {}), - proxy: { from: unsanitizedKey } - } + proxy: { from: unsanitizedKey }, + }, } } @@ -236,8 +236,8 @@ const getFieldConfigFromMapping = ({ typeMapping, selector }) => { return { type, extensions: { - link: { by: path.join(`.`) || `id` } - } + link: { by: path.join(`.`) || `id` }, + }, } } @@ -245,7 +245,7 @@ const getFieldConfigFromMapping = ({ typeMapping, selector }) => { const getFieldConfigFromFieldNameConvention = ({ schemaComposer, value, - key + key, }) => { const path = key.split(`___NODE___`)[1] // Allow linking by nested fields, e.g. `author___NODE___contact___email` @@ -287,8 +287,8 @@ const getFieldConfigFromFieldNameConvention = ({ return { type, extensions: { - link: { by: foreignKey || `id`, from: key } - } + link: { by: foreignKey || `id`, from: key }, + }, } } @@ -301,7 +301,7 @@ const getSimpleFieldConfig = ({ unsanitizedFieldPath, typeMapping, config, - arrays + arrays, }) => { switch (typeof value) { case `boolean`: @@ -367,7 +367,7 @@ const getSimpleFieldConfig = ({ ) addDerivedType({ typeComposer, - derivedTypeName: fieldTypeComposer.getTypeName() + derivedTypeName: fieldTypeComposer.getTypeName(), }) } } @@ -376,7 +376,7 @@ const getSimpleFieldConfig = ({ // with directive/extension, or inherited from the parent type. const inferenceConfig = getInferenceConfig({ typeComposer: fieldTypeComposer, - defaults: config + defaults: config, }) return { @@ -387,8 +387,8 @@ const getSimpleFieldConfig = ({ typeMapping, prefix: selector, unsanitizedFieldPath, - config: inferenceConfig - }) + config: inferenceConfig, + }), } } } @@ -397,10 +397,7 @@ const getSimpleFieldConfig = ({ const createTypeName = selector => { const keys = selector.split(`.`) - const suffix = keys - .slice(1) - .map(_.upperFirst) - .join(``) + const suffix = keys.slice(1).map(_.upperFirst).join(``) return `${keys[0]}${suffix}` } @@ -441,6 +438,6 @@ const getInferenceConfig = ({ typeComposer, defaults }) => { : defaults.shouldAddFields, shouldAddDefaultResolvers: typeComposer.hasExtension(`addDefaultResolvers`) ? typeComposer.getExtension(`addDefaultResolvers`) - : defaults.shouldAddDefaultResolvers + : defaults.shouldAddDefaultResolvers, } } diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index 87ff48ac08749..221e29bf9b064 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -15,15 +15,15 @@ jest.mock(`gatsby-cli/lib/reporter`, () => { return { start: jest.fn(), setStatus: jest.fn(), - end: jest.fn() + end: jest.fn(), } }, phantomActivity: () => { return { start: jest.fn(), - end: jest.fn() + end: jest.fn(), } - } + }, } }) @@ -56,7 +56,7 @@ describe(`isDate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00` + `2012-11-12T00:00:00+01:00`, ])(`should return true for valid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -78,7 +78,7 @@ describe(`isDate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z` + `1970-01-01 00 Z`, ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) @@ -130,7 +130,7 @@ describe(`isDate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball` + `2012-04-01T00:basketball`, ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(isDate(dateString)).toBeFalsy() }) @@ -163,7 +163,7 @@ describe(`looksLikeADate`, () => { `2010-01-30T00:00:00.000Z`, `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00` + `2012-11-12T00:00:00+01:00`, ])(`should return true for valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -185,7 +185,7 @@ describe(`looksLikeADate`, () => { `1970-01-01 00:00:00 Z`, `1970-01-01 000000 Z`, `1970-01-01 00:00 Z`, - `1970-01-01 00 Z` + `1970-01-01 00 Z`, ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -213,7 +213,7 @@ describe(`looksLikeADate`, () => { `2010-01-01T23:60`, `2010-01-01T23:59:60`, `2010-01-40T23:60+00:00`, - `2010-01-40T23:59:60+00:00` + `2010-01-40T23:59:60+00:00`, ])(`should return true for some valid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeTruthy() }) @@ -235,7 +235,7 @@ describe(`looksLikeADate`, () => { {}, ``, ` `, - `2012-04-01T00:basketball` + `2012-04-01T00:basketball`, ])(`should return false for invalid ISO 8601: %s`, dateString => { expect(looksLikeADate(dateString)).toBeFalsy() }) @@ -301,8 +301,8 @@ describe(`dateResolver`, () => { invalidDate14: ``, invalidDate15: ` `, invalidDate16: `2012-04-01T00:basketball`, - defaultFormatDate: `2010-01-30T23:59:59.999-07:00` - } + defaultFormatDate: `2010-01-30T23:59:59.999-07:00`, + }, ] nodes.forEach(node => actions.createNode(node, { name: `test` })(store.dispatch) @@ -403,7 +403,7 @@ describe(`dateResolver`, () => { `2018-01-29T23:25:16.019345Z`, // Seems to not require nanosecond definition to not fail `2018-01-29T23:25:16.019345123+02:00`, - `2018-01-29T23:25:16.019345123Z` + `2018-01-29T23:25:16.019345123Z`, ])(`should return "Jan 29, 2018": %s`, async dateString => { const schema = await buildTestSchema() const fields = schema.getType(`Test`).getFields() @@ -413,7 +413,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date` + fieldName: `date`, } ) ).toEqual(`Jan 29, 2018`) @@ -426,7 +426,7 @@ describe(`dateResolver`, () => { `2010-01-01T24:01`, `2010-01-01T23:60`, `2010-01-01T23:59:60`, - `2010-01-40T23:59:59.9999` + `2010-01-40T23:59:59.9999`, // Combine with above statement once we figure out why it passes // `2018-08-31T23:25:16.01234567899993+02:00`, ])(`should return "Invalid Date": %s`, async dateString => { @@ -438,7 +438,7 @@ describe(`dateResolver`, () => { { formatString: `MMM DD, YYYY` }, withResolverContext({}, schema), { - fieldName: `date` + fieldName: `date`, } ) ).toEqual(`Invalid date`) diff --git a/packages/gatsby/src/schema/types/date.ts b/packages/gatsby/src/schema/types/date.ts index f652a6d066ec5..08d18e0aacf6e 100644 --- a/packages/gatsby/src/schema/types/date.ts +++ b/packages/gatsby/src/schema/types/date.ts @@ -99,7 +99,7 @@ const ISO_8601_FORMAT = [ `YYYY-[W]WW-E`, `YYYY[W]WWE`, `YYYY-DDDD`, - `YYYYDDDD` + `YYYYDDDD`, ] export const GraphQLDate = new GraphQLScalarType({ @@ -111,7 +111,7 @@ export const GraphQLDate = new GraphQLScalarType({ parseValue: String, parseLiteral(ast): string | undefined { return ast.kind === Kind.STRING ? ast.value : undefined - } + }, }) const momentFormattingTokens = /(\[[^[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Qo?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g @@ -130,7 +130,7 @@ const momentFormattingRegexes = { WW: `\\d{2}`, "[W]": `W`, ".": `\\.`, - Z: `(Z|[+-]\\d\\d(?::?\\d\\d)?)` + Z: `(Z|[+-]\\d\\d(?::?\\d\\d)?)`, } const ISO_8601_FORMAT_AS_REGEX = ISO_8601_FORMAT.map(format => { const matchedFormat = format.match(momentFormattingTokens) @@ -155,7 +155,7 @@ const ISO_8601_FORMAT_LENGTHS = [ // we add count of +01 & +01:00 return acc.concat([val.length, val.length + 3, val.length + 5]) }, []) - ) + ), ] // lets imagine these formats: YYYY-MM-DDTHH & YYYY-MM-DD HHmmss.SSSSSS Z @@ -213,7 +213,7 @@ const formatDate = ({ fromNow, difference, formatString, - locale = `en` + locale = `en`, }: IFormatDateArgs): string | number => { const normalizedDate = JSON.parse(JSON.stringify(date)) if (formatString) { @@ -250,13 +250,13 @@ export const getDateResolver = ( \`date(formatString: "YYYY MMMM DD")\`. See https://momentjs.com/docs/#/displaying/format/ for documentation for different tokens.`, - defaultValue: formatString + defaultValue: formatString, }, fromNow: { type: `Boolean`, description: oneLine` Returns a string generated with Moment.js' \`fromNow\` function`, - defaultValue: fromNow + defaultValue: fromNow, }, difference: { type: `String`, @@ -265,27 +265,27 @@ export const getDateResolver = ( Defaults to "milliseconds" but you can also pass in as the measurement "years", "months", "weeks", "days", "hours", "minutes", and "seconds".`, - defaultValue: difference + defaultValue: difference, }, locale: { type: `String`, description: oneLine` Configures the locale Moment.js will use to format the date.`, - defaultValue: locale - } + defaultValue: locale, + }, }, async resolve(source, args, context, info): ReturnType { const resolver = fieldConfig.resolve || context.defaultFieldResolver const date = await resolver(source, args, context, { ...info, from: options.from || info.from, - fromNode: options.from ? options.fromNode : info.fromNode + fromNode: options.from ? options.fromNode : info.fromNode, }) if (date == null) return null return Array.isArray(date) ? date.map(d => formatDate({ date: d, ...args })) : formatDate({ date, ...args }) - } + }, } }