@@ -40,7 +40,6 @@ import type { Neo4jGraphQLSchemaModel } from "../schema-model/Neo4jGraphQLSchema
4040import { composeResolvers } from "@graphql-tools/resolvers-composition" ;
4141import type { IExecutableSchemaDefinition } from "@graphql-tools/schema" ;
4242import { addResolversToSchema , makeExecutableSchema } from "@graphql-tools/schema" ;
43- import type { TypeSource } from "@graphql-tools/utils" ;
4443import { forEachField , getResolversFromSchema } from "@graphql-tools/utils" ;
4544import type { DocumentNode , GraphQLSchema } from "graphql" ;
4645import type { Driver , SessionConfig } from "neo4j-driver" ;
@@ -50,8 +49,10 @@ import { makeDocumentToAugment } from "../schema/make-document-to-augment";
5049import { Neo4jGraphQLAuthorization } from "./authorization/Neo4jGraphQLAuthorization" ;
5150import { Neo4jGraphQLSubscriptionsDefaultEngine } from "./Neo4jGraphQLSubscriptionsDefaultEngine" ;
5251
52+ type TypeDefinitions = string | DocumentNode | TypeDefinitions [ ] | ( ( ) => TypeDefinitions ) ;
53+
5354export 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
6263class 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 ( ) ;
0 commit comments