Code generation
graphql-magic generates a lot of utility code for you based on the models, in particular TypeScript types.
This can be done directly with npx gqm generate.
During the first run, the tool applies the following changes to the repo:
- Generate
.gqmrc.jsonfile. - Add local database connection variables to
.envfile. - Add generated folder to
.gitignore - Generate
models.tsfile (if not present). - Generate a basic
get-me.tsexample GraphQL query. - Generate the
execute.tsfile for the execution
With each application, it generates the following files in the configured "generated" folder:
schema.graphql- the schema of the API, for referencemodels.json- the final models array, including generated fields such as "id","createdBy"... for referenceapi/index.ts- the server-side model TypeScript typesclient/index.ts- the client-side TypeScript types for the provided queriesclient/mutations.ts- standard mutation queries for all modelsdb/index.ts- types for data from/to the databasedb/knex.ts- types to extend theknexquery builder
Whenever the models have been changed, it is necessary to regenerate this code.
It is recommended to create a package.json script and to always generate code after install (or with npm run generate):
"scripts": {
"bootstrap": "npm ci && npm run generate",
"generate": "gqm generate"
}