Skip to content

Commit

Permalink
chore: clean up leftovers of @readonly and @writeonly directives (#4500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy2003 authored Jan 10, 2024
1 parent a9e8bef commit 4ba8b36
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 85 deletions.
1 change: 0 additions & 1 deletion packages/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import { Neo4jGraphQL, Neo4jGraphQLConstructor } from "./classes";
import { Neo4jGraphQLContext } from "./types/neo4j-graphql-context";

import * as directives from "./graphql/directives";
import { CartesianPoint } from "./graphql/objects/CartesianPoint";
import { Point } from "./graphql/objects/Point";
Expand Down
8 changes: 0 additions & 8 deletions packages/graphql/src/schema/get-obj-field-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ function getObjFieldMeta({
"id",
"authorization",
"authentication",
"readonly",
"writeonly",
"customResolver",
"default",
"coalesce",
Expand All @@ -190,12 +188,6 @@ function getObjFieldMeta({
),
arguments: [...(field.arguments || [])],
description: field.description?.value,
readonly:
directives.some((d) => d.name.value === "readonly") ||
interfaceField?.directives?.some((x) => x.name.value === "readonly"),
writeonly:
directives.some((d) => d.name.value === "writeonly") ||
interfaceField?.directives?.some((x) => x.name.value === "writeonly"),
...(unique ? { unique } : {}),
};

Expand Down
55 changes: 3 additions & 52 deletions packages/graphql/src/schema/to-compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/

import type { DirectiveNode, InputValueDefinitionNode } from "graphql";
import type { DirectiveNode } from "graphql";
import { GraphQLInt } from "graphql";
import type {
Directive,
Expand All @@ -33,27 +33,11 @@ import type { ConcreteEntityAdapter } from "../schema-model/entity/model-adapter
import type { InterfaceEntityAdapter } from "../schema-model/entity/model-adapters/InterfaceEntityAdapter";
import { parseValueNode } from "../schema-model/parser/parse-value-node";
import { RelationshipAdapter } from "../schema-model/relationship/model-adapters/RelationshipAdapter";
import type { BaseField, InputField, PrimitiveField, TemporalField } from "../types";
import type { InputField } from "../types";
import { DEPRECATE_NOT } from "./constants";
import getFieldTypeMeta from "./get-field-type-meta";
import { idResolver } from "./resolvers/field/id";
import { numericalResolver } from "./resolvers/field/numerical";

export function graphqlInputValueToCompose(args: InputValueDefinitionNode[]) {
return args.reduce((res, arg) => {
const meta = getFieldTypeMeta(arg.type);

return {
...res,
[arg.name.value]: {
type: meta.pretty,
description: arg.description,
...(arg.defaultValue ? { defaultValue: parseValueNode(arg.defaultValue) } : {}),
},
};
}, {});
}

export function graphqlArgsToCompose(args: Argument[]) {
return args.reduce((res, arg) => {
const inputValueAdapter = new ArgumentAdapter(arg);
Expand Down Expand Up @@ -81,40 +65,6 @@ export function graphqlDirectivesToCompose(directives: DirectiveNode[]): Directi
}));
}

export function objectFieldsToComposeFields(fields: BaseField[]): {
[k: string]: ObjectTypeComposerFieldConfigAsObjectDefinition<any, any>;
} {
return fields.reduce((res, field) => {
if (field.writeonly || field.selectableOptions.onRead === false) {
return res;
}

const newField: ObjectTypeComposerFieldConfigAsObjectDefinition<any, any> = {
type: field.typeMeta.pretty,
args: {},
description: field.description,
};

if (field.otherDirectives.length) {
newField.directives = graphqlDirectivesToCompose(field.otherDirectives);
}

if (["Int", "Float"].includes(field.typeMeta.name)) {
newField.resolve = numericalResolver;
}

if (field.typeMeta.name === "ID") {
newField.resolve = idResolver;
}

if (field.arguments) {
newField.args = graphqlInputValueToCompose(field.arguments);
}

return { ...res, [field.fieldName]: newField };
}, {});
}

export function relationshipAdapterToComposeFields(
objectFields: RelationshipAdapter[],
userDefinedFieldDirectives: Map<string, DirectiveNode[]>
Expand Down Expand Up @@ -334,6 +284,7 @@ export function withMathOperators(): AdditionalFieldsCallback {
return fields;
};
}

export function withArrayOperators(): AdditionalFieldsCallback {
return (attribute: AttributeAdapter): InputTypeComposerFieldConfigMapDefinition => {
const fields: InputTypeComposerFieldConfigMapDefinition = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ describe("createRelationshipPropertyElement", () => {
otherDirectives: [],
arguments: [],
description: undefined,
readonly: false,
writeonly: false,
} as PrimitiveField,
],
pointFields: [
Expand Down Expand Up @@ -115,8 +113,6 @@ describe("createRelationshipPropertyElement", () => {
otherDirectives: [],
arguments: [],
description: undefined,
readonly: false,
writeonly: false,
} as PointField,
],
temporalFields: [
Expand Down Expand Up @@ -158,8 +154,6 @@ describe("createRelationshipPropertyElement", () => {
otherDirectives: [],
arguments: [],
description: undefined,
readonly: false,
writeonly: false,
} as TemporalField,
],
});
Expand Down
2 changes: 0 additions & 2 deletions packages/graphql/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ export interface BaseField {
arguments: InputValueDefinitionNode[];
private?: boolean;
description?: string;
readonly?: boolean;
writeonly?: boolean;
dbPropertyName?: string;
dbPropertyNameUnescaped?: string;
unique?: Unique;
Expand Down
16 changes: 8 additions & 8 deletions packages/ogm/src/utils/filter-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import { filterDocument } from "./filter-document";
describe("filterDocument", () => {
test("should remove all directives", () => {
const initial = `
type User @auth @exclude {
id: ID @auth @private @readonly @writeonly
name: String @auth @private @readonly @writeonly
email: String @auth @private @readonly @writeonly
password: String @auth @private @readonly @writeonly
type User @authentication @authorization {
id: ID @id @private @unique
name: String @authentication @authorization @private
email: String @cypher @private
password: String @private
cars: [Car!]! @relationship(type: "HAS_CAR", direction: OUT, aggregate: false)
bikes: [Car!]! @relationship(type: "HAS_CAR", direction: OUT)
bikes: [Car!]! @relationship(type: "HAS_CAR", direction: OUT)
}
type Car @query(read: false, aggregate: false) @mutation(operations: []), @subscription(events: []) {
Expand All @@ -50,9 +50,9 @@ describe("filterDocument", () => {

expect(print(filtered)).toMatchInlineSnapshot(`
"type User {
id: ID
id: ID @id @unique
name: String
email: String
email: String @cypher
password: String
cars: [Car!]! @relationship(type: \\"HAS_CAR\\", direction: OUT, aggregate: true)
bikes: [Car!]! @relationship(type: \\"HAS_CAR\\", direction: OUT, aggregate: true)
Expand Down
8 changes: 2 additions & 6 deletions packages/ogm/src/utils/filter-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ import type { Neo4jGraphQLConstructor } from "@neo4j/graphql";
import { mergeTypeDefs } from "@graphql-tools/merge";

const excludedDirectives = [
"auth",
"authentication",
"authorization",
"subscriptionsAuthorization",
"exclude",
"private",
"readonly",
"writeonly",
"query",
"mutation",
"subscription",
Expand All @@ -41,8 +37,8 @@ const excludedDirectives = [
export function filterDocument(typeDefs: Neo4jGraphQLConstructor["typeDefs"]): DocumentNode {
// hack to keep aggregation enabled for OGM
const schemaExtension = `
extend schema @query(read: true, aggregate: true)
@mutation(operations: [CREATE, UPDATE, DELETE])
extend schema @query(read: true, aggregate: true)
@mutation(operations: [CREATE, UPDATE, DELETE])
@subscription(events: [CREATED, UPDATED, DELETED, RELATIONSHIP_CREATED, RELATIONSHIP_DELETED])`;
const merged = mergeTypeDefs(
Array.isArray(typeDefs) ? (typeDefs as string[]).concat(schemaExtension) : [typeDefs as string, schemaExtension]
Expand Down
4 changes: 2 additions & 2 deletions packages/ogm/tests/integration/ogm.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ describe("OGM", () => {
await session.close();
});

test("should filter the document and remove directives such as auth", async () => {
test("should filter the document and remove directives such as authentication", async () => {
const session = driver.session();

const typeDefs = `
type ${typeMovie} @auth(rules: [{ isAuthenticated: true }]){
type ${typeMovie} @authentication {
id: ID
}
`;
Expand Down

0 comments on commit 4ba8b36

Please sign in to comment.