Skip to content

Commit

Permalink
Add externalFragments props (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrove authored Feb 4, 2022
1 parent 37d1572 commit b318608
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ type Props = {
actionButtonStyle?: StyleMap,
},
showAttribution: boolean,
hideActions?: boolean
hideActions?: boolean,
externalFragments?: FragmentDefinitionNode[],
};

type OperationType = 'query' | 'mutation' | 'subscription' | 'fragment';
Expand Down Expand Up @@ -2605,7 +2606,7 @@ class Explorer extends React.PureComponent<Props, State> {
].filter(Boolean);

const actionsEl =
(actionsOptions.length === 0 || this.props.hideActions) ? null : (
actionsOptions.length === 0 || this.props.hideActions ? null : (
<div
style={{
minHeight: '50px',
Expand Down Expand Up @@ -2655,7 +2656,26 @@ class Explorer extends React.PureComponent<Props, State> {
</div>
);

const availableFragments: AvailableFragments = relevantOperations.reduce(
const externalFragments =
this.props.externalFragments &&
this.props.externalFragments.reduce((acc, fragment) => {
if (fragment.kind === 'FragmentDefinition') {
const fragmentTypeName = fragment.typeCondition.name.value;
const existingFragmentsForType = acc[fragmentTypeName] || [];
const newFragmentsForType = [
...existingFragmentsForType,
fragment,
].sort((a, b) => a.name.value.localeCompare(b.name.value));
return {
...acc,
[fragmentTypeName]: newFragmentsForType,
};
}

return acc;
}, {});

const documentFragments: AvailableFragments = relevantOperations.reduce(
(acc, operation) => {
if (operation.kind === 'FragmentDefinition') {
const fragmentTypeName = operation.typeCondition.name.value;
Expand All @@ -2675,6 +2695,8 @@ class Explorer extends React.PureComponent<Props, State> {
{},
);

const availableFragments = {...documentFragments, ...externalFragments};

const attribution = this.props.showAttribution ? <Attribution /> : null;

return (
Expand Down

0 comments on commit b318608

Please sign in to comment.