/
entity.ts
44 lines (36 loc) 路 1.42 KB
/
entity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { objectType } from 'nexus';
import { prop, identity, isEmpty } from 'lodash/fp';
import type { Schema } from '@strapi/types';
import type { Context } from '../types';
export default ({ strapi }: Context) => {
const { naming } = strapi.plugin('graphql').service('utils');
return {
/**
* Build a higher level type for a content type which contains the attributes, the ID and the metadata
* @param {object} contentType The content type which will be used to build its entity type
* @return {NexusObjectTypeDef}
*/
buildEntityDefinition(contentType: Schema.ContentType) {
const { attributes } = contentType;
const name = naming.getEntityName(contentType);
const typeName = naming.getTypeName(contentType);
return objectType({
name,
definition(t) {
// Keep the ID attribute at the top level
t.id('id', { resolve: prop('id') });
if (!isEmpty(attributes)) {
// Keep the fetched object into a dedicated `attributes` field
// TODO: [v4] precise why we keep the ID
t.field('attributes', {
type: typeName,
resolve: identity,
});
}
// todo[v4]: add the meta field to the entity when there will be data in it (can't add an empty type for now)
// t.field('meta', { type: utils.getEntityMetaName(contentType) });
},
});
},
};
};