Skip to content

Commit

Permalink
Merge branch 'JakeStanger-feat/advanced-query' into version-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Julie Turner committed Jul 15, 2024
2 parents c8195fc + 1ec3bad commit efec1c4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
15 changes: 15 additions & 0 deletions docs/graph/behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')")();
```
14 changes: 14 additions & 0 deletions packages/graph/behaviors/advanced-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TimelinePipe } from "@pnp/core";
import { Queryable } from "@pnp/queryable";
import { ConsistencyLevel } from "@pnp/graph";

export function AdvancedQuery(): TimelinePipe<Queryable> {

return (instance: Queryable) => {

instance.using(ConsistencyLevel());
instance.query.set("$count", "true");

return instance;
};
}
1 change: 1 addition & 0 deletions packages/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
25 changes: 24 additions & 1 deletion test/graph/query-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

Expand Down Expand Up @@ -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;
}));
});
});

0 comments on commit efec1c4

Please sign in to comment.