graphql-date-scalars
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

graphql-date-scalars

Build status TypeScript 4.4.4

GraphQL Scalars for Date (YYYY-MM-DD), DateTime (YYYY-MM-DDTHH:MM:SSZ), and Time (HH:MM:SSZ)

Schema Usage

import { gql, ApolloServer } from 'apollo-server';
import { DateScalar, TimeScalar, DateTimeScalar } from 'graphql-date-scalars';

const resolvers = {
  // Must define resolvers for these scalars
  Date: DateScalar,
  Time: TimeScalar,
  DateTime: DateTimeScalar,

  // along with all your other resolvers
  Query: {
    exampleDateQuery: () => {
      // Will serialize to a date string, such as 2007-12-03
      return new Date();
    },
    exampleTimeQuery: () => {
      // Will serialize to a time string at UTC, such as 10:15:30Z
      return new Date();
    },
  },
  Mutation: {
    exampleDateTimeMutation: () => {
      // Will serialize to a date-time string at UTC, such as 2007-12-03T10:15:30Z
      return new Date();
    },
  },
};

const typeDefs = gql`
  scalar Date
  scalar DateTime

  extend type Query {
    exampleDateQuery: Date!
    exampleTimeQuery: Time!
  }

  extend type Mutation {
    exampleDateTimeMutation: DateTime!
  }
`;

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

If you are using @graphql-codegen then you must include these scalars in your codegen yml file under config

schema: './example-schema.graphql'
config:
  scalars:
    Date: Date
    Time: Date
    DateTime: Date
generates:
  src/types/example-schema.d.ts:
    plugins:
      - 'typescript'

Direct Usage

You can also use the parse and serialize methods directly, which is useful if you are making a rest call to a 3rd party vendor.

import { DateTimeScalar } from 'graphql-date-scalars';

const vendorResponse = await restClient.get();

const response = {
  ...vendorResponse,
  createdAt: DateTimeScalar.parseValue(vendorResponse.createdAt),
};
import { DateScalar } from 'graphql-date-scalars';

const args = {
  ...input,
  dateOfBirth: DateScalar.serialize(input.dateOfBirth),
};

const response = await restClient.post(args);

Contributing

  1. Fork this repo
  2. Clone the forked repo
  3. Install dependencies: npm i

Building

npm run build

To clean the build directory run npm run clean

Running Tests

npm run test

Publishing

  1. Update the version in package.json
  2. Add a CHANGELOG.md entry
  3. Commit your changes
  4. Run npm pack to see what will be published then delete the .tgz file that was created
  5. Run npm publish
  6. Create a release on GitHub. Use the version as the tag and release name. For example for version 1.0.0 the tag and release name would be v1.0.0.

Contributors

This project started as a fork of https://github.com/excitement-engineer/graphql-iso-date

Package Sidebar

Install

npm i graphql-date-scalars

Weekly Downloads

5,747

Version

0.2.0

License

MIT

Unpacked Size

25.7 kB

Total Files

21

Last publish

Collaborators

  • neo-daniel-brennan
  • neoauderesantos
  • neo-matt-felice
  • dennisbarthel
  • neo-rivan-mota
  • lucachamecki
  • edward.nazarov
  • ar-ies
  • michelepiperni
  • kyle-elyk-neo
  • amjad-mahfoud-neo
  • vidalrmrz
  • rnetto46
  • kaelanneofinancial
  • maiko.trindade
  • darrenpicard25
  • brayden_sc_neo
  • eric.zaporzan.neo
  • lucasparreira
  • lewishorwood
  • craigsiemens
  • diegobmy
  • joelsimpson
  • ronellgarcia
  • neo-elgiz-abbasov
  • u2v22
  • neo-bclausi
  • nikita-syrotenko
  • jcavalcanti
  • johnclendvoy
  • neo-peter-spangler
  • dustinn1235
  • neogibson
  • beraki
  • vrochamaaia
  • d_fenniak
  • adamtranquilla
  • lucaslongarini
  • divanshu
  • caio.andrade
  • timothy.couch.neofinancial
  • brentcam
  • mike.himbeault-neo
  • stefan.amyotte
  • neo-travis-friesen
  • herculesjr
  • nahom-neo
  • joeljwood
  • neo-rodrigoraval
  • malcolm-neo
  • scottkayeneo
  • dhruvang
  • mohpor
  • raul.ribeiro
  • lavanya.mohan
  • mhowitt-neo
  • benx-neo
  • neo-dakota-chatt
  • vinicius-barcelos
  • rjberry
  • desmond-maloney
  • maiahneo
  • lucasjohannson
  • stburke
  • neo-ricardo-gomez
  • neo-dione-silva
  • tunde-neo
  • neobot1
  • neo-bryce-brandford
  • gaulford
  • danielafekhume-neo
  • kennethcolina
  • neo-paula-grangeiro
  • neo-ahmed-seifelnasr
  • jadonduby
  • biancaballena
  • neo-shubham-patel
  • neo-will-parker
  • neo-chris-noble
  • neo-diego-nunes
  • felipe.minetto
  • neo-oleksandr-yanchenko
  • neo-lior-ben-shahar
  • antonio.canabrava
  • mganzneo
  • neo-eason-chang
  • neo-edimar-cardoso
  • rajanatneo
  • neo-colin-hanlon-dearman
  • alejandrogarbi
  • matt-armstrong
  • leah-is-offline
  • annakumova
  • jimi487
  • neo-tamika-taylor
  • samin_farajian
  • mahtab.khan
  • niloofarshs
  • neo-julio-falbo
  • neo-levin-dixon
  • neo-bruno-lautenschlager
  • marianniem
  • sabaneo
  • michael.harmer.neo
  • ibrahimelement
  • neo-omar-hamid
  • neo-sourav-kumar
  • charles.ai.neo
  • matteo-molnar
  • neo-chloe-bouchard
  • lily.yao
  • jhio.soriano.neo
  • vadym-aminov
  • neo-ann-nguyen
  • neo-shiva-radmanesh
  • s.kettle
  • neo-thulio-horta
  • thiago.marinho.neo
  • neo-cody-johnson
  • felipe.assis
  • junliu007
  • jose_cardozo
  • iansu