Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing implicit types #211

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctions,
UnaryHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.BigIntService";
Expand Down Expand Up @@ -48,7 +50,9 @@ const $queryService = createQueryService({ service: BigIntService });
/**
* @generated from rpc connectrpc.eliza.v1.BigIntService.Count
*/
export const count = {
...$queryService.count,
...createUnaryHooks($queryService.count),
};
export const count: UnaryFunctions<CountRequest, CountResponse> &
UnaryHooks<
CountRequest,
CountResponse,
UnaryFunctions<CountRequest, CountResponse>
> = { ...$queryService.count, ...createUnaryHooks($queryService.count) };
16 changes: 8 additions & 8 deletions examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctions,
UnaryHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.ElizaService";
Expand Down Expand Up @@ -102,17 +104,15 @@ const $queryService = createQueryService({ service: ElizaService });
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.Say
*/
export const say = {
...$queryService.say,
...createUnaryHooks($queryService.say),
};
export const say: UnaryFunctions<SayRequest, SayResponse> &
UnaryHooks<SayRequest, SayResponse, UnaryFunctions<SayRequest, SayResponse>> =
{ ...$queryService.say, ...createUnaryHooks($queryService.say) };

/**
* SayAgain is a unary RPC. Eliza responds to the prompt with a single sentence.
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.SayAgain
*/
export const sayAgain = {
...$queryService.sayAgain,
...createUnaryHooks($queryService.sayAgain),
};
export const sayAgain: UnaryFunctions<SayRequest, SayResponse> &
UnaryHooks<SayRequest, SayResponse, UnaryFunctions<SayRequest, SayResponse>> =
{ ...$queryService.sayAgain, ...createUnaryHooks($queryService.sayAgain) };
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctions,
UnaryHooks,
} from "@connectrpc/connect-query";

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

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

export const typeName = "connectrpc.eliza.v1.SecondService";
Expand Down Expand Up @@ -86,7 +88,6 @@ const $queryService = createQueryService({ service: SecondService });
*
* @generated from rpc connectrpc.eliza.v1.SecondService.Say
*/
export const say = {
...$queryService.say,
...createUnaryHooks($queryService.say),
};
export const say: UnaryFunctions<SayRequest, SayResponse> &
UnaryHooks<SayRequest, SayResponse, UnaryFunctions<SayRequest, SayResponse>> =
{ ...$queryService.say, ...createUnaryHooks($queryService.say) };
5 changes: 4 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,8 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctions,
UnaryHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.Slouch";
Expand Down Expand Up @@ -48,7 +50,8 @@ const $queryService = createQueryService({ service: Slouch });
/**
* @generated from rpc connectrpc.eliza.v1.Slouch.Work
*/
export const work = {
export const work: UnaryFunctions<Nothing, Nothing> &
UnaryHooks<Nothing, Nothing, UnaryFunctions<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,8 @@ import { MethodKind } from "@bufbuild/protobuf";
import {
createQueryService,
createUnaryHooks,
UnaryFunctions,
UnaryHooks,
} from "@connectrpc/connect-query";

export const typeName = "connectrpc.eliza.v1.ElizaService";
Expand Down Expand Up @@ -92,7 +94,6 @@ const $queryService = createQueryService({ service: ElizaService });
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.Say
*/
export const say = {
...$queryService.say,
...createUnaryHooks($queryService.say),
};
export const say: UnaryFunctions<SayRequest, SayResponse> &
UnaryHooks<SayRequest, SayResponse, UnaryFunctions<SayRequest, SayResponse>> =
{ ...$queryService.say, ...createUnaryHooks($queryService.say) };
20 changes: 15 additions & 5 deletions packages/protoc-gen-connect-query/src/generateTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { DescFile, DescService } from "@bufbuild/protobuf";
import type { DescFile, DescMethod, DescService } from "@bufbuild/protobuf";
import { codegenInfo, MethodIdempotency, MethodKind } from "@bufbuild/protobuf";
import type { Schema } from "@bufbuild/protoplugin";
import {
Expand All @@ -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,22 @@ const generateServiceFile =
);
f.print();


const unaryFunctionType = (method: DescMethod) => [f.import('UnaryFunctions', '@connectrpc/connect-query'), `<${method.input.name}, ${method.output.name}>`]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mckelveygreg This looks good, though I think we can simplify by using the helper type UnaryFunctionsWithHooks instead of UnaryFunctions<...> & UnaryHooks<...>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅 Ha, yeah that is much more straight forward!
Thanks!

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

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
Loading