Skip to content

Commit

Permalink
Allow multiple entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-dldc committed Feb 8, 2025
1 parent 1ff8a63 commit 23a62b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/server/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface TEngineOptions {
graph: TGraphBaseAny;
resolvers: TResolver[];
operators?: TPrepareFromOperator[];
entry: string;
entry: string | string[]; // TODO: use graph object instead ?
}

export function createEngine(
Expand All @@ -35,6 +35,10 @@ export function createEngine(
entry,
}: TEngineOptions,
): TEngine {
const entries = Array.isArray(entry) ? entry : [entry];
if (entries.length === 0) {
throw new Error("You must provide at least one entry point");
}
const rootStructure = graph[ROOT];
const { getResolvers } = validateResolvers(rootStructure, resolvers);
const operators: TPrepareFromOperator[] = [
Expand All @@ -58,7 +62,7 @@ export function createEngine(
let variableCount = 0;
const mid = prepare(
{
entry,
entries,
rootGraph: graph,
rootStructure,
operators,
Expand Down
16 changes: 10 additions & 6 deletions src/server/erreur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type TGraphClientErreurData = {
} | {
kind: "InvalidEntry";
graph: TGraphBaseAny;
entry: string;
entries: string[];
requested: string;
} | {
kind: "InvalidUnionTypeQuery";
Expand Down Expand Up @@ -60,16 +60,20 @@ export function createInvalidQuery(

export function createInvalidEntry(
graph: TGraphBaseAny,
entry: string,
entries: string[],
requested: string,
): Error {
const entryMessage = entries.length === 1
? printValue(entries[0])
: `one of ${printValue(entries)}`;

return GraphClientErreurPrivate.setAndReturn(
new Error(
`Invalid entry, all queries should start from ${
printValue(entry)
} (requested: ${printValue(requested)})`,
`Invalid entry, all queries should start from ${entryMessage} (requested: ${
printValue(requested)
})`,
),
{ graph, kind: "InvalidEntry", entry, requested },
{ graph, kind: "InvalidEntry", entries, requested },
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type TPrepareFromOperator = (
) => TMiddleware | null;

export interface TPrepareContext {
entry: string;
entries: string[];
rootGraph: TGraphBaseAny;
rootStructure: TRootStructure;
operators: TPrepareFromOperator[];
Expand Down Expand Up @@ -97,8 +97,8 @@ const PREPARE_BY_STRUCTURE: TByStructureKind = {
if (typeof queryItem !== "string") {
return null;
}
if (queryItem !== context.entry) {
throw createInvalidEntry(graph, context.entry, queryItem);
if (!context.entries.includes(queryItem)) {
throw createInvalidEntry(graph, context.entries, queryItem);
}
return prepareObjectLike(context, graph, query, identity);
},
Expand Down

0 comments on commit 23a62b4

Please sign in to comment.