Hello there!
Looking at the caching features of RESTDataSource I thought "how amazing would it be if I could just generate these from my Swagger/OpenAPI spec files!".
Sadly there is no such option yet and I am considering writing such a template for myself.
I opened this issue to start a discussion about this topic and maybe find other alternatives I have not thought about yet.
This would obviously not be a feature of apollo-server, yet I opened the issue here because I believe that it is most relevant to apollo-server users.
There are a few points that came to my mind when thinking about a general solution:
Hi Christian--I've worked on auto-generating features myself but had a quick question to clarify before detailing more.
Were you talking about auto-generating GraphQL types/schema code from the Swagger/OpenAPI spec files?
Or are you referring to generating the resolvers using Swagger/OpenAPI spec etc?
Or are you referring to generating both specifically?
Could you briefly detail the ideal outcome/generated output or process flow? That would help clarify what bits I could share that may be relevant.
ApolloServer 2 allows us to register dataSources (that will be available in the context after registration)
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
product: new MyAutoGeneratedProductAPI(),
})
});
I would like to create an OpenAPI Generator Template that creates data sources which can be used like that directly / extended with minor customizations & then be used
I found a few similar projects specific to Swagger/OpenAPI that may be worth reviewing or discussing here.
Some of my code is around generating the entire lifecycle (types/inputs/filter inputs, queries/mutations, along with the resolvers. (or parts, one time, or as needed).
My thinking is if you are auto-generating the dataSources, you would still need to manually create all of the resolvers, then all of the types/inputs etc. Would there be a reason for not generating those pieces also? Or are you using the dataSources in a different way than I'm understanding?
I'm not suggesting to not build this functionality, just seeing where the code/schema generation is most beneficial to integrate an existing api quickly.
I understand your point, for some use cases it might be interesting to generate the whole graphql schema.
Personally, I want to define the graphql schema myself, as it consolidates, omits, cherry picks, transforms, .. what my clients really require. Spending time on integration with other services is what I would like to reduce.
Auto generated dataSources are going to allow me to take advantage of all benefits of the RESTDataSource, while spending minimal effort on integration with other services.
I agree with those points, thanks for clarifying.
For generating the datasources, are you thinking static code generation that you would copy into a project, or more of an installed module with dynamic generation continuously/as needed?
I have this idea in mind since 6 months but never took the time 😅 But now, I have an amazing openapi/swagger to restful-react typed component generator here, so it should be quite "easy" to convert this to an apollo datasource generator (80% of the logic is there and it's typesafe 💯 )
My only question is about the API/CLI that we want, do you prefer have this in a separate package or part of apollo-server/packages/apollo-datasource-rest?
Sorry for my late response, I got busy over X-Mas and new years :see_no_evil:
@fabien0102 I am going to look at your code and see if I can adapt it to generate the DataSources.
I suppose it should be a plugin for the apollo cli and a package there?
@ChristianOezelt Don't worry, I think we are all very busy during christmas :wink: I've bootstrap the project locally before christmas to have the test env working (basically a migration of my restful-react code for now).
I also need to take some times to figure what we want to generate:
Bref, it's time to re-work on this project 😃
Love this idea. Did this ever get off the ground? I'd be interesting in contributing if I can. My RESTDataSource is incredible repetitive and would love to just point it to a Swagger/OpenAPI spec and call it good!
Hi! This seems like something that could be useful, but we don't intend on supporting it as a part of the DataSource itself, since the maintenance burden could be quite high. If this gets finished, we'd love to have a guest post on the Apollo blog or share it, though!
Since this isn't an issue specifically with the DataSource, I'm going to close this issue, but this looks like something that could be a great thing to discuss further in the spectrum community if you want to collaborate further on it!
Most helpful comment
Love this idea. Did this ever get off the ground? I'd be interesting in contributing if I can. My RESTDataSource is incredible repetitive and would love to just point it to a Swagger/OpenAPI spec and call it good!