# @jdrydn/graphql-datetime

> [![NPM](https://badge.fury.io/js/@jdrydn%2Fgraphql-datetime.svg)](https://npm.im/@jdrydn/graphql-datetime) [![CI](https://github.com/jdrydn/graphql-scalars/actions/workflows/ci.yml/badge.svg)](https://github.com/jdrydn/graphql-scalars/actions/workflows/ci

Latest version **1.0.0** (published 2022-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jdrydn/graphql-datetime
pnpm add @jdrydn/graphql-datetime
yarn add @jdrydn/graphql-datetime
bun add @jdrydn/graphql-datetime
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-05-27 |
| First published | 2022-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.0 |
| Dependencies | 1 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jdrydn |
| Maintainers | jdrydn |
| Keywords | graphql, datetime |

## Links

- npm: https://www.npmjs.com/package/@jdrydn/graphql-datetime
- Repository: https://github.com/jdrydn/graphql-scalars
- Homepage: https://github.com/jdrydn/graphql-scalars#readme
- Issues: https://github.com/jdrydn/graphql-scalars/issues
- npm.io page: https://npm.io/package/@jdrydn/graphql-datetime

## Dependencies (1)

- [ms](https://npm.io/package/ms.md) ^2.1.3

## Alternatives

- [@js-joda/timezone](https://npm.io/package/@js-joda/timezone.md) — 383.4K weekly downloads
- [chartjs-adapter-moment](https://npm.io/package/chartjs-adapter-moment.md) — 210.8K weekly downloads
- [strftime](https://npm.io/package/strftime.md) — 171.2K weekly downloads
- [vue-flatpickr-component](https://npm.io/package/vue-flatpickr-component.md) — 115.8K weekly downloads
- [timepicker](https://npm.io/package/timepicker.md) — 51.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-05-27

## README

# graphql-datetime

[![NPM](https://badge.fury.io/js/@jdrydn%2Fgraphql-datetime.svg)](https://npm.im/@jdrydn/graphql-datetime)
[![CI](https://github.com/jdrydn/graphql-scalars/actions/workflows/ci.yml/badge.svg)](https://github.com/jdrydn/graphql-scalars/actions/workflows/ci.yml)

Standalone GraphQL Scalar type for Date values in JavaScript.

```graphql
type User {
  id: ID!
  name: String!
  createdAt: DateTime!
  lastActiveAt: DateTime!
}

extend type Query {
  user: User!
}

extend type Mutation {
  setUserLastActiveAt(id: ID!, lastActiveAt: lastActiveAt!): Boolean!
}
```

## Install

```
$ npm install --save graphql @jdrydn/graphql-datetime
```

From your codebase, you can either use predefined items (type definition & resolver) directly in your project or define the scalar yourself & include the scalar instance in your resolvers.

The following example uses [`graphql-tools`](https://npm.im/graphql-tools) & the predefined items:

```js
const assert = require('assert');
const { typeDefs: dateTimeTypeDefs, resolvers: dateTimeResolvers } = require('@jdrydn/graphql-datetime');

const typeDefs = /* GraphQL */`
  type User {
    id: ID!
    name: String!
    createdAt: DateTime!
    lastActiveAt: DateTime!
  }

  extend type Query {
    user: User!
  }

  extend type Mutation {
    setUserLastActiveAt(id: ID!, lastActiveAt: lastActiveAt!): Boolean!
  }
`;

const resolvers = {
  Query: {
    async user(_, { id }) {
      const user = await getUser(id);
      return user && user.id ? user : null;
    },
  },
  Mutation: {
    async setUserLastActiveAt(_, { id, lastActiveAt }) {
      const user = await getUser(id);
      assert(user && user.id, 'User not found');

      // Set the last active at
      user.lastActiveAt = lastActiveAt;

      await setUser(id, user);
      return true;
    },
  },
};

const schema = makeExecutableSchema({
  typeDefs: [ typeDefs, dateTimeTypeDefs ],
  resolvers: [ resolvers, dateTimeResolvers ],
});
```

Whereas this example uses [`apollo-server`](https://npm.im/apollo-server) & includes the scalar instance `DateTime` in its resolvers:

```js
const { ApolloServer } = require('apollo-server');
const { DateTime } = require('@jdrydn/graphql-datetime');

const typeDefs = /* GraphQL */`
  type User {
    id: ID!
    name: String!
    createdAt: DateTime!
    lastActiveAt: DateTime!
  }

  extend type Query {
    user: User!
  }

  extend type Mutation {
    setUserLastActiveAt(id: ID!, lastActiveAt: lastActiveAt!): Boolean!
  }

  # @NOTE You must define the scalar yourself
  scalar DateTime
`;

const resolvers = {
  Query: {
    async user(_, { id }) {
      const user = await getUser(id);
      return user && user.id ? user : null;
    },
  },
  Mutation: {
    async setUserLastActiveAt(_, { id, lastActiveAt }) {
      const user = await getUser(id);
      assert(user && user.id, 'User not found');

      // Set the last active at
      user.lastActiveAt = lastActiveAt;

      await setUser(id, user);
      return true;
    },
  },
  DateTime,
};

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

## Usage

Once the type definition & resolver is configured, you can send & receive Date values in GraphQL in both queries & mutations.

### Query

```graphql
query GetUser {
  user(id: "1") {
    id
    name
    lastActiveAt
  }
}
```
```json
{
  "data": {
    "user": {
      "id": "1",
      "name": "jdrydn",
      "lastActiveAt": "2022-05-01T09:00:00.000Z"
    }
  }
}
```

### Mutation

```graphql
mutation UpdateUser {
  setUserLastActiveAt(id: "1", lastActiveAt: "2022-05-01T09:00:00.000Z")
}
```
```json
{
  "data": {
    "setUserLastActiveAt": true
  }
}
```

## Notes

- Any questions or suggestions please [open an issue](https://github.com/jdrydn/graphql-scalars/issues).

---
_Source: https://npm.io/package/@jdrydn/graphql-datetime · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
