-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: gRPC request stubs #6196
feat: gRPC request stubs #6196
Conversation
packages/insomnia/src/ui/components/panes/grpc-request-pane.tsx
Outdated
Show resolved
Hide resolved
class StackDepth { | ||
private readonly depths: { [type: string]: number }; | ||
readonly maxStackSize: number; | ||
|
||
constructor(maxStackSize = 3) { | ||
this.depths = {}; | ||
this.maxStackSize = maxStackSize; | ||
} | ||
|
||
incrementAndCheckIfOverMax(key: string): boolean { | ||
if (this.depths[key] > this.maxStackSize) { | ||
return true; | ||
} | ||
if (!this.depths[key]) { | ||
this.depths[key] = 0; | ||
} | ||
this.depths[key]++; | ||
return false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrapped this in a class and made sure it was passed in everywhere. The original implementation didn't always pass it down.
function isProtoType(resolvedType: Enum | Type | null): resolvedType is Type { | ||
if (!resolvedType) { | ||
return false; | ||
} | ||
const fieldsArray: keyof Type = 'fieldsArray'; | ||
return resolvedType instanceof Type || ( | ||
fieldsArray in resolvedType && Array.isArray(resolvedType[fieldsArray]) | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this
Not sure why this is needed, but my example proto file did not work with resolvedType instanceof Type
even though during debug it definitely seemed to be a Type
I'm not sure why the build is failing. |
@nbgraham Mac and Windows are sometimes flaky on GitHub due to the GitHub runners performance being slow. If Ubuntu passes should be fine 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎸
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Tested and works great!
Some future work we might want to revisit in the future:
- Replace the custom auto-mocking class with faker or similar logic which we can use for autogenerating requests for open-api, graphql variables etc
* feat: grpc request stubs * mark changed lines * add tests * fix infinite recursion * replace empty body * catch Type objects * button to replace stubs * flatten grpc editor * remove grpc-editor * trigger setvalue * rename mocks to example --------- Co-authored-by: jackkav <[email protected]> Co-authored-by: James Gatz <[email protected]>
changelog(New Features): Added gRPC request body Mock/stub generation for servers with reflection enabled
From discussion: #3475
I need input on the things in bold
Copies code from bloomrpc (is this okay?) to create gRPC method request mocks.
Adds a button to replace the request body with a method mock/stub if available.
I initially tried to set it only when the method changed, if the body was empty, but that was hard to predict as a user and seemed unreliable. Another option is to add a new tab with the stub as static content that the user could manually copy.
Thoughts on which approach to use?
Only works with server reflection!
Current the proto file loader uses
@grpc/proto-loader
(instead ofprotobufjs
directly) which returns a simple JSON object and so doesn't have access to theprotobuf.Service
type. I'm not quite sure how to convert toprotobuf
JS and preserve the options. Or if we should.I think we want this but I don't think it's exported. Maybe I should just copy it in?
Does this need to support file imports to be merged?