Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring tool polish #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 57 additions & 17 deletions src/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as React from 'react';
import {
getNamedType,
GraphQLObjectType,
GraphQLInterfaceType,
isEnumType,
isInputObjectType,
isInterfaceType,
Expand Down Expand Up @@ -1744,12 +1745,31 @@ class FieldView extends React.PureComponent<
className += ' graphiql-explorer-deprecated';
}

const applicableFragments =
const availableInterfaceFragments = isObjectType(type)
? type.getInterfaces().reduce((acc, next) => {
if (next.name !== type.name) {
return [
...acc,
...((this.props.availableFragments || {})[next.name] || []),
];
}

// Don't add these matches, since they'll be picked up by the simpler next check
return acc;
}, [])
: null;

const basicApplicableFragments =
isObjectType(type) || isInterfaceType(type) || isUnionType(type)
? this.props.availableFragments &&
this.props.availableFragments[type.name]
: null;

const applicableFragments = [
...(basicApplicableFragments || []),
...(availableInterfaceFragments || []),
];

const node = (
<div className={className}>
<span
Expand All @@ -1767,7 +1787,7 @@ class FieldView extends React.PureComponent<
onClick={this._handleUpdateSelections}
onMouseEnter={() => {
const containsMeaningfulSubselection =
isObjectType(type) &&
(isObjectType(type) || isInterfaceType(type)) &&
selection &&
selection.selectionSet &&
selection.selectionSet.selections.filter(
Expand Down Expand Up @@ -2427,26 +2447,45 @@ class Explorer extends React.PureComponent<Props, State> {
: _relevantOperations;

const renameOperation = (targetOperation, name) => {
const targetOperationExistingName =
(targetOperation.name && targetOperation.name.value) || 'unknown';

const newName =
name == null || name === ''
? null
: {kind: 'Name', value: name, loc: undefined};
const newOperation = {...targetOperation, name: newName};

const existingDefs = parsedQuery.definitions;

const newDefinitions = existingDefs.map(existingOperation => {
if (targetOperation === existingOperation) {
return newOperation;
} else {
return existingOperation;
}
const targetOperationIsFragment =
targetOperation.kind === 'FragmentDefinition';

return visit(parsedQuery, {
OperationDefinition: node => {
if (
!targetOperationIsFragment &&
!!newName &&
node?.name?.value === targetOperation?.name?.value
) {
return {...node, name: newName};
}
},
FragmentDefinition: node => {
if (
targetOperationIsFragment &&
!!newName &&
node?.name?.value === targetOperation?.name?.value
) {
return {...node, name: newName};
}
},
FragmentSpread: node => {
if (
targetOperationIsFragment &&
node.name.value === targetOperationExistingName
) {
return {...node, name: {...newName}};
}
},
});

return {
...parsedQuery,
definitions: newDefinitions,
};
};

const cloneOperation = (
Expand Down Expand Up @@ -2733,7 +2772,8 @@ class Explorer extends React.PureComponent<Props, State> {
schema.getType(operation.typeCondition.name.value);

const fragmentFields =
fragmentType instanceof GraphQLObjectType
fragmentType instanceof GraphQLObjectType ||
fragmentType instanceof GraphQLInterfaceType
? fragmentType.getFields()
: null;

Expand Down