-
Notifications
You must be signed in to change notification settings - Fork 160
Delete Mutation #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
10bfb8d
test: add test cases for delete mutation
danstarns 85c9f80
feat: add delete mutation
danstarns a3c1e33
Merge branch 'master' into delete-mutation
danstarns fb36e77
refactor: merge changes remove trimmer
danstarns 09f9959
Merge branch 'master' into delete-mutation
danstarns 738cd65
Merge branch 'master' into delete-mutation
danstarns c22b13d
Merge branch 'master' into delete-mutation
danstarns d4a9a6f
Merge branch 'master' into delete-mutation
danstarns 180b89b
Merge branch 'master' into delete-mutation
danstarns 0b280f2
refactor: consistant resolver naming
danstarns 8363cc3
test: add coverage for negative path
danstarns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { GraphQLResolveInfo } from "graphql"; | ||
| import { execute } from "../utils"; | ||
| import { translate } from "../translate"; | ||
| import { NeoSchema, Node } from "../classes"; | ||
|
|
||
| function deleteResolver({ node, getSchema }: { node: Node; getSchema: () => NeoSchema }) { | ||
| async function resolve(_root: any, _args: any, context: any, resolveInfo: GraphQLResolveInfo) { | ||
| const neoSchema = getSchema(); | ||
| context.neoSchema = neoSchema; | ||
|
|
||
| const { driver } = context; | ||
| if (!driver) { | ||
| throw new Error("context.driver missing"); | ||
| } | ||
|
|
||
| const [cypher, params] = translate({ context, resolveInfo }); | ||
|
|
||
| const result = await execute({ | ||
| cypher, | ||
| params, | ||
| driver, | ||
| defaultAccessMode: "WRITE", | ||
| neoSchema, | ||
| statistics: true, | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| return { | ||
| type: `DeleteInfo!`, | ||
| resolve, | ||
| args: { where: `${node.name}Where` }, | ||
| }; | ||
| } | ||
|
|
||
| export default deleteResolver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { Driver } from "neo4j-driver"; | ||
| import { graphql } from "graphql"; | ||
| import { generate } from "randomstring"; | ||
| import neo4j from "./neo4j"; | ||
| import makeAugmentedSchema from "../../src/schema/make-augmented-schema"; | ||
|
|
||
| describe("delete", () => { | ||
| let driver: Driver; | ||
|
|
||
| beforeAll(async () => { | ||
| driver = await neo4j(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await driver.close(); | ||
| }); | ||
|
|
||
| test("should delete a single movie", async () => { | ||
| const session = driver.session(); | ||
|
|
||
| const typeDefs = ` | ||
| type Movie { | ||
| id: ID! | ||
| } | ||
| `; | ||
|
|
||
| const neoSchema = makeAugmentedSchema({ typeDefs }); | ||
|
|
||
| const id = generate({ | ||
| charset: "alphabetic", | ||
| }); | ||
|
|
||
| const mutation = ` | ||
| mutation($id: ID!) { | ||
| deleteMovies(where: { id: $id }) { | ||
| nodesDeleted | ||
| relationshipsDeleted | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| try { | ||
| await session.run( | ||
| ` | ||
| CREATE (:Movie {id: $id}) | ||
| `, | ||
| { id } | ||
| ); | ||
|
|
||
| const gqlResult = await graphql({ | ||
| schema: neoSchema.schema, | ||
| source: mutation, | ||
| variableValues: { id }, | ||
| contextValue: { driver }, | ||
| }); | ||
|
|
||
| expect(gqlResult.errors).toBeFalsy(); | ||
|
|
||
| expect(gqlResult?.data?.deleteMovies).toEqual({ nodesDeleted: 1, relationshipsDeleted: 0 }); | ||
|
|
||
| const reFind = await session.run( | ||
| ` | ||
| MATCH (m:Movie {id: $id}) | ||
| RETURN m | ||
| `, | ||
| { id } | ||
| ); | ||
|
|
||
| expect(reFind.records.length).toEqual(0); | ||
| } finally { | ||
| await session.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("should not delete a movie if predicate does not yield true", async () => { | ||
| const session = driver.session(); | ||
|
|
||
| const typeDefs = ` | ||
| type Movie { | ||
| id: ID! | ||
| } | ||
| `; | ||
|
|
||
| const neoSchema = makeAugmentedSchema({ typeDefs }); | ||
|
|
||
| const id = generate({ | ||
| charset: "alphabetic", | ||
| }); | ||
|
|
||
| const mutation = ` | ||
| mutation($id: ID!) { | ||
| deleteMovies(where: { id: $id }) { | ||
| nodesDeleted | ||
| relationshipsDeleted | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| try { | ||
| await session.run( | ||
| ` | ||
| CREATE (:Movie {id: $id}) | ||
| `, | ||
| { id } | ||
| ); | ||
|
|
||
| const gqlResult = await graphql({ | ||
| schema: neoSchema.schema, | ||
| source: mutation, | ||
| variableValues: { id: "NOT FOUND" }, | ||
| contextValue: { driver }, | ||
| }); | ||
|
|
||
| expect(gqlResult.errors).toBeFalsy(); | ||
|
|
||
| expect(gqlResult?.data?.deleteMovies).toEqual({ nodesDeleted: 0, relationshipsDeleted: 0 }); | ||
|
|
||
| const reFind = await session.run( | ||
| ` | ||
| MATCH (m:Movie {id: $id}) | ||
| RETURN m | ||
| `, | ||
| { id } | ||
| ); | ||
|
|
||
| expect(reFind.records.length).toEqual(1); | ||
| } finally { | ||
| await session.close(); | ||
| } | ||
| }); | ||
| }); | ||
43 changes: 43 additions & 0 deletions
43
packages/graphql/tests/tck/tck-test-files/cypher-delete.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ## Cypher Delete | ||
|
|
||
| Tests delete operations. | ||
|
|
||
| Schema: | ||
|
|
||
| ```schema | ||
| type Movie { | ||
| id: ID | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### Simple Delete | ||
|
|
||
| **GraphQL input** | ||
|
|
||
| ```graphql | ||
| mutation { | ||
| deleteMovies(where: {id: "123"}) { | ||
| nodesDeleted | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Expected Cypher output** | ||
|
|
||
| ```cypher | ||
| MATCH (this:Movie) | ||
| WHERE this.id = $this_id | ||
| DETACH DELETE this | ||
| ``` | ||
|
|
||
| **Expected Cypher params** | ||
|
|
||
| ```cypher-params | ||
| { | ||
| "this_id": "123" | ||
| } | ||
| ``` | ||
|
|
||
| --- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.