From 2e759b64c399de96dcba2b906be74cecf808160f Mon Sep 17 00:00:00 2001 From: eric-burel Date: Mon, 2 May 2022 12:01:05 +0200 Subject: [PATCH] data source fetching handles both model name and gql typename --- .../contextBuilder/createDataSources.ts | 2 ++ .../graphql/server/contextBuilder/utils.ts | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/graphql/server/contextBuilder/createDataSources.ts b/packages/graphql/server/contextBuilder/createDataSources.ts index 06155b0f..655f3db5 100644 --- a/packages/graphql/server/contextBuilder/createDataSources.ts +++ b/packages/graphql/server/contextBuilder/createDataSources.ts @@ -29,6 +29,8 @@ export const createDataSources = ); return { ...dataSources, + // TODO: in context we use "model.graphql.typeName" but here we use "name" + // The getters are robust to both, but we should clarify [model.name]: model.graphql.createDataSource() /*|| createMongooseDataSource(model, connector),*/, }; diff --git a/packages/graphql/server/contextBuilder/utils.ts b/packages/graphql/server/contextBuilder/utils.ts index 0ed52fb9..70acb857 100644 --- a/packages/graphql/server/contextBuilder/utils.ts +++ b/packages/graphql/server/contextBuilder/utils.ts @@ -9,6 +9,13 @@ import { VulcanGenericDataSource } from "./typings"; import type { VulcanGraphqlModel } from "../../typings"; import type { VulcanGraphqlModelServer } from "../typings"; +const printModelName = (model: VulcanGraphqlModel) => + `Model of name ${model.name} ${ + model.name !== model.graphql.typeName + ? `and typeName ${model.graphql.typeName}` + : undefined + }`; + /** * Get data source from the context. * @param context @@ -23,11 +30,12 @@ export const getModelDataSource = ( throw new Error( "DataSources not set in Apollo. You need to set at least the default dataSources for Vulcan graphql models." ); - const dataSource = context.dataSources[model.graphql.typeName]; + const dataSource = + // model.name is the default but we fallback to typeName just in case + context.dataSource[model.name] || + context.dataSources[model.graphql.typeName]; if (!dataSource) { - throw new Error( - `Model of typeName ${model.graphql.typeName} have no default dataSource.` - ); + throw new Error(`${printModelName(model)} have no default dataSource.`); } return dataSource; }; @@ -51,17 +59,18 @@ export const getModelConnector = ( context, model: VulcanGraphqlModel ): Connector => { - if (!context[model.graphql.typeName]) { - throw new Error( - `Model of typeName ${model.graphql.typeName} not found in Graphql context` - ); // TODO: unify error messages + const modelContext = context[model.name] || context[model.graphql.typeName]; + if (!modelContext) { + throw new Error(`${printModelName(model)} not found in Graphql context`); // TODO: unify error messages } - if (!context[model.graphql.typeName].connector) { + if (!modelContext.connector) { throw new Error( - `Model ${model.graphql.typeName} found in Graphql context but connector is not defined` + `${printModelName( + model + )} found in Graphql context but connector is not defined` ); // TODO: unify error messages } - return context[model.graphql.typeName].connector; + return modelContext.connector; }; /**