Skip to content

Commit 003f86a

Browse files
committed
Merge branch 'fix/subscriptions-context-typings' of github.com:darrellwarde/graphql into fix/subscriptions-context-typings
2 parents 41919e0 + 4f7f8ce commit 003f86a

22 files changed

+244
-269
lines changed

.changeset/neat-adults-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@neo4j/graphql": major
3+
---
4+
5+
The `limit` argument of the `@queryOptions` directive has been moved to its own directive, `@limit`.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import * as neo4j from "neo4j-driver";
21+
import { LimitDirective } from "./LimitDirective";
22+
23+
describe("QueryOptionsDirective", () => {
24+
describe("getLimit", () => {
25+
test("should return default limit", () => {
26+
const limit = new LimitDirective({
27+
default: neo4j.int(5),
28+
max: neo4j.int(8),
29+
});
30+
31+
expect(limit.getLimit()).toEqual(neo4j.int(5));
32+
});
33+
34+
test("should return max limit if default is not available", () => {
35+
const limit = new LimitDirective({
36+
max: neo4j.int(8),
37+
});
38+
39+
expect(limit.getLimit()).toEqual(neo4j.int(8));
40+
});
41+
42+
test("should override default limit", () => {
43+
const limit = new LimitDirective({
44+
default: neo4j.int(5),
45+
max: neo4j.int(8),
46+
});
47+
48+
expect(limit.getLimit(neo4j.int(2))).toEqual(neo4j.int(2));
49+
expect(limit.getLimit(neo4j.int(6))).toEqual(neo4j.int(6));
50+
});
51+
52+
test("should return if a higher one is given max limit if a higher one is given", () => {
53+
const limit = new LimitDirective({
54+
default: neo4j.int(5),
55+
max: neo4j.int(8),
56+
});
57+
58+
expect(limit.getLimit(neo4j.int(16))).toEqual(neo4j.int(8));
59+
});
60+
61+
test("should return undefined if no limit is given", () => {
62+
const limit = new LimitDirective({});
63+
64+
expect(limit.getLimit()).toBeUndefined();
65+
});
66+
});
67+
});

packages/graphql/src/classes/QueryOptionsDirective.ts renamed to packages/graphql/src/classes/LimitDirective.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@
2020
import * as neo4j from "neo4j-driver";
2121
import type { Integer } from "neo4j-driver";
2222

23-
type QueryOptionsDirectiveConstructor = {
24-
limit: {
25-
default?: Integer;
26-
max?: Integer;
27-
};
23+
type LimitDirectiveConstructor = {
24+
default?: Integer;
25+
max?: Integer;
2826
};
2927

30-
export class QueryOptionsDirective {
31-
private limit: QueryOptionsDirectiveConstructor["limit"];
28+
export class LimitDirective {
29+
private default?: Integer;
30+
private max?: Integer;
3231

33-
constructor(args: QueryOptionsDirectiveConstructor) {
34-
this.limit = args.limit;
32+
constructor(limit: LimitDirectiveConstructor) {
33+
this.default = limit.default;
34+
this.max = limit.max;
3535
}
3636

3737
public getLimit(optionsLimit?: Integer | number): Integer | undefined {
3838
if (optionsLimit) {
3939
const integerLimit = neo4j.int(optionsLimit);
40-
if (this.limit.max && integerLimit.greaterThan(this.limit.max)) {
41-
return this.limit.max;
40+
if (this.max && integerLimit.greaterThan(this.max)) {
41+
return this.max;
4242
}
4343
return integerLimit;
4444
}
4545

46-
return this.limit.default || this.limit.max;
46+
return this.default || this.max;
4747
}
4848
}

packages/graphql/src/classes/Node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import type Exclude from "./Exclude";
4141
import type { GraphElementConstructor } from "./GraphElement";
4242
import { GraphElement } from "./GraphElement";
4343
import type { NodeDirective } from "./NodeDirective";
44-
import type { QueryOptionsDirective } from "./QueryOptionsDirective";
44+
import type { LimitDirective } from "./LimitDirective";
4545
import type { SchemaConfiguration } from "../schema/schema-configuration";
4646
import { leadingUnderscores } from "../utils/leading-underscore";
4747
import type { Neo4jGraphQLContext } from "../types/neo4j-graphql-context";
@@ -68,7 +68,7 @@ export interface NodeConstructor extends GraphElementConstructor {
6868
schemaConfiguration?: SchemaConfiguration;
6969
nodeDirective?: NodeDirective;
7070
description?: string;
71-
queryOptionsDirective?: QueryOptionsDirective;
71+
limitDirective?: LimitDirective;
7272
isGlobalNode?: boolean;
7373
globalIdField?: string;
7474
globalIdFieldIsInt?: boolean;
@@ -134,7 +134,7 @@ class Node extends GraphElement {
134134
public nodeDirective?: NodeDirective;
135135
public fulltextDirective?: FullText;
136136
public description?: string;
137-
public queryOptions?: QueryOptionsDirective;
137+
public limit?: LimitDirective;
138138
public singular: string;
139139
public plural: string;
140140
public isGlobalNode: boolean | undefined;
@@ -156,7 +156,7 @@ class Node extends GraphElement {
156156
this.schemaConfiguration = input.schemaConfiguration;
157157
this.nodeDirective = input.nodeDirective;
158158
this.fulltextDirective = input.fulltextDirective;
159-
this.queryOptions = input.queryOptionsDirective;
159+
this.limit = input.limitDirective;
160160
this.isGlobalNode = input.isGlobalNode;
161161
this._idField = input.globalIdField;
162162
this._idFieldIsInt = input.globalIdFieldIsInt;

packages/graphql/src/classes/QueryOptionsDirective.test.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

packages/graphql/src/graphql/directives/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export { nodeDirective } from "./node";
3131
export { pluralDirective } from "./plural";
3232
export { populatedByDirective } from "./populatedBy";
3333
export { privateDirective } from "./private";
34-
export { queryOptionsDirective } from "./query-options";
34+
export { limitDirective } from "./limit";
3535
export { readonlyDirective } from "./readonly";
3636
export { relationshipPropertiesDirective } from "./relationship-properties";
3737
export { relationshipDirective } from "./relationship";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import { DirectiveLocation, GraphQLDirective, GraphQLInt } from "graphql";
21+
22+
export const limitDirective = new GraphQLDirective({
23+
name: "limit",
24+
description: "Instructs @neo4j/graphql to inject limit values into a query.",
25+
args: {
26+
default: {
27+
description: "If no limit argument is supplied on query will fallback to this value.",
28+
type: GraphQLInt,
29+
},
30+
max: {
31+
description: "Maximum limit to be used for queries.",
32+
type: GraphQLInt,
33+
},
34+
},
35+
locations: [DirectiveLocation.OBJECT],
36+
});

packages/graphql/src/graphql/directives/query-options.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/graphql/src/schema-model/annotation/Annotation.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { PluralAnnotation } from "./PluralAnnotation";
3434
import { PopulatedByAnnotation } from "./PopulatedByAnnotation";
3535
import { PrivateAnnotation } from "./PrivateAnnotation";
3636
import { QueryAnnotation } from "./QueryAnnotation";
37-
import { QueryOptionsAnnotation } from "./QueryOptionsAnnotation";
37+
import { LimitAnnotation } from "./LimitAnnotation";
3838
import { SelectableAnnotation } from "./SelectableAnnotation";
3939
import { SettableAnnotation } from "./SettableAnnotation";
4040
import { SubscriptionAnnotation } from "./SubscriptionAnnotation";
@@ -48,7 +48,7 @@ export type Annotation =
4848
| AuthenticationAnnotation
4949
| KeyAnnotation
5050
| SubscriptionsAuthorizationAnnotation
51-
| QueryOptionsAnnotation
51+
| LimitAnnotation
5252
| DefaultAnnotation
5353
| CoalesceAnnotation
5454
| CustomResolverAnnotation
@@ -68,7 +68,6 @@ export type Annotation =
6868
| JWTClaimAnnotation
6969
| JWTPayloadAnnotation;
7070

71-
7271
export enum AnnotationsKey {
7372
authentication = "authentication",
7473
authorization = "authorization",
@@ -82,12 +81,12 @@ export enum AnnotationsKey {
8281
jwtClaim = "jwtClaim",
8382
jwtPayload = "jwtPayload",
8483
key = "key",
84+
limit = "limit",
8585
mutation = "mutation",
8686
plural = "plural",
8787
populatedBy = "populatedBy",
8888
private = "private",
8989
query = "query",
90-
queryOptions = "queryOptions",
9190
selectable = "selectable",
9291
settable = "settable",
9392
subscription = "subscription",
@@ -102,7 +101,7 @@ export type Annotations = {
102101
[AnnotationsKey.authentication]: AuthenticationAnnotation;
103102
[AnnotationsKey.key]: KeyAnnotation;
104103
[AnnotationsKey.subscriptionsAuthorization]: SubscriptionsAuthorizationAnnotation;
105-
[AnnotationsKey.queryOptions]: QueryOptionsAnnotation;
104+
[AnnotationsKey.limit]: LimitAnnotation;
106105
[AnnotationsKey.default]: DefaultAnnotation;
107106
[AnnotationsKey.coalesce]: CoalesceAnnotation;
108107
[AnnotationsKey.customResolver]: CustomResolverAnnotation;
@@ -129,7 +128,7 @@ export function annotationToKey(ann: Annotation): keyof Annotations {
129128
if (ann instanceof AuthenticationAnnotation) return AnnotationsKey.authentication;
130129
if (ann instanceof KeyAnnotation) return AnnotationsKey.key;
131130
if (ann instanceof SubscriptionsAuthorizationAnnotation) return AnnotationsKey.subscriptionsAuthorization;
132-
if (ann instanceof QueryOptionsAnnotation) return AnnotationsKey.queryOptions;
131+
if (ann instanceof LimitAnnotation) return AnnotationsKey.limit;
133132
if (ann instanceof DefaultAnnotation) return AnnotationsKey.default;
134133
if (ann instanceof CoalesceAnnotation) return AnnotationsKey.coalesce;
135134
if (ann instanceof CustomResolverAnnotation) return AnnotationsKey.customResolver;

0 commit comments

Comments
 (0)