Skip to content

Commit

Permalink
Include defaultResolver by default
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-dldc committed Oct 18, 2023
1 parent ca4b4c6 commit c822943
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ export interface IEngine<ErrorData> {

export interface IEngineOptions<ErrorData> {
readonly resolvers: readonly TResolver[];
readonly noDefaultResolvers?: boolean;
readonly schema: TInstanceAny;
readonly onError: TOnError<ErrorData>;
}

export function engine<ErrorData>({
resolvers = defaultResolvers,
resolvers: userResolvers,
schema,
onError,
noDefaultResolvers = false,
}: IEngineOptions<ErrorData>): IEngine<ErrorData> {
const resolvers = new Set(noDefaultResolvers ? userResolvers : [...defaultResolvers, ...userResolvers]);
const resolverByAbstract = new Map<string, TAbstractResolverFnAny>();
const resolverByEntity = new Map<TEntityAny, TEntityResolverFnAny>();

Expand Down
4 changes: 2 additions & 2 deletions tests/basic/engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputDataKey, basicResolver, defaultResolvers, engine } from '../../src/mod';
import { InputDataKey, basicResolver, engine } from '../../src/mod';
import { appSchema, authLogin, maybeMe, meWorspacesType, version } from './schema';

const authLoginImplem = basicResolver(authLogin.entity, (ctx) => {
Expand All @@ -25,6 +25,6 @@ const maybeMeImple = basicResolver(maybeMe.entity, () => {

export const appEngine = engine<null>({
schema: appSchema,
resolvers: [...defaultResolvers, authLoginImplem, meWorspacesTypeImplem, versionImplem, maybeMeImple],
resolvers: [authLoginImplem, meWorspacesTypeImplem, versionImplem, maybeMeImple],
onError: () => null,
});
43 changes: 43 additions & 0 deletions tests/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Erreur } from '@dldc/erreur';
import { expect, test } from 'vitest';
import { engine, entity, obj, query, resolver } from '../src/mod';

const item = entity.object({
id: entity.string(),
name: entity.string(),
});

const items = entity.list(item);

const itemsResolver = resolver(items.entity, [], () => {
return [{ id: '1' }, { id: '1' }];
});

const itemResolver = resolver(item.entity, [], (ctx) => {
const { id } = ctx.value as { id: string };
return {
id,
name: `Item ${id}`,
};
});

const appEngine = engine({
schema: items,
resolvers: [itemsResolver, itemResolver],
onError: (error) => {
return error instanceof Erreur ? error.toJSON() : 'Unknown error';
},
});

test('should resolve partial result in list', async () => {
const q = query(items).all((item) => item(({ id, name }) => obj({ id, name })));
const res = await appEngine.run(q);

expect(res).toEqual({
success: true,
result: [
{ id: '1', name: 'Item 1' },
{ id: '1', name: 'Item 1' },
],
});
});
4 changes: 2 additions & 2 deletions tests/loop/engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Erreur } from '@dldc/erreur';
import { defaultResolvers, engine, resolver } from '../../src/mod';
import { engine, resolver } from '../../src/mod';
import { app, schema, user } from './schema';

const userResolver = resolver(user.entity, [], () => {
Expand All @@ -16,7 +16,7 @@ const appResolver = resolver(app.entity, [], () => {

export const appEngine = engine<unknown>({
schema,
resolvers: [...defaultResolvers, userResolver, appResolver],
resolvers: [userResolver, appResolver],
onError: (error) => {
return error instanceof Erreur ? error.toJSON() : 'Unknown error';
},
Expand Down
4 changes: 2 additions & 2 deletions tests/nested/engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Erreur } from '@dldc/erreur';
import { Key, defaultResolvers, engine, resolver } from '../../src/mod';
import { Key, engine, resolver } from '../../src/mod';
import { appSchema, login, maybePersonal } from './schema';

interface IUser {
Expand Down Expand Up @@ -32,7 +32,7 @@ const maybePersonalResolver = resolver(maybePersonal.entity, [], (ctx) => {

export const appEngine = engine<unknown>({
schema: appSchema,
resolvers: [...defaultResolvers, loginResolver, maybePersonalResolver],
resolvers: [loginResolver, maybePersonalResolver],
onError: (error) => {
return error instanceof Erreur ? error.toJSON() : 'Unknown error';
},
Expand Down

0 comments on commit c822943

Please sign in to comment.