Skip to content

Commit

Permalink
[3987] Transform widgetFields fragment to retrieve custom widget fields
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#3987
Signed-off-by: Florian ROUËNÉ <[email protected]>
  • Loading branch information
frouene authored and sbegaudeau committed Sep 12, 2024
1 parent 8d625b1 commit b09f325
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

- https://github.com/eclipse-sirius/sirius-web/issues/2604[#2604] [diagram] Expand a tool section when the mouse is over it
- https://github.com/eclipse-sirius/sirius-web/issues/3887[#3887] [diagram] Memoize diagram representation (improve performance when selecting an element on large diagram)
- https://github.com/eclipse-sirius/sirius-web/issues/3987[#3987] [sirius-web] Transform `widgetFields` fragment to retrieve custom widget fields


== v2024.9.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@
*******************************************************************************/

import { DocumentTransform } from '@apollo/client';
import { DocumentNode, FieldNode, InlineFragmentNode, Kind, SelectionNode, visit, FragmentSpreadNode } from 'graphql';
import { DocumentNode, FragmentDefinitionNode, InlineFragmentNode, Kind, SelectionNode, visit } from 'graphql';

const shouldTransform = (document: DocumentNode) => {
return (
document.definitions[0] &&
document.definitions[0].kind === Kind.OPERATION_DEFINITION &&
(document.definitions[0].name?.value === 'detailsEvent' || document.definitions[0].name?.value === 'formEvent')
return document.definitions.some(
(definition) =>
definition.kind === Kind.OPERATION_DEFINITION &&
(definition.name?.value === 'detailsEvent' ||
definition.name?.value === 'formEvent' ||
definition.name?.value === 'formDescriptionEditorEvent' ||
definition.name?.value === 'representationsEvent' ||
definition.name?.value === 'relatedElementsEvent' ||
definition.name?.value === 'diagramFilterEvent')
);
};

const isWidgetFragment = (field: FieldNode) => {
if (field.name.value === 'widgets' || field.name.value === 'children') {
const fragmentSpreads = field.selectionSet.selections
.filter((selection: SelectionNode): selection is FragmentSpreadNode => selection.kind === Kind.FRAGMENT_SPREAD)
.map((fragmentSpread: FragmentSpreadNode) => fragmentSpread.name.value);
if (fragmentSpreads.includes('widgetFields')) {
return true;
}
}
return false;
const isWidgetFragmentDefinition = (node: FragmentDefinitionNode) => {
return node.name.value === 'widgetFields';
};

const labelField: SelectionNode = {
Expand Down Expand Up @@ -201,12 +198,10 @@ const styleField: SelectionNode = {
export const referenceWidgetDocumentTransform = new DocumentTransform((document) => {
if (shouldTransform(document)) {
return visit(document, {
Field(field) {
if (!isWidgetFragment(field)) {
FragmentDefinition(node) {
if (!isWidgetFragmentDefinition(node)) {
return undefined;
}
const selections = field.selectionSet?.selections ?? [];

const referenceWidgetInlineFragment: InlineFragmentNode = {
kind: Kind.INLINE_FRAGMENT,
selectionSet: {
Expand All @@ -231,10 +226,10 @@ export const referenceWidgetDocumentTransform = new DocumentTransform((document)
};

return {
...field,
...node,
selectionSet: {
...field.selectionSet,
selections: [...selections, referenceWidgetInlineFragment],
...node.selectionSet,
selections: [...node.selectionSet.selections, referenceWidgetInlineFragment],
},
};
},
Expand Down

0 comments on commit b09f325

Please sign in to comment.