quave:custom-type-date-time

v1.0.1Published 4 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

quave:custom-type-date-time

quave:custom-type-date-time is a Meteor package that provides a date time type for GraphQL and EJSON.

Why

It is desired to have the correct type automatically in the client and in the server without manual transformation.

We believe we are not reinventing the wheel in this package but what we are doing is like putting together the wheels in the vehicle :).

Installation

meteor add quave:custom-type-date-time

Usage

GraphQL Server

You register the resolver and the definition itself in the schema.

1import { DateTimeResolver } from 'meteor/quave:custom-type-date-time/DateTimeResolver';
2import { DateTimeDefinition } from 'meteor/quave:custom-type-date-time/DateTimeDefinition';
3
4startGraphQLServer({
5  typeDefs: [
6    DateTimeDefinition,
7  ],
8  resolvers: [
9    DateTimeResolver,
10  ],
11});
12

Model Definition

You define a field with this type.

1import { createModelDefinition } from 'meteor/quave:definitions';
2import { DateTimeType } from 'meteor/quave:custom-type-date-time/DateTimeType';
3
4export const PlayerDefinition = createModelDefinition({
5  name: 'Player',
6  fields: {
7    name: {
8      type: String,
9    },
10    birthday: {
11      type: DateTimeType,
12      optional: true,
13    },
14  },
15});

Client

You register this type in the client as well then it's going to work with EJSON.

1import { DateTimeType } from 'meteor/quave:custom-type-date-time/DateTimeType';
2
3DateTimeType.register();

Ready

Now you can use your type anywhere you want, see one example with React.

1import { DateTime } from 'meteor/quave:custom-type-date-time/DateTime';
2import { PlayerDefinition } from '../players/PlayersDefinitions';
3
4// Component
5const Form = () => {
6  // other hooks
7  const [birthday, setBirthday] = useState(undefined);
8
9  const edit = player => {
10    cancel();
11    setId(player._id);
12    setName(player.name);
13    setBirthday(player.birthday.formatDate());
14  };
15
16  const save = () => {
17    const player = {
18      _id,
19      name,
20      birthday: DateTime.parseDate(birthday),
21    };
22
23    savePlayer({
24      variables: {
25        player,
26      },
27      refetchQueries: () => [PlayerDefinition.graphQLManyQueryName],
28    }).then(() => cancel());
29  };
30
31  // ... return component
32}

License

MIT