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

Bugfix/empty sort #316

Merged
merged 2 commits into from
Jul 13, 2021
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
29 changes: 25 additions & 4 deletions packages/graphql/src/schema/make-augmented-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,18 +1037,32 @@ function makeAugmentedSchema(
});
});
} else {
const relatedNode = nodes.find((n) => n.name === connectionField.relationship.typeMeta.name) as Node;

connectionWhere.addFields({
node: `${connectionField.relationship.typeMeta.name}Where`,
node_NOT: `${connectionField.relationship.typeMeta.name}Where`,
});

const connectionSort = composer.createInputTC({
name: `${connectionField.typeMeta.name}Sort`,
fields: {
node: `${connectionField.relationship.typeMeta.name}Sort`,
},
fields: {},
});

const nodeSortFields = [
...relatedNode.primitiveFields,
...relatedNode.enumFields,
...relatedNode.scalarFields,
...relatedNode.dateTimeFields,
...relatedNode.pointFields,
].filter((f) => !f.typeMeta.array);

if (nodeSortFields.length) {
connectionSort.addFields({
node: `${connectionField.relationship.typeMeta.name}Sort`,
});
}

if (connectionField.relationship.properties) {
connectionSort.addFields({
relationship: `${connectionField.relationship.properties}Sort`,
Expand All @@ -1057,14 +1071,21 @@ function makeAugmentedSchema(

composeNodeArgs = {
...composeNodeArgs,
sort: connectionSort.NonNull.List,
first: {
type: "Int",
},
after: {
type: "String",
},
};

// If any sortable fields, add sort argument to connection field
if (nodeSortFields.length || connectionField.relationship.properties) {
composeNodeArgs = {
...composeNodeArgs,
sort: connectionSort.NonNull.List,
};
}
}

composeNode.addFields({
Expand Down
108 changes: 108 additions & 0 deletions packages/graphql/tests/integration/issues/#288.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Driver } from "neo4j-driver";
import { graphql } from "graphql";
import { gql } from "apollo-server";
import { generate } from "randomstring";
import neo4j from "../neo4j";
import { Neo4jGraphQL } from "../../../src/classes";

describe("https://github.com/neo4j/graphql/issues/288", () => {
let driver: Driver;
const typeDefs = gql`
type USER {
USERID: String
COMPANYID: String
COMPANY: [COMPANY] @relationship(type: "IS_PART_OF", direction: OUT)
}

type COMPANY {
USERS: [USER] @relationship(type: "IS_PART_OF", direction: IN)
}
`;

beforeAll(async () => {
driver = await neo4j();
});

afterAll(async () => {
await driver.close();
});

test("COMPANYID can be populated on create and update", async () => {
const session = driver.session();

const neoSchema = new Neo4jGraphQL({ typeDefs, driver });

const userid = generate({ charset: "alphabetic" });
const companyid1 = generate({ charset: "alphabetic" });
const companyid2 = generate({ charset: "alphabetic" });

const createMutation = `
mutation {
createUSERS(input: { USERID: "${userid}", COMPANYID: "${companyid1}" }) {
users {
USERID
COMPANYID
}
}
}
`;

const updateMutation = `
mutation {
updateUSERS(where: { USERID: "${userid}" }, update: { COMPANYID: "${companyid2}" }) {
users {
USERID
COMPANYID
}
}
}
`;

try {
await neoSchema.checkNeo4jCompat();

const createResult = await graphql({
schema: neoSchema.schema,
source: createMutation,
contextValue: { driver },
});

expect(createResult.errors).toBeFalsy();

expect(createResult?.data?.createUSERS?.users).toEqual([{ USERID: userid, COMPANYID: companyid1 }]);

const updateResult = await graphql({
schema: neoSchema.schema,
source: updateMutation,
contextValue: { driver },
});

expect(updateResult.errors).toBeFalsy();

expect(updateResult?.data?.updateUSERS?.users).toEqual([{ USERID: userid, COMPANYID: companyid2 }]);

await session.run(`MATCH (u:USER) WHERE u.USERID = "${userid}" DELETE u`);
} finally {
await session.close();
}
});
});
99 changes: 99 additions & 0 deletions packages/graphql/tests/tck/tck-test-files/cypher/issues/#288.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
## #288

<https://github.com/neo4j/graphql/issues/288>

Type definitions:

```schema
type USER {
USERID: String
COMPANYID: String
COMPANY: [COMPANY] @relationship(type: "IS_PART_OF", direction: OUT)
}

type COMPANY {
USERS: [USER] @relationship(type: "IS_PART_OF", direction: IN)
}
```

---

### Can create a USER and COMPANYID is populated

**GraphQL input**

```graphql
mutation {
createUSERS(input: { USERID: "userid", COMPANYID: "companyid" }) {
users {
USERID
COMPANYID
}
}
}
```

**Expected Cypher output**

```cypher
CALL {
CREATE (this0:USER)
SET this0.USERID = $this0_USERID
SET this0.COMPANYID = $this0_COMPANYID
RETURN this0
}

RETURN
this0 { .USERID, .COMPANYID } AS this0
```

**Expected Cypher params**

```cypher-params
{
"this0_USERID": "userid",
"this0_COMPANYID": "companyid"
}
```

---

### Can update a USER and COMPANYID is populated

**GraphQL input**

```graphql
mutation {
updateUSERS(
where: { USERID: "userid" }
update: { COMPANYID: "companyid2" }
) {
users {
USERID
COMPANYID
}
}
}
```

**Expected Cypher output**

```cypher
MATCH (this:USER)
WHERE this.USERID = $this_USERID

SET this.COMPANYID = $this_update_COMPANYID

RETURN this { .USERID, .COMPANYID } AS this
```

**Expected Cypher params**

```cypher-params
{
"this_USERID": "userid",
"this_update_COMPANYID": "companyid2"
}
```

---
Loading