From 5bfebcd1c8cb69ecb469955c8223466b833dd89a 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 ++++++++++++++ 2 files changed, 29 insertions(+) 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; + }; +}