Skip to content

Commit 2ba1d45

Browse files
authored
Reduction in types accepted for type definitions (#3592)
* Refined normalization of user type definitions * Remove unused type * Wrap current behaviour with new lifecycle naming * Fix linting error * Delete unused tests * Fix test typings * Update Neo4jGraphQL.ts
1 parent 28fd15e commit 2ba1d45

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

.changeset/khaki-mayflies-float.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@neo4j/graphql": major
3+
"@neo4j/graphql-ogm": major
4+
---
5+
6+
The Neo4j GraphQL Library now only accepts a `string`, `DocumentNode` or an array containing these types. A callback function returning these is also accepted. This is a reduction from `TypeSource` which also included types such as `GraphQLSchema` and `DefinitionNode`, which would have resulted in unexpected behaviour if passed in.

packages/graphql/src/classes/Neo4jGraphQL.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import type { Neo4jGraphQLSchemaModel } from "../schema-model/Neo4jGraphQLSchema
4040
import { composeResolvers } from "@graphql-tools/resolvers-composition";
4141
import type { IExecutableSchemaDefinition } from "@graphql-tools/schema";
4242
import { addResolversToSchema, makeExecutableSchema } from "@graphql-tools/schema";
43-
import type { TypeSource } from "@graphql-tools/utils";
4443
import { forEachField, getResolversFromSchema } from "@graphql-tools/utils";
4544
import type { DocumentNode, GraphQLSchema } from "graphql";
4645
import type { Driver, SessionConfig } from "neo4j-driver";
@@ -50,8 +49,10 @@ import { makeDocumentToAugment } from "../schema/make-document-to-augment";
5049
import { Neo4jGraphQLAuthorization } from "./authorization/Neo4jGraphQLAuthorization";
5150
import { Neo4jGraphQLSubscriptionsDefaultEngine } from "./Neo4jGraphQLSubscriptionsDefaultEngine";
5251

52+
type TypeDefinitions = string | DocumentNode | TypeDefinitions[] | (() => TypeDefinitions);
53+
5354
export interface Neo4jGraphQLConstructor {
54-
typeDefs: TypeSource;
55+
typeDefs: TypeDefinitions;
5556
resolvers?: IExecutableSchemaDefinition["resolvers"];
5657
features?: Neo4jFeaturesSettings;
5758
driver?: Driver;
@@ -60,7 +61,7 @@ export interface Neo4jGraphQLConstructor {
6061
}
6162

6263
class Neo4jGraphQL {
63-
private typeDefs: TypeSource;
64+
private typeDefs: TypeDefinitions;
6465
private resolvers?: IExecutableSchemaDefinition["resolvers"];
6566

6667
private driver?: Driver;
@@ -208,6 +209,39 @@ class Neo4jGraphQL {
208209
return this._relationships;
209210
}
210211

212+
/**
213+
* Currently just merges all type definitions into a document. Eventual intention described below:
214+
*
215+
* Normalizes the user's type definitions using the method with the lowest risk of side effects:
216+
* - Type definitions of type `string` are parsed using the `parse` function from the reference GraphQL implementation.
217+
* - Type definitions of type `DocumentNode` are returned as they are.
218+
* - Type definitions in arrays are merged using `mergeTypeDefs` from `@graphql-tools/merge`.
219+
* - Callbacks are resolved to a type which can be parsed into a document.
220+
*
221+
* This method maps to the Type Definition Normalization stage of the Schema Generation lifecycle.
222+
*
223+
* @param {TypeDefinitions} typeDefinitions - The unnormalized type definitions.
224+
* @returns {DocumentNode} The normalized type definitons as a document.
225+
*/
226+
private normalizeTypeDefinitions(typeDefinitions: TypeDefinitions): DocumentNode {
227+
// TODO: The dream: minimal modification of the type definitions. However, this does not merge extensions, which we can't currently deal with in translation.
228+
// if (typeof typeDefinitions === "function") {
229+
// return this.normalizeTypeDefinitions(typeDefinitions());
230+
// }
231+
232+
// if (typeof typeDefinitions === "string") {
233+
// return parse(typeDefinitions);
234+
// }
235+
236+
// if (Array.isArray(typeDefinitions)) {
237+
// return mergeTypeDefs(typeDefinitions);
238+
// }
239+
240+
// return typeDefinitions;
241+
242+
return mergeTypeDefs(typeDefinitions);
243+
}
244+
211245
private addDefaultFieldResolvers(schema: GraphQLSchema): GraphQLSchema {
212246
forEachField(schema, (field) => {
213247
if (!field.resolve) {
@@ -228,10 +262,6 @@ class Neo4jGraphQL {
228262
}
229263
}
230264

231-
private getDocument(typeDefs: TypeSource): DocumentNode {
232-
return mergeTypeDefs(typeDefs);
233-
}
234-
235265
private async getNeo4jDatabaseInfo(driver: Driver, sessionConfig?: SessionConfig): Promise<Neo4jDatabaseInfo> {
236266
const executorConstructorParam: ExecutorConstructorParam = {
237267
executionContext: driver,
@@ -310,7 +340,7 @@ class Neo4jGraphQL {
310340

311341
private generateExecutableSchema(): Promise<GraphQLSchema> {
312342
return new Promise((resolve) => {
313-
const initialDocument = this.getDocument(this.typeDefs);
343+
const initialDocument = this.normalizeTypeDefinitions(this.typeDefs);
314344

315345
if (this.validate) {
316346
validateDocument({ document: initialDocument, features: this.features });
@@ -350,7 +380,7 @@ class Neo4jGraphQL {
350380
// Import only when needed to avoid issues if GraphQL 15 being used
351381
const { Subgraph } = await import("./Subgraph");
352382

353-
const initialDocument = this.getDocument(this.typeDefs);
383+
const initialDocument = this.normalizeTypeDefinitions(this.typeDefs);
354384
const subgraph = new Subgraph(this.typeDefs);
355385

356386
const { directives, types } = subgraph.getValidationDefinitions();

packages/graphql/tests/e2e/federation/setup/subgraph.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@
1717
* limitations under the License.
1818
*/
1919

20-
import type { TypeSource } from "@graphql-tools/utils";
21-
import type { GraphQLSchema } from "graphql";
20+
import type { DocumentNode, GraphQLSchema } from "graphql";
2221
import type * as neo4j from "neo4j-driver";
2322
import { Neo4jGraphQL } from "../../../../src";
2423

2524
export class TestSubgraph {
2625
library: Neo4jGraphQL;
2726

28-
constructor({ typeDefs, resolvers, driver }: { typeDefs: TypeSource; resolvers?: any; driver: neo4j.Driver }) {
27+
constructor({
28+
typeDefs,
29+
resolvers,
30+
driver,
31+
}: {
32+
typeDefs: string | DocumentNode;
33+
resolvers?: any;
34+
driver: neo4j.Driver;
35+
}) {
2936
this.library = new Neo4jGraphQL({
3037
typeDefs,
3138
resolvers,

0 commit comments

Comments
 (0)