Skip to content

Commit

Permalink
Merge branch 'fix/subscriptions-context-typings' of github.com:darrel…
Browse files Browse the repository at this point in the history
…lwarde/graphql into fix/subscriptions-context-typings
  • Loading branch information
darrellwarde committed Aug 21, 2023
2 parents 41919e0 + 4f7f8ce commit 003f86a
Show file tree
Hide file tree
Showing 22 changed files with 244 additions and 269 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-adults-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/graphql": major
---

The `limit` argument of the `@queryOptions` directive has been moved to its own directive, `@limit`.
67 changes: 67 additions & 0 deletions packages/graphql/src/classes/LimitDirective.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 * as neo4j from "neo4j-driver";
import { LimitDirective } from "./LimitDirective";

describe("QueryOptionsDirective", () => {
describe("getLimit", () => {
test("should return default limit", () => {
const limit = new LimitDirective({
default: neo4j.int(5),
max: neo4j.int(8),
});

expect(limit.getLimit()).toEqual(neo4j.int(5));
});

test("should return max limit if default is not available", () => {
const limit = new LimitDirective({
max: neo4j.int(8),
});

expect(limit.getLimit()).toEqual(neo4j.int(8));
});

test("should override default limit", () => {
const limit = new LimitDirective({
default: neo4j.int(5),
max: neo4j.int(8),
});

expect(limit.getLimit(neo4j.int(2))).toEqual(neo4j.int(2));
expect(limit.getLimit(neo4j.int(6))).toEqual(neo4j.int(6));
});

test("should return if a higher one is given max limit if a higher one is given", () => {
const limit = new LimitDirective({
default: neo4j.int(5),
max: neo4j.int(8),
});

expect(limit.getLimit(neo4j.int(16))).toEqual(neo4j.int(8));
});

test("should return undefined if no limit is given", () => {
const limit = new LimitDirective({});

expect(limit.getLimit()).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@
import * as neo4j from "neo4j-driver";
import type { Integer } from "neo4j-driver";

type QueryOptionsDirectiveConstructor = {
limit: {
default?: Integer;
max?: Integer;
};
type LimitDirectiveConstructor = {
default?: Integer;
max?: Integer;
};

export class QueryOptionsDirective {
private limit: QueryOptionsDirectiveConstructor["limit"];
export class LimitDirective {
private default?: Integer;
private max?: Integer;

constructor(args: QueryOptionsDirectiveConstructor) {
this.limit = args.limit;
constructor(limit: LimitDirectiveConstructor) {
this.default = limit.default;
this.max = limit.max;
}

public getLimit(optionsLimit?: Integer | number): Integer | undefined {
if (optionsLimit) {
const integerLimit = neo4j.int(optionsLimit);
if (this.limit.max && integerLimit.greaterThan(this.limit.max)) {
return this.limit.max;
if (this.max && integerLimit.greaterThan(this.max)) {
return this.max;
}
return integerLimit;
}

return this.limit.default || this.limit.max;
return this.default || this.max;
}
}
8 changes: 4 additions & 4 deletions packages/graphql/src/classes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import type Exclude from "./Exclude";
import type { GraphElementConstructor } from "./GraphElement";
import { GraphElement } from "./GraphElement";
import type { NodeDirective } from "./NodeDirective";
import type { QueryOptionsDirective } from "./QueryOptionsDirective";
import type { LimitDirective } from "./LimitDirective";
import type { SchemaConfiguration } from "../schema/schema-configuration";
import { leadingUnderscores } from "../utils/leading-underscore";
import type { Neo4jGraphQLContext } from "../types/neo4j-graphql-context";
Expand All @@ -68,7 +68,7 @@ export interface NodeConstructor extends GraphElementConstructor {
schemaConfiguration?: SchemaConfiguration;
nodeDirective?: NodeDirective;
description?: string;
queryOptionsDirective?: QueryOptionsDirective;
limitDirective?: LimitDirective;
isGlobalNode?: boolean;
globalIdField?: string;
globalIdFieldIsInt?: boolean;
Expand Down Expand Up @@ -134,7 +134,7 @@ class Node extends GraphElement {
public nodeDirective?: NodeDirective;
public fulltextDirective?: FullText;
public description?: string;
public queryOptions?: QueryOptionsDirective;
public limit?: LimitDirective;
public singular: string;
public plural: string;
public isGlobalNode: boolean | undefined;
Expand All @@ -156,7 +156,7 @@ class Node extends GraphElement {
this.schemaConfiguration = input.schemaConfiguration;
this.nodeDirective = input.nodeDirective;
this.fulltextDirective = input.fulltextDirective;
this.queryOptions = input.queryOptionsDirective;
this.limit = input.limitDirective;
this.isGlobalNode = input.isGlobalNode;
this._idField = input.globalIdField;
this._idFieldIsInt = input.globalIdFieldIsInt;
Expand Down
77 changes: 0 additions & 77 deletions packages/graphql/src/classes/QueryOptionsDirective.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/graphql/src/graphql/directives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export { nodeDirective } from "./node";
export { pluralDirective } from "./plural";
export { populatedByDirective } from "./populatedBy";
export { privateDirective } from "./private";
export { queryOptionsDirective } from "./query-options";
export { limitDirective } from "./limit";
export { readonlyDirective } from "./readonly";
export { relationshipPropertiesDirective } from "./relationship-properties";
export { relationshipDirective } from "./relationship";
Expand Down
36 changes: 36 additions & 0 deletions packages/graphql/src/graphql/directives/limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { DirectiveLocation, GraphQLDirective, GraphQLInt } from "graphql";

export const limitDirective = new GraphQLDirective({
name: "limit",
description: "Instructs @neo4j/graphql to inject limit values into a query.",
args: {
default: {
description: "If no limit argument is supplied on query will fallback to this value.",
type: GraphQLInt,
},
max: {
description: "Maximum limit to be used for queries.",
type: GraphQLInt,
},
},
locations: [DirectiveLocation.OBJECT],
});
44 changes: 0 additions & 44 deletions packages/graphql/src/graphql/directives/query-options.ts

This file was deleted.

11 changes: 5 additions & 6 deletions packages/graphql/src/schema-model/annotation/Annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { PluralAnnotation } from "./PluralAnnotation";
import { PopulatedByAnnotation } from "./PopulatedByAnnotation";
import { PrivateAnnotation } from "./PrivateAnnotation";
import { QueryAnnotation } from "./QueryAnnotation";
import { QueryOptionsAnnotation } from "./QueryOptionsAnnotation";
import { LimitAnnotation } from "./LimitAnnotation";
import { SelectableAnnotation } from "./SelectableAnnotation";
import { SettableAnnotation } from "./SettableAnnotation";
import { SubscriptionAnnotation } from "./SubscriptionAnnotation";
Expand All @@ -48,7 +48,7 @@ export type Annotation =
| AuthenticationAnnotation
| KeyAnnotation
| SubscriptionsAuthorizationAnnotation
| QueryOptionsAnnotation
| LimitAnnotation
| DefaultAnnotation
| CoalesceAnnotation
| CustomResolverAnnotation
Expand All @@ -68,7 +68,6 @@ export type Annotation =
| JWTClaimAnnotation
| JWTPayloadAnnotation;


export enum AnnotationsKey {
authentication = "authentication",
authorization = "authorization",
Expand All @@ -82,12 +81,12 @@ export enum AnnotationsKey {
jwtClaim = "jwtClaim",
jwtPayload = "jwtPayload",
key = "key",
limit = "limit",
mutation = "mutation",
plural = "plural",
populatedBy = "populatedBy",
private = "private",
query = "query",
queryOptions = "queryOptions",
selectable = "selectable",
settable = "settable",
subscription = "subscription",
Expand All @@ -102,7 +101,7 @@ export type Annotations = {
[AnnotationsKey.authentication]: AuthenticationAnnotation;
[AnnotationsKey.key]: KeyAnnotation;
[AnnotationsKey.subscriptionsAuthorization]: SubscriptionsAuthorizationAnnotation;
[AnnotationsKey.queryOptions]: QueryOptionsAnnotation;
[AnnotationsKey.limit]: LimitAnnotation;
[AnnotationsKey.default]: DefaultAnnotation;
[AnnotationsKey.coalesce]: CoalesceAnnotation;
[AnnotationsKey.customResolver]: CustomResolverAnnotation;
Expand All @@ -129,7 +128,7 @@ export function annotationToKey(ann: Annotation): keyof Annotations {
if (ann instanceof AuthenticationAnnotation) return AnnotationsKey.authentication;
if (ann instanceof KeyAnnotation) return AnnotationsKey.key;
if (ann instanceof SubscriptionsAuthorizationAnnotation) return AnnotationsKey.subscriptionsAuthorization;
if (ann instanceof QueryOptionsAnnotation) return AnnotationsKey.queryOptions;
if (ann instanceof LimitAnnotation) return AnnotationsKey.limit;
if (ann instanceof DefaultAnnotation) return AnnotationsKey.default;
if (ann instanceof CoalesceAnnotation) return AnnotationsKey.coalesce;
if (ann instanceof CustomResolverAnnotation) return AnnotationsKey.customResolver;
Expand Down
Loading

0 comments on commit 003f86a

Please sign in to comment.