Skip to content

Commit d918716

Browse files
authored
Merge pull request #1283 from contentful/field-resolver
feat: support graphQL annotations [MONET-1302]
2 parents 2ae3bdc + 61aa56f commit d918716

File tree

10 files changed

+71
-9
lines changed

10 files changed

+71
-9
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
module.exports = function (migration) {
22
const annotatedContentType = migration.editContentType('annotated')
33
annotatedContentType.editField('sources').setAnnotations(['Contentful:AggregateComponent'])
4+
annotatedContentType.editField('title').setAnnotations(['Contentful:GraphQLFieldResolver'], {
5+
parameters: {
6+
appFunctionId: '123',
7+
appDefinitionId: '456'
8+
}
9+
})
410
}
+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = function (migration) {
22
const annotatedContentType = migration.editContentType('annotated')
33
annotatedContentType.editField('sources').clearAnnotations()
4+
annotatedContentType.editField('title').clearAnnotations()
45
}

src/lib/action/field-annotate.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ class FieldAnnotateAction extends EntityAction {
55
private contentTypeId: string
66
private fieldId: string
77
private annotations: AnnotationLink[]
8+
private fieldAnnotationPayload: Record<string, any>
89

9-
constructor(contentTypeId: string, fieldId: string, annotations?: AnnotationLink[]) {
10+
constructor(
11+
contentTypeId: string,
12+
fieldId: string,
13+
annotations?: AnnotationLink[],
14+
fieldAnnotationPayload?: Record<string, any>
15+
) {
1016
super()
1117
this.contentTypeId = contentTypeId
1218
this.fieldId = fieldId
1319
this.annotations = annotations
20+
this.fieldAnnotationPayload = fieldAnnotationPayload
1421
}
1522

1623
getEntityId(): string {
@@ -25,7 +32,7 @@ class FieldAnnotateAction extends EntityAction {
2532
if (!this.annotations || !this.annotations.length) {
2633
ct.clearFieldAnnotations(this.fieldId)
2734
} else {
28-
ct.setFieldAnnotations(this.fieldId, this.annotations)
35+
ct.setFieldAnnotations(this.fieldId, this.annotations, this.fieldAnnotationPayload)
2936
}
3037
}
3138
}

src/lib/entities/content-type.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,16 @@ class ContentType {
711711
delete this._metadata?.annotations?.ContentType
712712
}
713713

714-
setFieldAnnotations(fieldId: string, annotations: AnnotationLink[]) {
715-
set(this, `_metadata.annotations.ContentTypeField.${fieldId}`, annotations)
714+
setFieldAnnotations(
715+
fieldId: string,
716+
annotations: AnnotationLink[],
717+
fieldAnnotationPayload?: Record<string, any>
718+
) {
719+
set(
720+
this,
721+
`_metadata.annotations.ContentTypeField.${fieldId}`,
722+
Object.assign(annotations, fieldAnnotationPayload)
723+
)
716724
}
717725

718726
getFieldAnnotations(fieldId: string) {

src/lib/intent/field-annotate.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export default class FieldAnnotateIntent extends Intent {
3131
id,
3232
type: 'Link',
3333
linkType: 'Annotation'
34-
}
34+
},
35+
...this.payload.fieldAnnotationPayload
3536
}))
3637
return [new FieldAnnotateAction(this.getContentTypeId(), this.getFieldId(), annotationLinks)]
3738
}

src/lib/interfaces/annotation.ts

+22
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ export const availableAnnotations = {
9494
]
9595
}
9696
]
97+
}),
98+
'Contentful:GraphQLFieldResolver': new Annotation({
99+
id: 'Contentful:GraphQLFieldResolver',
100+
targets: [
101+
{
102+
type: 'ContentTypeField',
103+
accept: [
104+
{
105+
type: 'Symbol'
106+
},
107+
{
108+
type: 'Array',
109+
items: {
110+
type: 'Symbol'
111+
}
112+
},
113+
{
114+
type: 'Object'
115+
}
116+
]
117+
}
118+
]
97119
})
98120
}
99121

src/lib/interfaces/raw-step.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ interface RawStepPayload {
5252
tagVisibility?: TagVisibility
5353
entryTransformationForTags?: EntrySetTags
5454
annotations?: AnnotationId[]
55+
fieldAnnotationPayload?: Record<string, any>
5556
}
5657

5758
interface EditorInterfaceInfo {

src/lib/migration-steps/action-creators.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ const actionCreators = {
674674
fieldId,
675675
fieldInstanceId,
676676
callsite,
677-
annotationIds
677+
annotationIds,
678+
fieldAnnotationPayload?
678679
): Intents.FieldAnnotate =>
679680
new Intents.FieldAnnotate({
680681
type: 'field/annotate',
@@ -689,7 +690,8 @@ const actionCreators = {
689690
payload: {
690691
contentTypeId,
691692
fieldId,
692-
annotations: annotationIds
693+
annotations: annotationIds,
694+
fieldAnnotationPayload
693695
}
694696
})
695697
},

src/lib/migration-steps/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Field extends DispatchProxy {
5252
})
5353
}
5454

55-
setAnnotations(annotationIds: string[]) {
55+
setAnnotations(annotationIds: string[], fieldAnnotationPayload?: Record<string, any>) {
5656
const callsite = getFirstExternalCaller()
5757
const fieldInstanceId = this.contentType.fieldInstanceIds.getNew(this.id)
5858
this.contentType.dispatch(
@@ -62,7 +62,8 @@ class Field extends DispatchProxy {
6262
this.id,
6363
fieldInstanceId,
6464
callsite,
65-
annotationIds
65+
annotationIds,
66+
fieldAnnotationPayload
6667
)
6768
)
6869

test/integration/migration.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,19 @@ describe('the migration', function () {
12171217
linkType: 'Annotation'
12181218
}
12191219
}
1220+
],
1221+
title: [
1222+
{
1223+
sys: {
1224+
type: 'Link',
1225+
linkType: 'Annotation',
1226+
id: 'Contentful:GraphQLFieldResolver'
1227+
},
1228+
parameters: {
1229+
appFunctionId: '123',
1230+
appDefinitionId: '456'
1231+
}
1232+
}
12201233
]
12211234
}
12221235
}

0 commit comments

Comments
 (0)