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

Merge 1.2.1 changes from master into 2.0.0 #319

Merged
merged 7 commits into from
Jul 14, 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ This is a TypeScript Monorepo managed with [Yarn Workspaces](https://classic.yar

1. [`@neo4j/graphql`](./packages/graphql) - Familiar GraphQL generation, for usage with an API such as [Apollo Server](https://www.apollographql.com/docs/apollo-server/)
2. [`@neo4j/graphql-ogm`](./packages/ogm) - Use GraphQL Type Definitions to drive interactions with the database

## Media

Blogs, talks and other content surrounding Neo4j GraphQL. Sign up for [NODES 2021](https://neo4j.brand.live/c/2021nodes-live) to view even more Neo4j GraphQL content.

1. [Neo4j and GraphQL The Past, Present and Future](https://youtu.be/sZ-eBznM71M)
2. [Securing Your Graph With Neo4j GraphQL](https://medium.com/neo4j/securing-your-graph-with-neo4j-graphql-91a2d7b08631)
3. [Best Practices For Using Cypher With GraphQL](https://youtu.be/YceBpk01Gxs)
4. [Migrating To The Official Neo4j GraphQL Library](https://youtu.be/4_rp1ikvFKc)
5. [Announcing the Stable Release of the Official Neo4j GraphQL Library 1.0.0](https://medium.com/neo4j/announcing-the-stable-release-of-the-official-neo4j-graphql-library-1-0-0-6cdd30cd40b)
6. [Announcing the Neo4j GraphQL Library Beta Release](https://medium.com/neo4j/announcing-the-neo4j-graphql-library-beta-99ae8541bbe7)
2 changes: 1 addition & 1 deletion packages/graphql/src/schema/resolvers/cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function cypherResolver({
) as { strs: string[]; params: any };
const apocParamsStr = `{${apocParams.strs.length ? `${apocParams.strs.join(", ")}` : ""}}`;

const expectMultipleValues = referenceNode && field.typeMeta.array ? "true" : "false";
const expectMultipleValues = field.typeMeta.array ? "true" : "false";
if (type === "Query") {
cypherStrs.push(`
WITH apoc.cypher.runFirstColumn("${statement}", ${apocParamsStr}, ${expectMultipleValues}) as x
Expand Down
94 changes: 93 additions & 1 deletion packages/graphql/tests/integration/cypher.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,98 @@ describe("cypher", () => {
await session.close();
}
});

test("should query multiple nodes and return relationship data", async () => {
const session = driver.session();

const movieTitle1 = generate({
charset: "alphabetic",
});
const movieTitle2 = generate({
charset: "alphabetic",
});
const movieTitle3 = generate({
charset: "alphabetic",
});

const actorName = generate({
charset: "alphabetic",
});

const typeDefs = `
type Movie {
title: String!
actors: [Actor] @relationship(type: "ACTED_IN", direction: IN)
}

type Actor {
name: String!
movies: [Movie] @relationship(type: "ACTED_IN", direction: OUT)
}

type Query {
customMovies(titles: [String!]!): [Movie] @cypher(statement: """
MATCH (m:Movie)
WHERE m.title in $titles
RETURN m
""")
}
`;

const neoSchema = new Neo4jGraphQL({ typeDefs });

const source = `
query($titles: [String!]!) {
customMovies(titles: $titles) {
title
actors {
name
}
}
}
`;

try {
await session.run(
`
CREATE (:Movie {title: $title1})<-[:ACTED_IN]-(a:Actor {name: $name})
CREATE (:Movie {title: $title2})<-[:ACTED_IN]-(a)
CREATE (:Movie {title: $title3})<-[:ACTED_IN]-(a)
`,
{
title1: movieTitle1,
title2: movieTitle2,
title3: movieTitle3,
name: actorName,
}
);

const gqlResult = await graphql({
schema: neoSchema.schema,
source,
contextValue: { driver },
variableValues: { titles: [movieTitle1, movieTitle2, movieTitle3] },
});

expect(gqlResult.errors).toBeFalsy();

expect((gqlResult?.data as any).customMovies).toHaveLength(3);
expect((gqlResult?.data as any).customMovies).toContainEqual({
title: movieTitle1,
actors: [{ name: actorName }],
});
expect((gqlResult?.data as any).customMovies).toContainEqual({
title: movieTitle2,
actors: [{ name: actorName }],
});
expect((gqlResult?.data as any).customMovies).toContainEqual({
title: movieTitle3,
actors: [{ name: actorName }],
});
} finally {
await session.close();
}
});
});

describe("Mutation", () => {
Expand Down Expand Up @@ -511,7 +603,7 @@ describe("cypher", () => {
gender
}
}
}
}
`;

try {
Expand Down
246 changes: 246 additions & 0 deletions packages/graphql/tests/integration/issues/#315.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/*
* 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/315", () => {
let driver: Driver;
const typeDefs = gql`
union Content = Post

type Post {
content: String!
modifiedDate: DateTime! @timestamp(operations: [CREATE, UPDATE])
}

type User {
id: ID!
friends: [User!]! @relationship(type: "HAS_FRIEND", direction: OUT)
posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT)
}

type Query {
getContent(userID: ID): [Content]
@cypher(
statement: """
MATCH (myUser:User {id: $userID})
OPTIONAL MATCH (myUser)-[:HAS_FRIEND]->(myFriends:User)
CALL {
WITH myUser, myFriends
MATCH (myUser)-[:HAS_POST]->(post:Post)
RETURN post
UNION
WITH myUser, myFriends
MATCH (myFriends)-[:HAS_POST]->(post:Post)
RETURN post
}
RETURN DISTINCT apoc.map.merge(properties(post), { __resolveType: 'Post' }) AS result ORDER BY result.modifiedDate DESC
"""
)
}
`;

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

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

test("multiple entries are returned for custom cypher", async () => {
const session = driver.session();

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

const userID = generate({ charset: "alphabetic" });

const input = [
{
id: userID,
friends: {
create: [
{
node: {
id: generate({ charset: "alphabetic" }),
posts: {
create: [
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
],
},
},
},
{
node: {
id: generate({ charset: "alphabetic" }),
posts: {
create: [
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
],
},
},
},
{
node: {
id: generate({ charset: "alphabetic" }),
posts: {
create: [
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
],
},
},
},
],
},
posts: {
create: [
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
{
node: {
content: generate({ charset: "alphabetic" }),
},
},
],
},
},
];

const mutation = `
mutation CreateUsers($input: [UserCreateInput!]!) {
createUsers(input: $input) {
users {
id
friends {
id
posts {
content
}
}
posts {
content
}
}
}
}
`;

const query = `
query GetContent($userID: ID) {
getContent(userID: $userID) {
__typename
... on Post {
content
}
}
}
`;

try {
await neoSchema.checkNeo4jCompat();

const mutationResult = await graphql({
schema: neoSchema.schema,
source: mutation,
contextValue: { driver },
variableValues: { input },
});

expect(mutationResult.errors).toBeFalsy();

expect(mutationResult?.data?.createUsers?.users[0].id).toEqual(userID);
expect(mutationResult?.data?.createUsers?.users[0].friends).toHaveLength(3);
expect(mutationResult?.data?.createUsers?.users[0].posts).toHaveLength(3);

mutationResult?.data?.createUsers?.users[0].friends.forEach((friend) => {
expect(friend.posts).toHaveLength(3);
});

const queryResult = await graphql({
schema: neoSchema.schema,
source: query,
contextValue: { driver },
variableValues: {
userID,
},
});

expect(queryResult.errors).toBeFalsy();

expect(queryResult?.data?.getContent).toHaveLength(12);
} finally {
await session.close();
}
});
});