Skip to content

Fix for loss of scalar and field level resolvers #297

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

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
1 change: 1 addition & 0 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"rimraf": "3.0.2",
"semver": "7.3.5",
"ts-jest": "26.1.4",
"ts-node": "^10.0.0",
"typescript": "3.9.7"
},
"dependencies": {
Expand Down
22 changes: 19 additions & 3 deletions packages/graphql/src/classes/Neo4jGraphQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import Debug from "debug";
import { Driver } from "neo4j-driver";
import { DocumentNode, GraphQLResolveInfo, GraphQLSchema, parse, printSchema, print } from "graphql";
import { addSchemaLevelResolver, IExecutableSchemaDefinition } from "@graphql-tools/schema";
import { addResolversToSchema, addSchemaLevelResolver, IExecutableSchemaDefinition } from "@graphql-tools/schema";
import type { DriverConfig } from "../types";
import { makeAugmentedSchema } from "../schema";
import Node from "./Node";
import { checkNeo4jCompat } from "../utils";
import { getJWT } from "../auth/index";
import { DEBUG_GRAPHQL } from "../constants";
import getNeo4jResolveTree from "../utils/get-neo4j-resolve-tree";
import createAuthParam from "../translate/create-auth-param";

const debug = Debug(DEBUG_GRAPHQL);

Expand Down Expand Up @@ -60,13 +61,26 @@ class Neo4jGraphQL {
public config?: Neo4jGraphQLConfig;

constructor(input: Neo4jGraphQLConstructor) {
const { config = {}, driver, ...schemaDefinition } = input;
const { config = {}, driver, resolvers, ...schemaDefinition } = input;
const { nodes, schema } = makeAugmentedSchema(schemaDefinition, { enableRegex: config.enableRegex });

this.driver = driver;
this.config = config;
this.nodes = nodes;
this.schema = this.createWrappedSchema({ schema, config });
this.schema = schema;
/*
addResolversToSchema must be first, so that custom resolvers also get schema level resolvers
*/
if (resolvers) {
if (Array.isArray(resolvers)) {
resolvers.forEach((r) => {
this.schema = addResolversToSchema(this.schema, r);
});
} else {
this.schema = addResolversToSchema(this.schema, resolvers);
}
}
this.schema = this.createWrappedSchema({ schema: this.schema, config });
this.document = parse(printSchema(schema));
}

Expand Down Expand Up @@ -118,6 +132,8 @@ class Neo4jGraphQL {
context.resolveTree = getNeo4jResolveTree(resolveInfo);

context.jwt = getJWT(context);

context.auth = createAuthParam({ context });
});
}

Expand Down
16 changes: 2 additions & 14 deletions packages/graphql/src/schema/make-augmented-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ import { findResolver, createResolver, deleteResolver, cypherResolver, updateRes
import checkNodeImplementsInterfaces from "./check-node-implements-interfaces";
import * as Scalars from "./scalars";
import parseExcludeDirective from "./parse-exclude-directive";
import wrapCustomResolvers from "./wrap-custom-resolvers";
import getCustomResolvers from "./get-custom-resolvers";
import getObjFieldMeta from "./get-obj-field-meta";
import * as point from "./point";
import { graphqlDirectivesToCompose, objectFieldsToComposeFields } from "./to-compose";
// import validateTypeDefs from "./validation";

function makeAugmentedSchema(
{ typeDefs, resolvers, ...schemaDefinition }: IExecutableSchemaDefinition,
{ typeDefs, ...schemaDefinition }: IExecutableSchemaDefinition,
{ enableRegex }: { enableRegex?: boolean } = {}
): { schema: GraphQLSchema; nodes: Node[] } {
const document = mergeTypeDefs(Array.isArray(typeDefs) ? (typeDefs as string[]) : [typeDefs as string]);
Expand Down Expand Up @@ -175,8 +174,6 @@ function makeAugmentedSchema(
return node;
});

const nodeNames = nodes.map((x) => x.name);

nodes.forEach((node) => {
const nodeFields = objectFieldsToComposeFields([
...node.primitiveFields,
Expand Down Expand Up @@ -741,7 +738,7 @@ function makeAugmentedSchema(
}

const generatedTypeDefs = composer.toSDL();
let generatedResolvers: any = {
const generatedResolvers = {
...composer.getResolveMethods(),
...Object.entries(Scalars).reduce((res, [name, scalar]) => {
if (generatedTypeDefs.includes(`scalar ${name}\n`)) {
Expand All @@ -751,16 +748,7 @@ function makeAugmentedSchema(
}, {}),
};

if (resolvers) {
generatedResolvers = wrapCustomResolvers({
generatedResolvers,
nodeNames,
resolvers,
});
}

unions.forEach((union) => {
// eslint-disable-next-line no-underscore-dangle
if (!generatedResolvers[union.name.value]) {
generatedResolvers[union.name.value] = { __resolveType: (root) => root.__resolveType };
}
Expand Down
132 changes: 0 additions & 132 deletions packages/graphql/src/schema/wrap-custom-resolvers.ts

This file was deleted.

112 changes: 112 additions & 0 deletions packages/graphql/tests/integration/issues/#207.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 { Driver } from "neo4j-driver";
import { graphql } from "graphql";
import { gql } from "apollo-server";
import neo4j from "../neo4j";
import { Neo4jGraphQL } from "../../../src/classes";

describe("https://github.com/neo4j/graphql/issues/207", () => {
let driver: Driver;
const typeDefs = gql`
union Result = Book | Author

type Book {
title: String
}

type Author {
name: String
}

type Query {
search: [Result]
}
`;
const resolvers = {
Result: {
__resolveType(obj) {
if (obj.name) {
return "Author";
}
if (obj.title) {
return "Book";
}
return null; // GraphQLError is thrown
},
},
Query: {
search: () => [{ title: "GraphQL Unions for Dummies" }, { name: "Darrell" }],
},
};

beforeAll(async () => {
driver = await neo4j();
});

afterAll(async () => {
await driver.close();
});

test("__resolveType resolvers are correctly evaluated", async () => {
const session = driver.session();

const neoSchema = new Neo4jGraphQL({ typeDefs, resolvers, driver });

const mutation = `
query GetSearchResults {
search {
__typename
... on Book {
title
}
... on Author {
name
}
}
}
`;

try {
await neoSchema.checkNeo4jCompat();

const result = await graphql({
schema: neoSchema.schema,
source: mutation,
contextValue: { driver },
});

expect(result.errors).toBeFalsy();

expect(result?.data?.search).toEqual([
{
__typename: "Book",
title: "GraphQL Unions for Dummies",
},
{
__typename: "Author",
name: "Darrell",
},
]);
} finally {
await session.close();
}
});
});
Loading