I know there’s already a variety of questions on these forums about file structure - but I have a directed question around reliably exposing server methods, and I don’t quite understand the intended workflow since the import-based structure that has been espoused since Meteor 1.3.
Consider the case of having some methods on the server - because you don’t really have to do anything with a method on the server to create the DDP endpoint, simply defining the method in a given file is typically all that’s done - export const myMethod = ..
. Now, since only explicitly imported files are included, you need to import './your-file'
for every file which defines a method. Sucks, but okay.
Next month, you come along and add two methods - a
and b
- and the a
method happens to call the b
method (it’s perfectly valid to call other meteor methods from each other). Unfortunately, you only remember to explicitly import the file containing a
in your server startup file - but things simply work since importing the file for a
naturally imports b
due to the dependency on it. This is no good, since now b
will only be defined if referenced in a
- but everyone will think it’s reliably there as its own method, like all the others.
Some time passes and someone else comes along and refactors a
which now no longer depends on b
. They test use cases for a
thoroughly, everything works, merge and deploy. However, production breaks, claiming b
is no longer a method - even though there were zero changes made to anything related to b
.
Now, sure, you can argue the cases that you should have more tests, do better testing, etc. - but this seems a fundamental flaw in the recommended server-side setup that you need to remember to explicitly import every method export to ensure you don’t accidentally remove a method from the server side that’s used on the client.
In a traditional API design, you’d need to explicitly expose backend methods in some way such that the endpoints (REST, GraphQL, whatever) simply wouldn’t exist without being referenced from some more global object which exposes or in some way sends requests to them - but Meteor is special in that it creates endpoints simply by the fact of defining the method, no other configuration necessary.
Long story long, the explicit import recommendations on the server-side seem broken to me - I don’t see a reason I wouldn’t want every file in every server
directory always included no matter what - there’s essentially no cost to server-side code size aside from some minor build/deployment time, but once it’s running, it doesn’t matter how much code there is - this isn’t the frontend where we’re passing big chunks of code over the wire. If you don’t want something to exist on the server, you should just delete the file anyway.
Has anyone else reached a reasonable solution to this aside from simply hoping you don’t screw up?