Skip to content

Commit

Permalink
Merge pull request #5738 from neo4j/deprecate-unique
Browse files Browse the repository at this point in the history
deprecation warning for unique
  • Loading branch information
angrykoala authored Nov 1, 2024
2 parents 3b37c3d + c6ea37d commit 123d1bf
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-chicken-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/graphql": patch
---

Add deprecation warning when using `@unique`
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;
}
},
};
};
}
2 changes: 2 additions & 0 deletions packages/graphql/src/schema/validation/validate-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
import { ValidFieldTypes } from "./custom-rules/valid-types/valid-field-types";
import { ValidObjectType } from "./custom-rules/valid-types/valid-object-type";
import { WarnIfAuthorizationFeatureDisabled } from "./custom-rules/warnings/authorization-feature-disabled";
import { WarnUniqueDeprecation } from "./custom-rules/warnings/deprecated-unique";
import { WarnIfAMaxLimitCanBeBypassedThroughInterface } from "./custom-rules/warnings/limit-max-can-be-bypassed";
import { WarnIfListOfListsFieldDefinition } from "./custom-rules/warnings/list-of-lists";
import { WarnObjectFieldsWithoutResolver } from "./custom-rules/warnings/object-fields-without-resolver";
Expand Down Expand Up @@ -233,6 +234,7 @@ function runValidationRulesOnFilteredDocument({
WarnIfSubscriptionsAuthorizationMissing(Boolean(features?.subscriptions)),
WarnIfTypeIsNotMarkedAsNode(),
WarnIfQueryDirectionIsUsedWithDeprecatedValues,
WarnUniqueDeprecation(),
],
schema
);
Expand Down
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.");
});
});

0 comments on commit 123d1bf

Please sign in to comment.