-
Notifications
You must be signed in to change notification settings - Fork 149
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 #5738 from neo4j/deprecate-unique
deprecation warning for unique
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 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": patch | ||
--- | ||
|
||
Add deprecation warning when using `@unique` |
51 changes: 51 additions & 0 deletions
51
packages/graphql/src/schema/validation/custom-rules/warnings/deprecated-unique.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,51 @@ | ||
/* | ||
* 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 { ASTVisitor, ObjectTypeDefinitionNode } from "graphql"; | ||
import { uniqueDirective } from "../../../../graphql/directives"; | ||
|
||
export function WarnUniqueDeprecation() { | ||
return function (): ASTVisitor { | ||
let warningAlreadyIssued = false; | ||
|
||
return { | ||
ObjectTypeDefinition(objectTypeDefinition: ObjectTypeDefinitionNode) { | ||
if (["Query", "Mutation", "Subscription"].includes(objectTypeDefinition.name.value)) { | ||
return; | ||
} | ||
if (warningAlreadyIssued) { | ||
return; | ||
} | ||
let hasUniqueDirective = false; | ||
for (const field of objectTypeDefinition.fields || []) { | ||
for (const directive of field.directives ?? []) { | ||
if (directive.name.value === uniqueDirective.name) { | ||
hasUniqueDirective = true; | ||
} | ||
} | ||
} | ||
|
||
if (hasUniqueDirective) { | ||
console.warn(`Future library versions will not support @unique directive.`); | ||
warningAlreadyIssued = 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
52 changes: 52 additions & 0 deletions
52
packages/graphql/tests/integration/migration-warnings/unique-directive.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,52 @@ | ||
/* | ||
* 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 { Neo4jGraphQL } from "../../../src/classes"; | ||
|
||
describe("deprecated @unique warnings", () => { | ||
let warn: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
warn = jest.spyOn(console, "warn").mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
warn.mockReset(); | ||
}); | ||
|
||
test("warning on unique usage", async () => { | ||
const typeDefs = /* GraphQL */ ` | ||
type User @node { | ||
id: ID! @unique | ||
firstName: String! | ||
} | ||
type Movie @node { | ||
id: ID! @unique | ||
} | ||
`; | ||
|
||
const neoSchema = new Neo4jGraphQL({ | ||
typeDefs: typeDefs, | ||
validate: true, | ||
}); | ||
await neoSchema.getSchema(); | ||
expect(warn).toHaveBeenCalledWith("Future library versions will not support @unique directive."); | ||
}); | ||
}); |