Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 06e97372f0 |
@@ -0,0 +1,33 @@
|
||||
import { knex } from "../../db";
|
||||
import { Knex } from "knex";
|
||||
|
||||
type Thing = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
// talk to the DB
|
||||
const repo =
|
||||
(db: Knex<Thing>) =>
|
||||
async (id: string): Promise<Thing | null> => {
|
||||
return (await db.where({ id }).first()) ?? null;
|
||||
};
|
||||
|
||||
// business / domain logic
|
||||
const service =
|
||||
(thingGetter: (id: string) => Promise<Thing | null>) =>
|
||||
async (id: string): Promise<Thing | null> => {
|
||||
return thingGetter(id);
|
||||
};
|
||||
|
||||
const getThingClient = (id: string | undefined): Knex => {
|
||||
if (!id) return knex;
|
||||
return knex;
|
||||
};
|
||||
|
||||
// graphql entry
|
||||
export const resolver = async (args: { id: string }): Promise<Thing> => {
|
||||
const thing = await service(repo(getThingClient(args.id)))(args.id);
|
||||
if (!thing) throw new Error("not found");
|
||||
return thing;
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Knex } from "knex";
|
||||
import { knex } from "../../db";
|
||||
type Thing = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type ServedThing = {
|
||||
foo: number;
|
||||
} & Thing;
|
||||
|
||||
// talk to the DB
|
||||
const repo =
|
||||
({ db }: { db: Knex<Thing> }) =>
|
||||
async (id: string): Promise<Thing | null> => {
|
||||
return (await db().where({ id }).first()) ?? null;
|
||||
};
|
||||
|
||||
// business / domain logic
|
||||
const service =
|
||||
({ thingGetter }: { thingGetter: (id: string) => Promise<Thing | null> }) =>
|
||||
async (id: string): Promise<ServedThing | null> => {
|
||||
const thing = await thingGetter(id);
|
||||
const foo = 123;
|
||||
return thing ? { ...thing, foo } : null;
|
||||
};
|
||||
|
||||
// graphql entry
|
||||
export const resolver = async (id: string): Promise<ServedThing> => {
|
||||
const thing = await service({ thingGetter: repo({ db: knex }) })(id);
|
||||
if (!thing) throw new Error("not found");
|
||||
return thing;
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
export type Thing = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type ThingRepo = {
|
||||
findThing: (id: string) => Promise<Thing | null>;
|
||||
queryThing: () => Promise<Thing[]>;
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Knex } from "knex";
|
||||
import { Thing } from "./domain";
|
||||
|
||||
const findThing =
|
||||
({ db }: { db: Knex }) =>
|
||||
async (id: string): Promise<Thing | null> => {
|
||||
return null;
|
||||
};
|
||||
|
||||
export const thingRepo = ({ db }: { db: Knex }) => ({
|
||||
findThing: findThing({ db }),
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { ServedThing, service } from "./services/service";
|
||||
import { thingRepo } from "./repo";
|
||||
import { knex } from "../../db";
|
||||
|
||||
export const resolver = async (id: string): Promise<ServedThing> => {
|
||||
const thing = await service({
|
||||
thingRepo: thingRepo({ db: knex }),
|
||||
})(id);
|
||||
if (!thing) throw new Error("not found");
|
||||
return thing;
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Thing, type ThingRepo } from "../domain";
|
||||
|
||||
export type ServedThing = {
|
||||
foo: number;
|
||||
} & Thing;
|
||||
|
||||
export const service =
|
||||
({ thingRepo }: { thingRepo: Pick<ThingRepo, "findThing"> }) =>
|
||||
async (id: string): Promise<ServedThing | null> => {
|
||||
const thing = await thingRepo.findThing(id);
|
||||
const foo = 123;
|
||||
return thing ? { ...thing, foo } : null;
|
||||
};
|
||||
|
||||
export const service2 = ({
|
||||
thingRepo,
|
||||
}: {
|
||||
thingRepo: Pick<ThingRepo, "queryThing">;
|
||||
}) => {};
|
||||
@@ -0,0 +1,24 @@
|
||||
import { knex } from "../../db";
|
||||
type Thing = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
const Things = () => knex<Thing>("things");
|
||||
|
||||
// talk to the DB
|
||||
const repo = async (id: string): Promise<Thing | null> => {
|
||||
return (await Things().where({ id }).first()) ?? null;
|
||||
};
|
||||
|
||||
// business / domain logic
|
||||
const service = async (id: string): Promise<Thing | null> => {
|
||||
return repo(id);
|
||||
};
|
||||
|
||||
// graphql entry
|
||||
export const resolver = async (id: string): Promise<Thing> => {
|
||||
const thing = await service(id);
|
||||
if (!thing) throw new Error("not found");
|
||||
return thing;
|
||||
};
|
||||
Reference in New Issue
Block a user