apollo-tooling icon indicating copy to clipboard operation
apollo-tooling copied to clipboard

Typescript types generated in snake case instead of camel case

Open tomitrescak opened this issue 8 years ago • 9 comments

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

tomitrescak avatar Jul 10 '18 12:07 tomitrescak

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.

danimbrogno-pml avatar Jul 20 '18 13:07 danimbrogno-pml

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 avatar Sep 17 '18 13:09 obiwan007

@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.

danimbrogno-pml avatar Sep 20 '18 21:09 danimbrogno-pml

@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...

lukejagodzinski avatar Nov 29 '18 15:11 lukejagodzinski

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 avatar Nov 30 '18 03:11 mvanlonden

@mvanlonden thanks, nice workaround but would prefer to have something built into apollo cli :) and best would be to have full control over naming

lukejagodzinski avatar Nov 30 '18 12:11 lukejagodzinski

@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'
});

tomitrescak avatar Dec 03 '18 00:12 tomitrescak

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.

tomitrescak avatar Dec 03 '18 00:12 tomitrescak

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.

lukejagodzinski avatar Dec 11 '18 16:12 lukejagodzinski