2021-07-05 10:43:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { objectType } = require('nexus');
|
2021-07-30 11:44:26 +02:00
|
|
|
const { prop } = require('lodash/fp');
|
2021-07-05 10:43:36 +02:00
|
|
|
|
2021-08-24 12:10:47 +02:00
|
|
|
const { utils } = require('../types');
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a higher level type for a content type which contains both the attributes, the ID and the metadata
|
|
|
|
* @param {object} contentType The content type which will be used to build its entity type
|
|
|
|
* @return {NexusObjectTypeDef}
|
|
|
|
*/
|
|
|
|
const buildEntityDefinition = contentType => {
|
|
|
|
const name = utils.getEntityName(contentType);
|
|
|
|
const typeName = utils.getTypeName(contentType);
|
|
|
|
|
|
|
|
return objectType({
|
|
|
|
name,
|
|
|
|
|
|
|
|
definition(t) {
|
2021-07-30 11:44:26 +02:00
|
|
|
// Keep the ID attribute at the top level
|
|
|
|
t.id('id', { resolve: prop('id') });
|
|
|
|
|
|
|
|
// Keep the fetched object into a dedicated `attributes` field
|
|
|
|
t.field('attributes', {
|
|
|
|
type: typeName,
|
|
|
|
resolve: source => source,
|
|
|
|
});
|
|
|
|
|
2021-08-18 17:57:12 +02:00
|
|
|
// todo[v4]: add the meta field to the entity when there will be data in it (can't add an empty type for now)
|
2021-07-05 10:43:36 +02:00
|
|
|
// t.field('meta', { type: utils.getEntityMetaName(contentType) });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = () => ({
|
|
|
|
buildEntityDefinition,
|
|
|
|
});
|