adds createdAt and updatedAt to GraphQL

This commit is contained in:
James
2020-07-12 14:37:46 -04:00
parent b4d054e1b2
commit 44a4c413a7
2 changed files with 38 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ function registerCollections() {
plural,
},
fields: initialFields,
timestamps,
},
} = collection;
@@ -48,18 +49,48 @@ function registerCollections() {
collection.graphQL = {};
const baseFields = {
id: {
type: new GraphQLNonNull(GraphQLString),
},
};
const whereInputFields = [
...fields,
];
if (timestamps) {
baseFields.createdAt = {
type: new GraphQLNonNull(GraphQLString),
};
baseFields.updatedAt = {
type: new GraphQLNonNull(GraphQLString),
};
whereInputFields.push({
name: 'createdAt',
label: 'Created At',
type: 'date',
});
whereInputFields.push({
name: 'updatedAt',
label: 'Upated At',
type: 'date',
});
}
collection.graphQL.type = this.buildObjectType(
singularLabel,
fields,
singularLabel,
{
id: { type: GraphQLString },
},
baseFields,
);
collection.graphQL.whereInputType = this.buildWhereInputType(
singularLabel,
fields,
whereInputFields,
singularLabel,
);

View File

@@ -1,7 +1,9 @@
const { GraphQLNonNull } = require('graphql');
const withNullableType = (field, type) => {
if (field.required && !field.localized) {
const hasReadAccessControl = field.access && field.access.read;
if (field.required && !field.localized && !hasReadAccessControl) {
return new GraphQLNonNull(type);
}