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

Fix wrong primary key being used in update and merge mutation #378

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
19 changes: 7 additions & 12 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import {
typeIdentifiers,
decideNeo4jTypeConstructor,
getAdditionalLabels,
getDerivedTypeNames
getDerivedTypeNames,
getPrimaryKeyFromFields
} from './utils';
import {
getNamedType,
Expand Down Expand Up @@ -283,9 +284,7 @@ export const relationTypeFieldOnNodeType = ({
if (innerSchemaTypeRelation.from === innerSchemaTypeRelation.to) {
return {
selection: {
initial: `${initial}${fieldName}: {${
subSelection[0]
}}${skipLimit} ${commaIfTail}`,
initial: `${initial}${fieldName}: {${subSelection[0]}}${skipLimit} ${commaIfTail}`,
...tailParams
},
subSelection
Expand Down Expand Up @@ -537,9 +536,7 @@ const directedNodeTypeFieldOnRelationType = ({
// e.g., 'from: Movie' -> 'Movie: Movie'
return {
selection: {
initial: `${initial}${fieldName}: ${variableName} {${
subSelection[0]
}}${skipLimit} ${commaIfTail}`,
initial: `${initial}${fieldName}: ${variableName} {${subSelection[0]}}${skipLimit} ${commaIfTail}`,
...tailParams
},
subSelection
Expand Down Expand Up @@ -699,9 +696,7 @@ export const neo4jType = ({
return {
initial: `${initial}${fieldName}: ${
fieldIsArray
? `reduce(a = [], INSTANCE IN ${variableName}.${fieldName} | a + {${
subSelection[0]
}})${commaIfTail}`
? `reduce(a = [], INSTANCE IN ${variableName}.${fieldName} | a + {${subSelection[0]}})${commaIfTail}`
: temporalOrderingFieldExists(parentSchemaType, parentFilterParams)
? `${safeVariableName}.${fieldName}${commaIfTail}`
: `{${subSelection[0]}}${commaIfTail}`
Expand Down Expand Up @@ -1182,7 +1177,7 @@ const nodeDelete = ({
const safeVariableName = safeVar(variableName);
const safeLabelName = safeLabel([typeName, ...additionalLabels]);
const args = getMutationArguments(resolveInfo);
const primaryKeyArg = args[0];
const primaryKeyArg = getPrimaryKeyFromFields(args) || args[0];
const primaryKeyArgName = primaryKeyArg.name.value;
const neo4jTypeArgs = getNeo4jTypeArguments(args);
const [primaryKeyParam] = splitSelectionParameters(params, primaryKeyArgName);
Expand Down Expand Up @@ -1624,7 +1619,7 @@ const nodeMergeOrUpdate = ({
const safeVariableName = safeVar(variableName);
const safeLabelName = safeLabel([typeName, ...additionalLabels]);
const args = getMutationArguments(resolveInfo);
const primaryKeyArg = args[0];
const primaryKeyArg = getPrimaryKeyFromFields(args) || args[0];
const primaryKeyArgName = primaryKeyArg.name.value;
const neo4jTypeArgs = getNeo4jTypeArguments(args);
const [primaryKeyParam, updateParams] = splitSelectionParameters(
Expand Down
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ const firstField = fields => {

export const getPrimaryKey = astNode => {
let fields = astNode.fields;
let pk = undefined;
// prevent ignored, relation, and computed fields
// from being used as primary keys
fields = fields.filter(
Expand All @@ -699,6 +698,11 @@ export const getPrimaryKey = astNode => {
!getFieldDirective(field, 'relation') &&
!getFieldDirective(field, 'cypher')
);
return getPrimaryKeyFromFields(fields);
};

export const getPrimaryKeyFromFields = fields => {
let pk = undefined;
if (!fields.length) return pk;
pk = firstNonNullAndIdField(fields);
if (!pk) {
Expand Down