Skip to content

Commit

Permalink
data source fetching handles both model name and gql typename
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed May 2, 2022
1 parent 433ade1 commit 2e759b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions packages/graphql/server/contextBuilder/createDataSources.ts
Expand Up @@ -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),*/,
};
Expand Down
31 changes: 20 additions & 11 deletions packages/graphql/server/contextBuilder/utils.ts
Expand Up @@ -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
Expand All @@ -23,11 +30,12 @@ export const getModelDataSource = <TModel extends VulcanDocument>(
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;
};
Expand All @@ -51,17 +59,18 @@ export const getModelConnector = <TModel extends VulcanDocument>(
context,
model: VulcanGraphqlModel
): Connector<TModel> => {
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;
};

/**
Expand Down

0 comments on commit 2e759b6

Please sign in to comment.