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

fix: do not assign undefined to x_filter keys #281

Merged
merged 3 commits into from
Aug 28, 2019
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ neo4j-version

test/tck/*
!test/tck/.gitkeep

.history
15 changes: 12 additions & 3 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export const relationFieldOnNodeType = ({
const filterParamKey = `${tailParams.paramIndex}_filter`;
const fieldArgumentParams = subSelection[1];
const filterParam = fieldArgumentParams[filterParamKey];
if (filterParam) {
if (
filterParam &&
typeof serializedFilterParam[filterParamKey] !== 'undefined'
) {
subSelection[1][filterParamKey] = serializedFilterParam[filterParamKey];
}

Expand Down Expand Up @@ -264,7 +267,10 @@ export const relationTypeFieldOnNodeType = ({
const filterParamKey = `${tailParams.paramIndex}_filter`;
const fieldArgumentParams = subSelection[1];
const filterParam = fieldArgumentParams[filterParamKey];
if (filterParam) {
if (
filterParam &&
typeof serializedFilterParam[filterParamKey] !== 'undefined'
) {
subSelection[1][filterParamKey] = serializedFilterParam[filterParamKey];
}

Expand Down Expand Up @@ -426,7 +432,10 @@ const directedNodeTypeFieldOnRelationType = ({
const filterParamKey = `${tailParams.paramIndex}_filter`;
const fieldArgumentParams = subSelection[1];
const filterParam = fieldArgumentParams[filterParamKey];
if (filterParam) {
if (
filterParam &&
typeof serializedFilterParam[filterParamKey] !== 'undefined'
) {
subSelection[1][filterParamKey] = serializedFilterParam[filterParamKey];
}
const whereClauses = [...temporalClauses, ...filterPredicates];
Expand Down
85 changes: 85 additions & 0 deletions test/unit/cypherTest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
augmentedSchemaCypherTestRunner
} from '../helpers/cypherTestHelpers';

import { augmentSchema, augmentTypeDefs, cypherQuery } from '../../src';
import { makeExecutableSchema } from 'graphql-tools';
import { graphql } from 'graphql';

const CYPHER_PARAMS = {
userId: 'user-id'
};
Expand Down Expand Up @@ -4454,3 +4458,84 @@ test('Handle @cypher field with parameterized value for field of input type argu
augmentedSchemaCypherTestRunner(t, graphQLQuery, {}, expectedCypherQuery)
]);
});

test('Cypher subquery filters with unfiltered parents', async t => {
const typeDefs = /* GraphQL */ `
type A_B_Relation @relation(name: "A_TO_B") {
from: A
to: B
}

type B_C_Relation @relation(name: "B_TO_C") {
from: B
to: C
active: Boolean!
}

type A {
bArray: [A_B_Relation!]!
}

type B {
cArray: [B_C_Relation!]!
}

type C {
id: ID!
}

type Query {
A: [A]
}
`;

const graphqlQuery = /* GraphQL */ `
{
A {
bArray {
B {
filteredCArray: cArray(filter: { active: true }) {
C {
id
}
}
}
}
}
}
`;

const expectedCypherQuery =
'MATCH (`a`:`A`) RETURN `a` {bArray: [(`a`)-[`a_bArray_relation`:`A_TO_B`]->(:`B`) | a_bArray_relation {B: head([(:`A`)-[`a_bArray_relation`]->(`a_bArray_B`:`B`) | a_bArray_B {cArray: [(`a_bArray_B`)-[`a_bArray_B_cArray_relation`:`B_TO_C`]->(:`C`) WHERE (`a_bArray_B_cArray_relation`.active = $1_filter.active) | a_bArray_B_cArray_relation {C: head([(:`B`)-[`a_bArray_B_cArray_relation`]->(`a_bArray_B_cArray_C`:`C`) | a_bArray_B_cArray_C { .id }]) }] }]) }] } AS `a`';
const expectedCypherParams = {
'1_filter': { active: true },
first: -1,
offset: 0
};

const resolvers = {
Query: {
A(object, params, ctx, resolveInfo) {
const [query, queryParams] = cypherQuery(params, ctx, resolveInfo);
t.is(query, expectedCypherQuery);
t.deepEqual(queryParams, expectedCypherParams);
return [];
}
}
};

const schema = augmentSchema(
makeExecutableSchema({
typeDefs: augmentTypeDefs(typeDefs),
resolvers
}),
{ mutation: false }
);

// query the test schema with the test query, assertion is in the resolver
const resp = await graphql(schema, graphqlQuery);

t.deepEqual(resp.data.A, []);

return;
});