Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
allow manual timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyEntropy committed Aug 20, 2018
1 parent b64e041 commit be63f62
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 16 deletions.
8 changes: 5 additions & 3 deletions docs/sdl.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- [Scalar Types](#scalar-types)
- [Example](#example)
- [Altering your schema and Migrations](#altering-your-schema-and-migrations)
- [Special cases and scripting migrations](#special-cases-and-scription-migrations)
- [Special cases and scripting migrations](#special-cases-and-scripting-migrations)

## Type Definitions

Expand All @@ -29,12 +29,14 @@ GraphQL Genie supports interfaces and unions! You may want to look into using th
* The directive @connection can be put on a list field to turn it into a type following the [Relay Cursor Connections Specification](https://facebook.github.io/relay/graphql/connections.htm) rather than just returning a normal list.
* **@model**
* By default any object type will be part of the CRUD model, using the @model directive will limit to just types that use this directive
* **@createdTimestamp**
* **@createdTimestamp (allowManual: Boolean = false)**
* The field will be automatically set when the record is created
* Must be of type DateTime
* **@updatedTimestamp**
* If you want to also be able to allow the field to be set manually in mutations set allowManual to true
* **@updatedTimestamp (allowManual: Boolean = false)**
* The field will automatically be updated when the record is updated
* Must be of type DateTime
* If you want to also be able to allow the field to be set manually in mutations set allowManual to true
* **@storeName(value: String!)**
* If you want the actual name of the type in your backend store to be something other than based off the type name. Using this you can effectively rename your type without actually migrating any data
* Interfaces and Unions
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-genie",
"version": "0.4.9",
"version": "0.4.10",
"description": "GraphQL Genie",
"browser": "./lib/browser.umd.js",
"jsnext:main": "./lib/module.js",
Expand Down Expand Up @@ -40,7 +40,7 @@
"lint": "tslint -c tslint.json -p linttsconfig.json --fix",
"lint-no-fix": "tslint -c tslint.json -p linttsconfig.json",
"postpublish": "npm run tag",
"tag": "git tag v`npm v graphql-genie version` && git push origin --tags"
"tag": "git tag -a v`npm v graphql-genie version` && git push origin --tags"
},
"jest": {
"testURL": "http://localhost",
Expand Down
10 changes: 7 additions & 3 deletions src/FortuneGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export default class FortuneGraph implements DataResolver {
return this.fortuneTypeNames;
}

private getStoreName = (type, currStoreName: string, currTypeName: string): {storeName: string, typeName: string} => {
private getStoreName = (type, currStoreName: string, currTypeName: string): { storeName: string, typeName: string } => {
let storeName = get(type, 'metadata.storeName', null);
let typeName = type.name;
if (!isEmpty(storeName) && currStoreName && currStoreName !== storeName) {
Expand Down Expand Up @@ -442,7 +442,9 @@ export default class FortuneGraph implements DataResolver {
this.addInputHook(name, (context, record) => {
switch (context.request.method) {
case 'create':
record[field.name] = new Date();
if ((isArray && isEmpty(record[field.name])) || !record[field.name]) {
record[field.name] = new Date();
}
return record;
}
});
Expand All @@ -452,7 +454,9 @@ export default class FortuneGraph implements DataResolver {
switch (context.request.method) {
case 'update':
if (!('replace' in update)) update.replace = {};
update.replace[field.name] = new Date();
if ((isArray && isEmpty(update.replace[field.name])) || !update.replace[field.name]) {
update.replace[field.name] = new Date();
}
return update;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/GenerateMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class GenerateMigrations implements TypeGenerator {
Note when merging list fields by default the array in the provided data will replace the existing data array. If you don't want to do that instead of providing an array you can provide an object with fields for push and pull or set. `
},
defaultTypename: {
type: GraphQLBoolean,
type: GraphQLString,
descriptions: 'Must be provided if every object in data does not have a `__typename` property or ids with the typename encoded'
},
conditions: {
Expand Down
12 changes: 8 additions & 4 deletions src/GraphQLSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ export class GraphQLSchemaBuilder {
directive @unique on FIELD_DEFINITION
directive @updatedTimestamp on FIELD_DEFINITION
directive @updatedTimestamp(allowManual: Boolean = false) on FIELD_DEFINITION
directive @createdTimestamp on FIELD_DEFINITION
directive @createdTimestamp on FIELD_DEFINITION
directive @createdTimestamp(allowManual: Boolean = false) on FIELD_DEFINITION
"""
An object with an ID
Expand Down Expand Up @@ -408,6 +406,9 @@ class UpdatedTimestampDirective extends SchemaDirectiveVisitor {
const type = field.type;
if (type.name === 'DateTime') {
field['updatedTimestamp'] = true;
if (this.args && this.args.allowManual) {
field['updatedTimestampAllowManual'] = true;
}
}
}
}
Expand All @@ -416,6 +417,9 @@ class CreatedTimestampDirective extends SchemaDirectiveVisitor {
const type = field.type;
if (type.name === 'DateTime') {
field.createdTimestamp = true;
if (this.args && this.args.allowManual) {
field['createdTimestampAllowManual'] = true;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/InputGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export class InputGenerator {
if (field) {
if (field.name === 'id') {
isAutoField = true;
} else if (get(field, 'metadata.updatedTimestamp') === true) {
} else if (get(field, 'metadata.updatedTimestamp') === true && !get(field, 'metadata.updatedTimestampAllowManual', false) ) {
isAutoField = true;
} else if (get(field, 'metadata.createdTimestamp') === true) {
} else if (get(field, 'metadata.createdTimestamp') === true && !get(field, 'metadata.createdTimestampAllowManual', false)) {
isAutoField = true;
}
}
Expand Down
41 changes: 40 additions & 1 deletion src/tests/__tests__/mutationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('mutationTests', () => {
});

test('create - create post connect author', async () => {
const date = new Date(1988, 2, 23);
const post = await client.mutate({
mutation: gql`mutation {
createPost(
Expand All @@ -100,6 +101,8 @@ describe('mutationTests', () => {
email: "[email protected]"
}
}
created: "${date.toISOString()}"
createdManual: "${date.toISOString()}"
}
}
) {
Expand All @@ -111,17 +114,23 @@ describe('mutationTests', () => {
author {
email
}
created
createdManual
}
}
}
`
});
console.log('post :', post);
testData.posts.push(post.data.createPost.data);
expect(post.data.createPost.data.title).toBe('Genie is great');
expect(post.data.createPost.data.text).toBe('Look how fast I can create an executable schema');
expect(post.data.createPost.data.tags).toEqual(['genie', 'graphql', 'database']);
expect(post.data.createPost.data.author.email).toBe('[email protected]');

const created = new Date(post.data.createPost.data.created);
expect(created > date).toBe(true);
const createdManual = new Date(post.data.createPost.data.createdManual);
expect(createdManual.getTime()).toBe(date.getTime());
});

test('update - push onto tags', async () => {
Expand Down Expand Up @@ -258,6 +267,36 @@ describe('mutationTests', () => {

});

test('update - updated set to passed in value', async () => {
const date = new Date(1988, 2, 23);
const post = await client.mutate({
mutation: gql`mutation {
updatePost(
input: {
data: {
updated: "${date.toISOString()}"
updatedManual: "${date.toISOString()}"
}
where: {
id: "${testData.posts[0].id}"
}
}
) {
data {
id
updated
updatedManual
}
}
}
`
});
const updated = new Date(post.data.updatePost.data.updated);
expect(updated > date).toBe(true);
const updatedManual = new Date(post.data.updatePost.data.updatedManual);
expect(updatedManual.getTime()).toBe(date.getTime());
});

test('update - pull from tags', async () => {
const post = await client.mutate({
mutation: gql`mutation {
Expand Down
2 changes: 2 additions & 0 deletions src/tests/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Post implements Submission {
published: Boolean @default(value: "true")
created: DateTime @createdTimestamp
updated: DateTime @updatedTimestamp
createdManual: DateTime @createdTimestamp(allowManual: true)
updatedManual: DateTime @updatedTimestamp(allowManual: true)
}
type Comment implements Submission {
Expand Down

0 comments on commit be63f62

Please sign in to comment.