-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6029 from darrellwarde/feature/sortable-directive
Add `@sortable` directive
- Loading branch information
1 parent
33ca95a
commit f792a02
Showing
11 changed files
with
1,575 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@neo4j/graphql": minor | ||
--- | ||
|
||
Add a new field directive `@sortable` which can be used to configure whether results can be sorted by field values or not. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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, GraphQLBoolean, GraphQLDirective, GraphQLNonNull } from "graphql"; | ||
|
||
export const sortableDirective = new GraphQLDirective({ | ||
name: "sortable", | ||
description: "Instructs @neo4j/graphql to generate sorting inputs for this field.", | ||
locations: [DirectiveLocation.FIELD_DEFINITION], | ||
args: { | ||
byValue: { | ||
description: "Generates sorting inputs for this field", | ||
type: new GraphQLNonNull(GraphQLBoolean), | ||
defaultValue: true, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/graphql/src/schema-model/annotation/SortableAnnotation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 type { Annotation } from "./Annotation"; | ||
|
||
export class SortableAnnotation implements Annotation { | ||
readonly name = "sortable"; | ||
public readonly byValue: boolean; | ||
|
||
constructor({ byValue }: { byValue: boolean }) { | ||
this.byValue = byValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/graphql/src/schema-model/parser/annotations-parser/sortable-annotation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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 { makeDirectiveNode } from "@graphql-tools/utils"; | ||
import { sortableDirective } from "../../../graphql/directives"; | ||
import { parseSortableAnnotation } from "./sortable-annotation"; | ||
|
||
const tests = [ | ||
{ | ||
name: "should parse correctly when byValue is true", | ||
directive: makeDirectiveNode( | ||
"sortable", | ||
{ | ||
byValue: true, | ||
}, | ||
sortableDirective | ||
), | ||
expected: { | ||
byValue: true, | ||
}, | ||
}, | ||
{ | ||
name: "should parse correctly when byValue is false", | ||
directive: makeDirectiveNode( | ||
"sortable", | ||
{ | ||
byValue: false, | ||
}, | ||
sortableDirective | ||
), | ||
expected: { | ||
byValue: false, | ||
}, | ||
}, | ||
]; | ||
|
||
describe("parseSortableAnnotation", () => { | ||
tests.forEach((test) => { | ||
it(`${test.name}`, () => { | ||
const sortableAnnotation = parseSortableAnnotation(test.directive); | ||
expect(sortableAnnotation.byValue).toBe(test.expected.byValue); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/graphql/src/schema-model/parser/annotations-parser/sortable-annotation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 type { DirectiveNode } from "graphql"; | ||
import { sortableDirective } from "../../../graphql/directives"; | ||
import { SortableAnnotation } from "../../annotation/SortableAnnotation"; | ||
import { parseArguments } from "../parse-arguments"; | ||
|
||
export function parseSortableAnnotation(directive: DirectiveNode): SortableAnnotation { | ||
const { byValue } = parseArguments<{ | ||
byValue: boolean; | ||
}>(sortableDirective, directive); | ||
|
||
return new SortableAnnotation({ | ||
byValue, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.