-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from tsyringe singletons to injectables
- Loading branch information
Showing
19 changed files
with
388 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { registry, singleton } from "tsyringe"; | ||
export { singleton as injectable }; | ||
export { container, inject, injectAll } from "tsyringe"; | ||
import { injectable, registry } from "tsyringe"; | ||
// export { singleton as injectable }; | ||
export { container, inject, injectAll, injectable } from "tsyringe"; | ||
|
||
export const makeRegistryDecorator = | ||
(token: symbol) => (): ClassDecorator => (target) => { | ||
const targetConstructor = target as unknown as new () => unknown; | ||
singleton()(targetConstructor); | ||
injectable()(targetConstructor); | ||
// singleton()(targetConstructor); | ||
registry([{ token, useClass: targetConstructor }])(targetConstructor); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { GraphQLClient, RequestOptions } from "graphql-request"; | ||
import { gql } from "graphql-request"; | ||
type Maybe<T> = T | undefined; | ||
type InputMaybe<T> = T | undefined; | ||
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] }; | ||
type MakeOptional<T, K extends keyof T> = Omit<T, K> & { | ||
[SubKey in K]?: Maybe<T[SubKey]>; | ||
}; | ||
type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { | ||
[SubKey in K]: Maybe<T[SubKey]>; | ||
}; | ||
type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { | ||
[_ in K]?: never; | ||
}; | ||
type Incremental<T> = | ||
| T | ||
| { | ||
[P in keyof T]?: P extends " $fragmentName" | "__typename" ? T[P] : never; | ||
}; | ||
type GraphQLClientRequestHeaders = RequestOptions["requestHeaders"]; | ||
/** All built-in and custom scalars, mapped to their actual values */ | ||
type Scalars = { | ||
ID: { input: string; output: string }; | ||
String: { input: string; output: string }; | ||
Boolean: { input: boolean; output: boolean }; | ||
Int: { input: number; output: number }; | ||
Float: { input: number; output: number }; | ||
}; | ||
|
||
type IQuery = { | ||
__typename?: "Query"; | ||
hello: Scalars["String"]["output"]; | ||
users: IUser[]; | ||
}; | ||
|
||
type IUser = { | ||
__typename?: "User"; | ||
id: Scalars["ID"]["output"]; | ||
username: Scalars["String"]["output"]; | ||
}; | ||
|
||
type IHelloQueryVariables = Exact<{ [key: string]: never }>; | ||
|
||
type IHelloQuery = { __typename?: "Query"; hello: string }; | ||
|
||
type IGetUsersQueryVariables = Exact<{ [key: string]: never }>; | ||
|
||
type IGetUsersQuery = { | ||
__typename?: "Query"; | ||
users: Array<{ __typename?: "User"; id: string; username: string }>; | ||
}; | ||
|
||
const HelloDocument = gql` | ||
query hello { | ||
hello | ||
} | ||
`; | ||
const GetUsersDocument = gql` | ||
query getUsers { | ||
users { | ||
id | ||
username | ||
} | ||
} | ||
`; | ||
|
||
export type SdkFunctionWrapper = <T>( | ||
action: (requestHeaders?: Record<string, string>) => Promise<T>, | ||
operationName: string, | ||
operationType?: string, | ||
variables?: any, | ||
) => Promise<T>; | ||
|
||
const defaultWrapper: SdkFunctionWrapper = ( | ||
action, | ||
_operationName, | ||
_operationType, | ||
_variables, | ||
) => action(); | ||
|
||
export function getSdk( | ||
client: GraphQLClient, | ||
withWrapper: SdkFunctionWrapper = defaultWrapper, | ||
) { | ||
return { | ||
hello( | ||
variables?: IHelloQueryVariables, | ||
requestHeaders?: GraphQLClientRequestHeaders, | ||
): Promise<IHelloQuery> { | ||
return withWrapper( | ||
(wrappedRequestHeaders) => | ||
client.request<IHelloQuery>(HelloDocument, variables, { | ||
...requestHeaders, | ||
...wrappedRequestHeaders, | ||
}), | ||
"hello", | ||
"query", | ||
variables, | ||
); | ||
}, | ||
getUsers( | ||
variables?: IGetUsersQueryVariables, | ||
requestHeaders?: GraphQLClientRequestHeaders, | ||
): Promise<IGetUsersQuery> { | ||
return withWrapper( | ||
(wrappedRequestHeaders) => | ||
client.request<IGetUsersQuery>(GetUsersDocument, variables, { | ||
...requestHeaders, | ||
...wrappedRequestHeaders, | ||
}), | ||
"getUsers", | ||
"query", | ||
variables, | ||
); | ||
}, | ||
}; | ||
} | ||
export type Sdk = ReturnType<typeof getSdk>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
query hello { | ||
hello | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,42 @@ | ||
import { createHash } from "node:crypto"; | ||
import { container } from "@athenajs/core"; | ||
import { describe, expect, it } from "vitest"; | ||
import { withTestApp } from "../test-util.js"; | ||
import { UserResolver } from "./user-resolver.js"; | ||
import { UserService } from "./user-service.js"; | ||
|
||
const ids = { | ||
foo: createHash("md5").update("foo").digest("hex"), | ||
bar: createHash("md5").update("bar").digest("hex"), | ||
}; | ||
|
||
describe("UserResolver", () => { | ||
const userResolver = new UserResolver(); | ||
describe("users()", () => { | ||
it("should return some users with usernames", async () => { | ||
const expected = [{ username: "foo" }, { username: "bar" }]; | ||
const actual = await userResolver.users(); | ||
expect(actual).toEqual(expected); | ||
it("should resolve IDs", async () => { | ||
await withTestApp(async (sdk) => { | ||
const { users } = await sdk.getUsers(); | ||
expect(users).toEqual([ | ||
{ | ||
id: ids.foo, | ||
username: "foo", | ||
}, | ||
{ | ||
id: ids.bar, | ||
username: "bar", | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("#id", () => { | ||
it("should return md5 hashes of usernames", async () => { | ||
// md5 of my name. this is a demo | ||
const expected = ["534b44a19bf18d20b71ecc4eb77c572f"]; | ||
const actual = await userResolver.id([{ username: "alex" }]); | ||
expect(actual).toEqual(expected); | ||
it("should return mocked data", async () => { | ||
container.register(UserService, { | ||
useValue: { | ||
getMany: () => Promise.resolve([{ username: "mocked" }]), | ||
}, | ||
}); | ||
const resolver = container.resolve(UserResolver); | ||
const users = await resolver.users(); | ||
expect(users).toEqual([{ username: "mocked" }]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.