Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually add schemaDirectives after resolvers #356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"author": "Neo4j Inc.",
"devDependencies": {
"@graphql-tools/utils": "^7.7.3",
"@types/deep-equal": "1.0.1",
"@types/faker": "5.1.7",
"@types/is-uuid": "1.0.0",
Expand All @@ -63,6 +62,7 @@
"dependencies": {
"@graphql-tools/merge": "^6.2.13",
"@graphql-tools/schema": "^7.1.3",
"@graphql-tools/utils": "^7.10.0",
"camelcase": "^6.2.0",
"debug": "^4.3.1",
"deep-equal": "^2.0.5",
Expand Down
20 changes: 17 additions & 3 deletions packages/graphql/src/classes/Neo4jGraphQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Debug from "debug";
import { Driver } from "neo4j-driver";
import { DocumentNode, GraphQLResolveInfo, GraphQLSchema, parse, printSchema, print } from "graphql";
import { addResolversToSchema, addSchemaLevelResolver, IExecutableSchemaDefinition } from "@graphql-tools/schema";
import { SchemaDirectiveVisitor } from "@graphql-tools/utils";
import type { DriverConfig, CypherQueryOptions } from "../types";
import { makeAugmentedSchema } from "../schema";
import Node from "./Node";
Expand All @@ -46,9 +47,10 @@ export interface Neo4jGraphQLConfig {
queryOptions?: CypherQueryOptions;
}

export interface Neo4jGraphQLConstructor extends IExecutableSchemaDefinition {
export interface Neo4jGraphQLConstructor extends Omit<IExecutableSchemaDefinition, "schemaDirectives"> {
config?: Neo4jGraphQLConfig;
driver?: Driver;
schemaDirectives?: Record<string, typeof SchemaDirectiveVisitor>;
}

class Neo4jGraphQL {
Expand All @@ -65,7 +67,7 @@ class Neo4jGraphQL {
public config?: Neo4jGraphQLConfig;

constructor(input: Neo4jGraphQLConstructor) {
const { config = {}, driver, resolvers, ...schemaDefinition } = input;
const { config = {}, driver, resolvers, schemaDirectives, ...schemaDefinition } = input;
const { nodes, relationships, schema } = makeAugmentedSchema(schemaDefinition, {
enableRegex: config.enableRegex,
});
Expand All @@ -75,8 +77,15 @@ class Neo4jGraphQL {
this.nodes = nodes;
this.relationships = relationships;
this.schema = schema;

/*
addResolversToSchema must be first, so that custom resolvers also get schema level resolvers
Order must be:

addResolversToSchema -> visitSchemaDirectives -> createWrappedSchema

addResolversToSchema breaks schema directives added before it

createWrappedSchema must come last so that all requests have context prepared correctly
*/
if (resolvers) {
if (Array.isArray(resolvers)) {
Expand All @@ -87,6 +96,11 @@ class Neo4jGraphQL {
this.schema = addResolversToSchema(this.schema, resolvers);
}
}

if (schemaDirectives) {
SchemaDirectiveVisitor.visitSchemaDirectives(this.schema, schemaDirectives);
}

this.schema = this.createWrappedSchema({ schema: this.schema, config });
this.document = parse(printSchema(schema));
}
Expand Down
215 changes: 215 additions & 0 deletions packages/graphql/tests/integration/issues/349.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*
* 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 neo4j from "neo4j-driver";
import { SchemaDirectiveVisitor } from "@graphql-tools/utils";
import { graphql } from "graphql";
import { Neo4jGraphQL } from "../../../src/classes";

describe("https://github.com/neo4j/graphql/issues/349", () => {
type Field = Parameters<SchemaDirectiveVisitor["visitFieldDefinition"]>[0];

class DisallowDirective extends SchemaDirectiveVisitor {
// eslint-disable-next-line class-methods-use-this
public visitFieldDefinition(field: Field) {
field.resolve = function () {
// Disallow any and all access, all the time
throw new Error("go away");
};
}
}

const schemaDirectives = {
disallow: DisallowDirective,
};

describe("https://github.com/neo4j/graphql/issues/349#issuecomment-885295157", () => {
const neo4jGraphQL = new Neo4jGraphQL({
typeDefs: /* GraphQL */ `
directive @disallow on FIELD_DEFINITION

type Mutation {
doStuff: String! @disallow
}

type Query {
noop: Boolean
}
`,

driver: neo4j.driver("bolt://localhost:7687"),
resolvers: { Mutation: { doStuff: () => "OK" } },
schemaDirectives,
});

test("DisallowDirective", async () => {
const gqlResult = await graphql({
schema: neo4jGraphQL.schema,
source: /* GraphQL */ `
mutation {
doStuff
}
`,
contextValue: { driver: neo4j.driver("bolt://localhost:7687") },
});

expect(gqlResult.data).toBeNull();
expect(gqlResult.errors).toBeTruthy();
});
});

describe("https://github.com/neo4j/graphql/issues/349#issuecomment-885311918", () => {
const neo4jGraphQL = new Neo4jGraphQL({
typeDefs: /* GraphQL */ `
directive @disallow on FIELD_DEFINITION

type NestedResult {
stuff: String! @disallow
}

type Mutation {
doStuff: String! @disallow
doNestedStuff: NestedResult!
}

type Query {
getStuff: String! @disallow
getNestedStuff: NestedResult!
}
`,

driver: neo4j.driver("bolt://localhost:7687"),
resolvers: {
NestedResult: {
stuff: (parent: string) => parent,
},

Mutation: {
doStuff: () => "OK",
doNestedStuff: () => "OK",
},

Query: {
getStuff: () => "OK",
getNestedStuff: () => "OK",
},
},
schemaDirectives,
});

test("mutation top - DisallowDirective", async () => {
const gqlResult = await graphql({
schema: neo4jGraphQL.schema,
source: /* GraphQL */ `
mutation {
doStuff
}
`,
contextValue: { driver: neo4j.driver("bolt://localhost:7687") },
});

expect(gqlResult.data).toBeNull();
expect(gqlResult.errors && gqlResult.errors[0].message).toBe("go away");
});

test("query top - DisallowDirective", async () => {
const gqlResult = await graphql({
schema: neo4jGraphQL.schema,
source: /* GraphQL */ `
query {
getStuff
}
`,
contextValue: { driver: neo4j.driver("bolt://localhost:7687") },
});

expect(gqlResult.data).toBeNull();
expect(gqlResult.errors && gqlResult.errors[0].message).toBe("go away");
});

test("mutation nested - DisallowDirective", async () => {
const gqlResult = await graphql({
schema: neo4jGraphQL.schema,
source: /* GraphQL */ `
mutation {
doNestedStuff {
stuff
}
}
`,
contextValue: { driver: neo4j.driver("bolt://localhost:7687") },
});

expect(gqlResult.data).toBeNull();
expect(gqlResult.errors && gqlResult.errors[0].message).toBe("go away");
});

test("query nested - DisallowDirective", async () => {
const gqlResult = await graphql({
schema: neo4jGraphQL.schema,
source: /* GraphQL */ `
query {
getNestedStuff {
stuff
}
}
`,
contextValue: { driver: neo4j.driver("bolt://localhost:7687") },
});

expect(gqlResult.data).toBeNull();
expect(gqlResult.errors && gqlResult.errors[0].message).toBe("go away");
});
});

describe("schemaDirectives can be an empty object", () => {
const neo4jGraphQL = new Neo4jGraphQL({
typeDefs: /* GraphQL */ `
directive @disallow on FIELD_DEFINITION

type Mutation {
doStuff: String! @disallow
}

type Query {
noop: Boolean
}
`,

driver: neo4j.driver("bolt://localhost:7687"),
resolvers: { Mutation: { doStuff: () => "OK" } },
schemaDirectives: {},
});

test("DisallowDirective", async () => {
const gqlResult = await graphql({
schema: neo4jGraphQL.schema,
source: /* GraphQL */ `
mutation {
doStuff
}
`,
contextValue: { driver: neo4j.driver("bolt://localhost:7687") },
});

expect(gqlResult.data?.doStuff).toEqual("OK");
expect(gqlResult.errors).toBeFalsy();
});
});
});
17 changes: 15 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ __metadata:
languageName: node
linkType: hard

"@graphql-tools/utils@npm:^7.1.2, @graphql-tools/utils@npm:^7.7.0, @graphql-tools/utils@npm:^7.7.3":
"@graphql-tools/utils@npm:^7.1.2, @graphql-tools/utils@npm:^7.7.0":
version: 7.8.0
resolution: "@graphql-tools/utils@npm:7.8.0"
dependencies:
Expand All @@ -637,6 +637,19 @@ __metadata:
languageName: node
linkType: hard

"@graphql-tools/utils@npm:^7.10.0":
version: 7.10.0
resolution: "@graphql-tools/utils@npm:7.10.0"
dependencies:
"@ardatan/aggregate-error": 0.0.6
camel-case: 4.1.2
tslib: ~2.2.0
peerDependencies:
graphql: ^14.0.0 || ^15.0.0
checksum: 8b8e674f344e825c27816ead8a66edca5aed2eac26b601bb028350837ae39fee3ca9241255b918b8199d60bff41884a4814f98df465a0a8a5db5b91428ce2aa9
languageName: node
linkType: hard

"@graphql-typed-document-node/core@npm:^3.0.0":
version: 3.1.0
resolution: "@graphql-typed-document-node/core@npm:3.1.0"
Expand Down Expand Up @@ -929,7 +942,7 @@ __metadata:
dependencies:
"@graphql-tools/merge": ^6.2.13
"@graphql-tools/schema": ^7.1.3
"@graphql-tools/utils": ^7.7.3
"@graphql-tools/utils": ^7.10.0
"@types/deep-equal": 1.0.1
"@types/faker": 5.1.7
"@types/is-uuid": 1.0.0
Expand Down