GithubHelp home page GithubHelp logo

nickisyourfan / graphql-date-scalars Goto Github PK

View Code? Open in Web Editor NEW

This project forked from neofinancial/graphql-date-scalars

0.0 0.0 0.0 442 KB

Date and DateTime scalars for GraphQL

License: MIT License

JavaScript 0.97% TypeScript 99.03%

graphql-date-scalars's Introduction

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: yarn

Building

yarn build

To clean the build directory run yarn clean

Running Tests

yarn 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

graphql-date-scalars's People

Contributors

maxgoo avatar markpolivchuk avatar iansu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.