Code generation is essentially ‘the technique of using and writing programmes that write the source code’1.
This is usually achieved by the code generation module within the compiler, this compiler will generate code in a language that is different to the original source code. Normally this translation will be a high-level language to a low-level language e.g. machine code or assembly code2. However, there are a couple of exceptions to this rule, firstly a source to source compiler will translate a high-level language to another high-level language, while a decompiler will reverse the original operation3, i.e. it will translate a low-level into a high-level language.
Within GraphQL, the native code generation tools will take a ‘description of the type system and a set of GraphQL queries as an input and will then generate an output of a set of response models and queries requests. ‘4
Code generators are good for a lot of reasons, for example they are good for wrapping your data layer with a consistent code, e.g. ‘you can generate a function that executes the GraphQL document through a network interface, fetches the data and returns the response wrapped in a custom structure’.5
Code generation itself is a form of Model-to-Text generation, other uses of M2T transformation include implementation of code and documentation, model serialisation, model visualisation and exploration.6 There are currently a large number of different M2T languages that can be used today such as; JET, JET2, XSLT and Xtend . This transformation can be achieved in several different ways depending on your source metamodel and your destination language, for example, you can extract and generate Java code from a UML.