-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: add init coverage for count * feat: add count queries * test: changes to existing for count queries * docs: add count queries * docs: remove misleading comment on count * refactor: change to xCount over countX * feat: add count to ogm
- Loading branch information
Showing
48 changed files
with
749 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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,20 @@ | ||
[[ogm-methods-count]] | ||
= Count | ||
|
||
Use to count nodes. | ||
|
||
== Usage | ||
|
||
=== Basic | ||
|
||
[source, javascript] | ||
---- | ||
const User = ogm.model("User"); | ||
const usersCount = await User.count(); | ||
---- | ||
|
||
== Args | ||
|
||
=== `where` | ||
JavaScript object representation of the GraphQL `where` input type, used for <<schema-queries>>. |
This file contains 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 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 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 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 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 @@ | ||
/* | ||
* 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 { Node } from "../../classes"; | ||
import countResolver from "./count"; | ||
|
||
describe("Count resolver", () => { | ||
test("should return the correct; type, args and resolve", () => { | ||
// @ts-ignore | ||
const node: Node = { | ||
name: "Movie", | ||
}; | ||
|
||
const result = countResolver({ node }); | ||
expect(result.type).toEqual("Int!"); | ||
expect(result.resolve).toBeInstanceOf(Function); | ||
expect(result.args).toMatchObject({ | ||
where: "MovieWhere", | ||
}); | ||
}); | ||
}); |
This file contains 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,45 @@ | ||
/* | ||
* 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 { execute } from "../../utils"; | ||
import { translateCount } from "../../translate"; | ||
import { Node } from "../../classes"; | ||
import { Context } from "../../types"; | ||
|
||
export default function countResolver({ node }: { node: Node }) { | ||
async function resolve(_root: any, _args: any, _context: unknown) { | ||
const context = _context as Context; | ||
const [cypher, params] = translateCount({ context, node }); | ||
|
||
const result = await execute({ | ||
cypher, | ||
params, | ||
defaultAccessMode: "READ", | ||
context, | ||
raw: true, | ||
}); | ||
|
||
return result.records[0]._fields[0].toNumber(); | ||
} | ||
|
||
return { | ||
type: `Int!`, | ||
resolve, | ||
args: { where: `${node.name}Where` }, | ||
}; | ||
} |
This file contains 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 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 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,83 @@ | ||
/* | ||
* 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 { Node } from "../classes"; | ||
import { AUTH_FORBIDDEN_ERROR } from "../constants"; | ||
import { Context, GraphQLWhereArg } from "../types"; | ||
import createAuthAndParams from "./create-auth-and-params"; | ||
import createWhereAndParams from "./create-where-and-params"; | ||
|
||
function translateCount({ node, context }: { node: Node; context: Context }): [string, any] { | ||
const whereInput = context.resolveTree.args.where as GraphQLWhereArg; | ||
const varName = "this"; | ||
let cypherParams: { [k: string]: any } = {}; | ||
const whereStrs: string[] = []; | ||
const cypherStrs: string[] = []; | ||
|
||
cypherStrs.push(`MATCH (${varName}:${node.name})`); | ||
|
||
if (whereInput) { | ||
const where = createWhereAndParams({ | ||
whereInput, | ||
varName, | ||
node, | ||
context, | ||
recursing: true, | ||
}); | ||
if (where[0]) { | ||
whereStrs.push(where[0]); | ||
cypherParams = { ...cypherParams, ...where[1] }; | ||
} | ||
} | ||
|
||
const whereAuth = createAuthAndParams({ | ||
operation: "READ", | ||
entity: node, | ||
context, | ||
where: { varName, node }, | ||
}); | ||
if (whereAuth[0]) { | ||
whereStrs.push(whereAuth[0]); | ||
cypherParams = { ...cypherParams, ...whereAuth[1] }; | ||
} | ||
|
||
const allowAuth = createAuthAndParams({ | ||
operation: "READ", | ||
entity: node, | ||
context, | ||
allow: { | ||
parentNode: node, | ||
varName, | ||
}, | ||
}); | ||
if (allowAuth[0]) { | ||
cypherParams = { ...cypherParams, ...allowAuth[1] }; | ||
cypherStrs.push(`CALL apoc.util.validate(NOT(${allowAuth[0]}), "${AUTH_FORBIDDEN_ERROR}", [0])`); | ||
} | ||
|
||
if (whereStrs.length) { | ||
cypherStrs.push(`WHERE ${whereStrs.join(" AND ")}`); | ||
} | ||
|
||
cypherStrs.push(`RETURN count(${varName})`); | ||
|
||
return [cypherStrs.filter(Boolean).join("\n"), cypherParams]; | ||
} | ||
|
||
export default translateCount; |
Oops, something went wrong.