From 34500c332cfb00b494156cfebf221409a57c185c Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Thu, 20 Jun 2024 11:07:06 +0100 Subject: [PATCH] feat(graph): add advanced query behaviour --- docs/graph/behaviors.md | 15 +++++++++++++ packages/graph/behaviors/advanced-query.ts | 14 ++++++++++++ packages/graph/index.ts | 1 + test/graph/query-params.ts | 25 +++++++++++++++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 packages/graph/behaviors/advanced-query.ts diff --git a/docs/graph/behaviors.md b/docs/graph/behaviors.md index cbdbcb670..80366e001 100644 --- a/docs/graph/behaviors.md +++ b/docs/graph/behaviors.md @@ -194,3 +194,18 @@ const graph = graphfi().using(ConsistencyLevel("{level value}")); await graph.users(); ``` + +## AdvancedQuery + +Using this behaviour, you can enable [advanced query capabilities](https://learn.microsoft.com/en-us/graph/aad-advanced-queries?tabs=http) when filtering supported collections. + +This sets the consistency level to eventual and enables the `$count` query parameter. + +```TypeScript +import { graphfi, AdvancedQuery } from "@pnp/graph"; +import "@pnp/graph/users"; + +const graph = graphfi().using(AdvancedQuery()); + +await graph.users.filter("companyName ne null and NOT(companyName eq 'Microsoft')")(); +``` diff --git a/packages/graph/behaviors/advanced-query.ts b/packages/graph/behaviors/advanced-query.ts new file mode 100644 index 000000000..975195e10 --- /dev/null +++ b/packages/graph/behaviors/advanced-query.ts @@ -0,0 +1,14 @@ +import { TimelinePipe } from "@pnp/core"; +import { Queryable } from "@pnp/queryable"; +import { ConsistencyLevel } from "@pnp/graph"; + +export function AdvancedQuery(): TimelinePipe { + + return (instance: Queryable) => { + + instance.using(ConsistencyLevel()); + instance.query.set("$count", "true"); + + return instance; + }; +} diff --git a/packages/graph/index.ts b/packages/graph/index.ts index d57ce90e8..fd1be6296 100644 --- a/packages/graph/index.ts +++ b/packages/graph/index.ts @@ -2,6 +2,7 @@ export { graphfi as graphfi, GraphFI as GraphFI } from "./fi.js"; export * from "./graphqueryable.js"; +export * from "./behaviors/advanced-query.js"; export * from "./behaviors/consistency-level.js"; export * from "./behaviors/defaults.js"; export * from "./behaviors/endpoint.js"; diff --git a/test/graph/query-params.ts b/test/graph/query-params.ts index 1c2b0e824..609794181 100644 --- a/test/graph/query-params.ts +++ b/test/graph/query-params.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import { pnpTest } from "../pnp-test.js"; import "@pnp/graph/groups"; import "@pnp/graph/users"; -import { ConsistencyLevel } from "@pnp/graph/index.js"; +import { ConsistencyLevel, AdvancedQuery } from "@pnp/graph/index.js"; describe("Graph Query Params", function () { @@ -41,4 +41,27 @@ describe("Graph Query Params", function () { return expect(query()).to.eventually.be.fulfilled; })); + + describe("AdvancedQuery", () => { + it("NOT groupTypes/any(c:c eq 'Unified')", pnpTest("d24d9b36-d5dc-4a6c-81fa-2e9a73911372", async function () { + + const query = this.pnp.graph.groups.using(AdvancedQuery()).filter("NOT groupTypes/any(c:c eq 'Unified')"); + + return expect(query()).to.eventually.be.fulfilled; + })); + + it("companyName ne null and NOT(companyName eq 'Microsoft')", pnpTest("33791988-de36-4a6d-88e1-23f6838236ac", async function () { + + const query = this.pnp.graph.users.using(AdvancedQuery()).filter("companyName ne null and NOT(companyName eq 'Microsoft')"); + + return expect(query()).to.eventually.be.fulfilled; + })); + + it("not(assignedLicenses/$count eq 0)", pnpTest("fe202c37-e10e-4b1c-b410-99cf059a491b", async function () { + + const query = this.pnp.graph.users.using(AdvancedQuery()).filter("not(assignedLicenses/$count eq 0)"); + + return expect(query()).to.eventually.be.fulfilled; + })); + }); });