This is just a question. Any particular reason why types in typescript types are defined with snake case? This goes against standards, where types have upper case letters. Makes the code smell a bit. Is there maybe a switch where I can save it in camel case?
var a: b // ouch !!
var a: B // yeiiiii
Yeah this change seems to have taken place between 0.19 and 0.20 versions of apollo-codegen. Don't see any option to toggle it back to how it worked before. Downgrading to 0.19.1 fixed it.
Is it possible to create a toggle switch to change that behaviour back to the previous one?
It would take alot of work on migrating the complete codebase for the renamed Exports/classes.
@obiwan007
When I took a quick peak at the file that apollo cli outputs I thought it had changed everything to snake case, but this is not the case. The main Mutation, Mutation Variable, Query, Query Variable and Fragment types are all generated in CamelCase, however some extra types also get created to support these main types, and those are snake cased (I think to essentially namespace them).
So have another look to make sure the types you need aren't in the output file. In my case they were, I was just thrown off by all the other generated types with underscores.
@jbaxleyiii Why this issue was closed? I think user should have more control over what case is being used for types. Maybe some naming function to be used during generation? I agree that snake case is against standards...
After the codegen you can run
find $GRAPHQL_TYPE_DIR -type f -exec sed -i -r 's/_([a-z])/\U\1/gi' {} \;
to convert snake case to camel case
@mvanlonden thanks, nice workaround but would prefer to have something built into apollo cli :) and best would be to have full control over naming
@mvanlonden considering that not even my PR was considered and closed without much discussion I think that there is no will from developers to include this functionality, what I respect. As a result I also have a hacky solution, where "post-install" I run a script that updates the naming function to what I like.
console.log('Fixing apollo');
var apollo = fs.readFileSync('./node_modules/apollo-codegen-typescript/lib/language.js', {
encoding: 'utf-8'
});
apollo = apollo.replace(
'return scope.join("_");',
"return scope.map(s => s[0].toUpperCase() + s.substring(1)).join('_');"
);
fs.writeFileSync(`./node_modules/apollo-codegen-typescript/lib/language.js`, apollo, {
encoding: 'utf-8'
});
You can certainly try to create a more robust version of my attempt, configurable through apollo.config.js. I think that devs would take you more seriously then my bleak attempt to directly include CamelCase.
I've found this tool which works great! It solved all my problems and types that it produces are also namespaced which makes it even more elegant solution.
Most helpful comment
I've found this tool which works great! It solved all my problems and types that it produces are also namespaced which makes it even more elegant solution.