Skip to content

Commit

Permalink
Fix missing implicit types (#211)
Browse files Browse the repository at this point in the history
We have really appreciated this lib, and have been using it since it
came out! I think I found a typing fix to make it even more usable.

When trying to bundle up the generated code with `tsc
--declaration=true`, ts couldn't figure out the `disableQuery` type and
the `BaseInfiniteQueryOptions` type. This is able to be resolved if the
exported queries are explicitly typed.

This also will resolve #209 🎉 

Let me know if there is anything else I can do to this PR!
  • Loading branch information
mckelveygreg authored Oct 6, 2023
1 parent d61b857 commit 6065baa
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.BigIntService";
Expand Down Expand Up @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: BigIntService });
/**
* @generated from rpc connectrpc.eliza.v1.BigIntService.Count
*/
export const count = {
export const count: UnaryFunctionsWithHooks<CountRequest, CountResponse> = {
...$queryService.count,
...createUnaryHooks($queryService.count),
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.ElizaService";
Expand Down Expand Up @@ -102,7 +103,7 @@ const $queryService = createQueryService({ service: ElizaService });
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.Say
*/
export const say = {
export const say: UnaryFunctionsWithHooks<SayRequest, SayResponse> = {
...$queryService.say,
...createUnaryHooks($queryService.say),
};
Expand All @@ -112,7 +113,7 @@ export const say = {
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.SayAgain
*/
export const sayAgain = {
export const sayAgain: UnaryFunctionsWithHooks<SayRequest, SayResponse> = {
...$queryService.sayAgain,
...createUnaryHooks($queryService.sayAgain),
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.Haberdasher";
Expand Down Expand Up @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: Haberdasher });
/**
* @generated from rpc connectrpc.eliza.v1.Haberdasher.Work
*/
export const work = {
export const work: UnaryFunctionsWithHooks<Nothing, Nothing> = {
...$queryService.work,
...createUnaryHooks($queryService.work),
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.PaginatedService";
Expand Down Expand Up @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: PaginatedService });
/**
* @generated from rpc connectrpc.eliza.v1.PaginatedService.List
*/
export const list = {
export const list: UnaryFunctionsWithHooks<ListRequest, ListResponse> = {
...$queryService.list,
...createUnaryHooks($queryService.list),
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.SecondService";
Expand Down Expand Up @@ -86,7 +87,7 @@ const $queryService = createQueryService({ service: SecondService });
*
* @generated from rpc connectrpc.eliza.v1.SecondService.Say
*/
export const say = {
export const say: UnaryFunctionsWithHooks<SayRequest, SayResponse> = {
...$queryService.say,
...createUnaryHooks($queryService.say),
};
3 changes: 2 additions & 1 deletion examples/react/basic/src/gen/eliza-Slouch_connectquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.Slouch";
Expand Down Expand Up @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: Slouch });
/**
* @generated from rpc connectrpc.eliza.v1.Slouch.Work
*/
export const work = {
export const work: UnaryFunctionsWithHooks<Nothing, Nothing> = {
...$queryService.work,
...createUnaryHooks($queryService.work),
};
3 changes: 2 additions & 1 deletion examples/react/basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"strict": true,
"target": "ESNext",
"useDefineForClassFields": true,
"types": ["jest", "node"]
"types": ["jest", "node"],
"declaration": true // necessary to check if generated code can be published
},
"include": ["src", "./*.config.ts", "__mocks__"],
"references": [{ "path": "./tsconfig.node.json" }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctionsWithHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.ElizaService";
Expand Down Expand Up @@ -92,7 +93,7 @@ const $queryService = createQueryService({ service: ElizaService });
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.Say
*/
export const say = {
export const say: UnaryFunctionsWithHooks<SayRequest, SayResponse> = {
...$queryService.say,
...createUnaryHooks($queryService.say),
};
14 changes: 10 additions & 4 deletions packages/protoc-gen-connect-query/src/generateTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const { safeIdentifier } = codegenInfo;
*/
const generateServiceFile =
(schema: Schema, protoFile: DescFile, extension: 'js' | 'ts') =>
(service: DescService) => {
(service: DescService) => {
const isTs = extension === "ts";
const f = schema.generateFile(
`${protoFile.name}-${localName(service)}_connectquery.${extension}`,
);
Expand Down Expand Up @@ -63,7 +64,7 @@ const generateServiceFile =
f.print(" },");
}
f.print(" }");
f.print("}", extension === "ts" ? " as const" : "", ";");
f.print("}", isTs ? " as const" : "", ";");
f.print();

f.print(`const $queryService = `,
Expand All @@ -74,13 +75,18 @@ const generateServiceFile =
);
f.print();


service.methods
.filter((method) => method.methodKind === MethodKind.Unary)
.forEach((method, index, filteredMethods) => {
f.print(makeJsDoc(method));
const methodTsType = [
": ",
f.import('UnaryFunctionsWithHooks', '@connectrpc/connect-query'),
`<${method.input.name}, ${method.output.name}>`
]

f.print(
`export const ${safeIdentifier(localName(method))} = { `,
`export const ${safeIdentifier(localName(method))}`, ...(isTs ? methodTsType : []), ` = { `,
` ...$queryService.${localName(method)},`,
` ...`, f.import('createUnaryHooks', '@connectrpc/connect-query'),`($queryService.${localName(method)})`,
`};`
Expand Down

0 comments on commit 6065baa

Please sign in to comment.