Skip to content

Commit

Permalink
Merge pull request #1283 from contentful/field-resolver
Browse files Browse the repository at this point in the history
feat: support graphQL annotations [MONET-1302]
  • Loading branch information
kdamball authored Dec 20, 2023
2 parents 2ae3bdc + 61aa56f commit d918716
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 9 deletions.
6 changes: 6 additions & 0 deletions examples/43-assign-field-annotations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module.exports = function (migration) {
const annotatedContentType = migration.editContentType('annotated')
annotatedContentType.editField('sources').setAnnotations(['Contentful:AggregateComponent'])
annotatedContentType.editField('title').setAnnotations(['Contentful:GraphQLFieldResolver'], {
parameters: {
appFunctionId: '123',
appDefinitionId: '456'
}
})
}
1 change: 1 addition & 0 deletions examples/44-clear-field-annotations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = function (migration) {
const annotatedContentType = migration.editContentType('annotated')
annotatedContentType.editField('sources').clearAnnotations()
annotatedContentType.editField('title').clearAnnotations()
}
11 changes: 9 additions & 2 deletions src/lib/action/field-annotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ class FieldAnnotateAction extends EntityAction {
private contentTypeId: string
private fieldId: string
private annotations: AnnotationLink[]
private fieldAnnotationPayload: Record<string, any>

constructor(contentTypeId: string, fieldId: string, annotations?: AnnotationLink[]) {
constructor(
contentTypeId: string,
fieldId: string,
annotations?: AnnotationLink[],
fieldAnnotationPayload?: Record<string, any>
) {
super()
this.contentTypeId = contentTypeId
this.fieldId = fieldId
this.annotations = annotations
this.fieldAnnotationPayload = fieldAnnotationPayload
}

getEntityId(): string {
Expand All @@ -25,7 +32,7 @@ class FieldAnnotateAction extends EntityAction {
if (!this.annotations || !this.annotations.length) {
ct.clearFieldAnnotations(this.fieldId)
} else {
ct.setFieldAnnotations(this.fieldId, this.annotations)
ct.setFieldAnnotations(this.fieldId, this.annotations, this.fieldAnnotationPayload)
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/lib/entities/content-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,16 @@ class ContentType {
delete this._metadata?.annotations?.ContentType
}

setFieldAnnotations(fieldId: string, annotations: AnnotationLink[]) {
set(this, `_metadata.annotations.ContentTypeField.${fieldId}`, annotations)
setFieldAnnotations(
fieldId: string,
annotations: AnnotationLink[],
fieldAnnotationPayload?: Record<string, any>
) {
set(
this,
`_metadata.annotations.ContentTypeField.${fieldId}`,
Object.assign(annotations, fieldAnnotationPayload)
)
}

getFieldAnnotations(fieldId: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/intent/field-annotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default class FieldAnnotateIntent extends Intent {
id,
type: 'Link',
linkType: 'Annotation'
}
},
...this.payload.fieldAnnotationPayload
}))
return [new FieldAnnotateAction(this.getContentTypeId(), this.getFieldId(), annotationLinks)]
}
Expand Down
22 changes: 22 additions & 0 deletions src/lib/interfaces/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ export const availableAnnotations = {
]
}
]
}),
'Contentful:GraphQLFieldResolver': new Annotation({
id: 'Contentful:GraphQLFieldResolver',
targets: [
{
type: 'ContentTypeField',
accept: [
{
type: 'Symbol'
},
{
type: 'Array',
items: {
type: 'Symbol'
}
},
{
type: 'Object'
}
]
}
]
})
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/interfaces/raw-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface RawStepPayload {
tagVisibility?: TagVisibility
entryTransformationForTags?: EntrySetTags
annotations?: AnnotationId[]
fieldAnnotationPayload?: Record<string, any>
}

interface EditorInterfaceInfo {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/migration-steps/action-creators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ const actionCreators = {
fieldId,
fieldInstanceId,
callsite,
annotationIds
annotationIds,
fieldAnnotationPayload?
): Intents.FieldAnnotate =>
new Intents.FieldAnnotate({
type: 'field/annotate',
Expand All @@ -689,7 +690,8 @@ const actionCreators = {
payload: {
contentTypeId,
fieldId,
annotations: annotationIds
annotations: annotationIds,
fieldAnnotationPayload
}
})
},
Expand Down
5 changes: 3 additions & 2 deletions src/lib/migration-steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Field extends DispatchProxy {
})
}

setAnnotations(annotationIds: string[]) {
setAnnotations(annotationIds: string[], fieldAnnotationPayload?: Record<string, any>) {
const callsite = getFirstExternalCaller()
const fieldInstanceId = this.contentType.fieldInstanceIds.getNew(this.id)
this.contentType.dispatch(
Expand All @@ -62,7 +62,8 @@ class Field extends DispatchProxy {
this.id,
fieldInstanceId,
callsite,
annotationIds
annotationIds,
fieldAnnotationPayload
)
)

Expand Down
13 changes: 13 additions & 0 deletions test/integration/migration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,19 @@ describe('the migration', function () {
linkType: 'Annotation'
}
}
],
title: [
{
sys: {
type: 'Link',
linkType: 'Annotation',
id: 'Contentful:GraphQLFieldResolver'
},
parameters: {
appFunctionId: '123',
appDefinitionId: '456'
}
}
]
}
}
Expand Down

0 comments on commit d918716

Please sign in to comment.