-
Notifications
You must be signed in to change notification settings - Fork 13
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
Usage with OpenAPI-Generator javascript SDK #58
Comments
I'm a bit at a loss - why would we generate a full external API client when this is a code generator for an api client library? This is primarily just to create an endpoint declaration and types that users can then enhance with hand-written invalidation rules, lifecycle events and data transformation logic, so the result would look like https://github.com/rtk-incubator/rtk-query-codegen/blob/next/test/fixtures/generated.ts#L2-L79 |
I'm kinda confused. @ybelenko , can you clarify what your specific concerns are, and what you're asking for here? |
I already have generated front client SDK with models and helper classes which looks like: var OpenApiPetstore = require('open_api_petstore');
var api = new OpenApiPetstore.AnotherFakeApi()
var client = new OpenApiPetstore.Client(); // {Client} client model
api.call123testSpecialTags(client).then(function(data) {
console.log('API called successfully. Returned data: ' + data);
}, function(error) {
console.error(error);
}); Ref to full OpenAPI Petstore source samples. So, it feels natural to customize My questions is, when I use both OpenAPI schema and OpenAPI generator to produce front client SDK should I also use your package? |
If you bring your own API client, you would usually use The API client you linked to seems to be not very suited for storage in Redux as it generates model classes for everything and we recommend not to store non-serializable values as classes in a redux store. Essentially everything returned from there would have to be converted to an object before storing it in Redux, which kinda makes it pretty pointless I think |
Yes, you right, but generated library returns model and response object in each endpoint call. Usually I put response body into Redux state instead of model. While models seems useful when it request param not a response.
Of course, I get it. So for me it's choice between your codegen and OpenAPI-Generator codegen. Should I wrap client SDK with |
x: builder.addQuery({
async queryFn(arg){
try {
const result = externalApi.callX(arg)
return { data: result }
} catch (e) {
return { error: error.message }
}
}
}) for each of your endpoints. Of course, you can write a little helper for yourself that will cut down on code quite a bit, but you would essentially be duplicating your generated api client. In the end, I'd try to generate the code once and see if it works for your api :) |
Is it typescript only? It's hard to read for me and I think typescript is overkill for a small projects. I use vanilla React JSX where possible. And what http client this package uses by default? Is it |
You can throw the generated TypeScript into something like the TypeScript playground to just get the compiled JavaScript (which is pretty much stripping away the type annotations). Personally I would use TypeScript before I even add a second file to a project. Especially when just writing
and then getting full autocomplete over what data the API returns when you write would be reason enough for me personally to go TypeScript.
|
I almost forgot that OpenAPI-Generator contains long list of TypeScript generators: Full list of client samples here I'm not here to advertise generator project, but want to describe my doubts as developer and user. The biggest advantage of OpenAPI Schema(formerly Swagger API) to me was ability to generate client libraries for any language. Today I need to accept that client generating is useless regarding Redux and maybe it's easier to proceed with your small package. I've found one advantage of client SDK recently, we use mustaches for templates, so it's really easy to overwrite model template and generate new build. |
Actually, the use of mustache made me not go for the code generator - I honestly evaluated it (and dozens others) before building this. For a guarantee of building correct TypeScript code, I did not want to rely on some string concatenation, but build a TypeScript AST and write that to a TypeScript file using the compiler itself. Also, the goal of this in combination of RTKQ is that you should never need to touch your templates, or the generated files. |
After reading this thread with interest wanted to ask a somewhat related question. I have an
With your system, I'd probably just need to remove the typescript-fetch here, have a script that also would run the rtk generation alongside the openapi node-express-server? Sorry if this is a dumb question, I'm new to a lot of the tools here. Do you guys also generate server code and use in conjunction with RTK generator? |
You are right, if you want to generate server code, you have to use both tools for that. |
For anyone interested in using plain JS: I created a script in my
|
@phryneas One advantage of having the generated API calls generated separately from the RTKQuery concerns might be that the generated client can be used from other part of the application, e.g. from some custom Redux middleware code. Are there any other approaches at the moment to do this? |
This is written in Redux. It is literally dispatching actions and storing the results in the store from where you can retrieve them. (This version you can also await mutations in middleware, from the next version on you'll also be able to await queries since it will remove a possible race condition.) We have a svelte example in the docs, there are people using this with angular and vue. You don't need an extra client for this, especially when using Redux. |
Great to hear about the upcoming await query feature! |
Yes, as soon as you are not interested in the cache result any more and it can be collected. |
So once the await queries lands, the pattern to make requests in custom middleware would be something like this? const { result, data } = await dispatch(api.endpoints.getPosts.initiate())
result.unsubscribe()
// use data |
Yup. That should also already work similarly in the current version of RTKQ, by selecting the result from the store after awaiting like this: await dispatch(api.endpoints.getPosts.initiate())
const { result, data } = api.endpoints.getPosts.select()(getState())
result.unsubscribe() Atm, there is just one race condition which will be resolved in the next version of RTQK: If you
|
@phryneas Maybe this was just an example, but calling Similarly, how do I reuse a selector specified in const firstPostFinder = (data: Posts[] | undefined) => data?.[0]
const { firstPost } = api.useGetPostsQuery(undefined, {
selectFromResult: ({ data }) => ({
firstPost: firstPostFinder(data),
}),
})
const getPostsSelector = api.endpoints.getPosts.select()
const firstPostSelector = createSelector(getPostsSelector, firstPost) |
Going very far off topic, but yes, About |
Maybe it's just me as a member of OpenAPI-Generator project, but I understood this package completely wrong from the first sight. I thought it's just a wrapper of some TypeScript generator from mentioned repo, but seems like you generate everything yourself?
If so, could you add small notice in main README that you don't use any generation libraries as a core. I think it might be important for developers who would like to use already generated API clients. Beside client SDKs from OpenAPI-Generator might be more stable and enhanced at current point.
The text was updated successfully, but these errors were encountered: