Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Interfaces - Handle multiple fragments. #270

Merged
merged 9 commits into from
Aug 12, 2019
4 changes: 3 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"presets": ["env"],
"presets": [
"env"
],
"plugins": [
"transform-runtime",
"transform-async-generator-functions",
Expand Down
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 27 additions & 59 deletions src/selections.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
relationDirective,
getRelationTypeDirective,
decideNestedVariableName,
safeLabel,
safeVar,
isTemporalType,
isTemporalField,
Expand All @@ -30,7 +29,7 @@ import {
} from './translate';

export function buildCypherSelection({
initial,
initial = '',
cypherParams,
selections,
variableName,
Expand All @@ -40,9 +39,7 @@ export function buildCypherSelection({
parentSelectionInfo = {},
secondParentSelectionInfo = {}
}) {
if (!selections.length) {
return [initial, {}];
}
if (!selections.length) return [initial, {}];
selections = removeIgnoredFields(schemaType, selections);
let selectionFilters = filtersFromSelections(
selections,
Expand Down Expand Up @@ -80,32 +77,31 @@ export function buildCypherSelection({
return [subSelection, { ...shallowFilterParams, ...subFilterParams }];
};

let fieldName;
let isInlineFragment = false;
let interfaceLabel;
if (headSelection) {
if (headSelection.kind === 'InlineFragment') {
// get selections for the fragment and recurse on those
const fragmentSelections = headSelection.selectionSet.selections;
let fragmentTailParams = {
selections: fragmentSelections,
variableName,
schemaType,
resolveInfo,
parentSelectionInfo,
secondParentSelectionInfo
};
return recurse({
initial: fragmentSelections.length
? initial
: initial.substring(0, initial.lastIndexOf(',')),
...fragmentTailParams
});
} else {
fieldName = headSelection.name.value;
}
if (selections.find(({ kind }) => kind && kind === 'InlineFragment')) {
return selections
.filter(({ kind }) => kind && kind === 'InlineFragment')
.reduce((query, selection, index) => {
const fragmentSelections = selection.selectionSet.selections;
const fragmentSchemaType = resolveInfo.schema.getType(
selection.typeCondition.name.value
);
let fragmentTailParams = {
selections: fragmentSelections,
variableName,
schemaType: fragmentSchemaType,
resolveInfo,
parentSelectionInfo,
secondParentSelectionInfo
};
const result = recurse({
initial: index === 0 ? query[0] : query[0] + ',',
...fragmentTailParams
});
return result;
}, initial || ['']);
}

const fieldName = headSelection.name.value;
const commaIfTail = tailSelections.length > 0 ? ',' : '';
const isScalarSchemaType = isGraphqlScalarType(schemaType);
const schemaTypeField = !isScalarSchemaType
Expand All @@ -125,37 +121,11 @@ export function buildCypherSelection({
schemaTypeField && schemaTypeField.type ? schemaTypeField.type : {};
const innerSchemaType = innerType(fieldType); // for target "type" aka label

if (
const isInlineFragment =
innerSchemaType &&
innerSchemaType.astNode &&
innerSchemaType.astNode.kind === 'InterfaceTypeDefinition'
) {
isInlineFragment = true;
// FIXME: remove unused variables
const interfaceType = schemaType;
const interfaceName = innerSchemaType.name;

const fragments = headSelection.selectionSet.selections.filter(
item => item.kind === 'InlineFragment'
);

// FIXME: this will only handle the first inline fragment
const fragment = fragments[0];

interfaceLabel = fragment
? fragment.typeCondition.name.value
: interfaceName;
const implementationName = fragment
? fragment.typeCondition.name.value
: interfaceName;

const schemaType = resolveInfo.schema._implementations[interfaceName].find(
intfc => intfc.name === implementationName
);
}

innerSchemaType.astNode.kind === 'InterfaceTypeDefinition';
const { statement: customCypher } = cypherDirective(schemaType, fieldName);

const typeMap = resolveInfo.schema.getTypeMap();
const schemaTypeAstNode = typeMap[schemaType].astNode;

Expand Down Expand Up @@ -314,7 +284,6 @@ export function buildCypherSelection({
relDirection,
relType,
isInlineFragment,
interfaceLabel,
innerSchemaType,
temporalClauses,
resolveInfo,
Expand All @@ -332,7 +301,6 @@ export function buildCypherSelection({
schemaTypeRelation,
innerSchemaType,
isInlineFragment,
interfaceLabel,
paramIndex,
schemaType,
filterParams,
Expand Down
Loading