Skip to content

Commit

Permalink
Encourage project-specific models folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tjme committed Mar 16, 2021
1 parent a9b149d commit da16f79
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO test;
\q
```

- create a database sources directory: `mkdir models`

## Example [Vue](https://vuejs.org)/[PrimeVue](https://github.com/primefaces/primevue) front-end, using [Vite](https://github.com/vitejs/vite) and [Villus](https://github.com/logaretm/villus)

- install Vue CLI: `yarn global add @vue/cli`
- initialize: `yarn create @vitejs/app --template vue-ts vue-toh`
- change to your project directory: `cd vue-toh`
- you are advised to initialize version control to help track source code changes (e.g. `git init`)
- create a data sources directory: `mkdir models`
- copy or merge the template's [package~.json](templates/primevue/package~.json) as your new ./package.json (or selectively copy/paste, especially the following scripts)
- generate JSON schema files: `yarn gen-json` (alternatively, if not using PostGraphile, use apollo `yarn gen-json-a` or get-graphql-schema `yarn gen-json-g`)
- generate GraphQL schema files: `yarn gen-gql` (alternatively, ditto the above)
Expand All @@ -76,9 +75,10 @@ GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO test;
- install angular CLI globally (again, you may need elevated privileges) `yarn global add @angular/cli`
- create your Angular project: `ng new ng-toh`
- change to your project directory: `cd ng-toh`
- you are advised to initialize version control to help track source code changes (e.g. `git init`)
- create a data sources directory: `mkdir models`
- if using yarn, configure: `ng config cli.packageManager yarn`
- You are advised to initialize version control to help track source code changes (e.g. `git init`)
- generate JSON and GraphQL schema files: `yarn postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-json ../models/schema.json --export-schema-graphql ../models/schema.gql`
- generate JSON and GraphQL schema files: `yarn postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-json models/schema.json --export-schema-graphql models/schema.gql`
- run the GraphQL server (in the background): `yarn postgraphile -c postgres://test:testpass@/toh -s public -o &`
- optionally you can click on the link to GraphiQL generated by the above, and explore the server by entering queries such as: `{allHeroes{nodes{nodeId,id,name}}}`
- add Angular and Material dependencies: `yarn add rxjs rxjs-compat @angular/material @angular/cdk graphql graphql-tag apollo-client apollo-angular apollo-link apollo-angular-link-http apollo-cache-inmemory`
Expand All @@ -100,7 +100,8 @@ GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO test;
- create your Vue project: `vue create vuetify-toh`, and select manual with babel, pwa, router eslint, (or select the equivalent using `vue ui`), but not with TypeScript currently, due to Vue 3 beta limitations
- change to your project directory: `cd vuetify-toh`
- You are advised to initialize version control to help track source code changes (e.g. `git init`)
- generate JSON and GraphQL schema files: `yarn postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-json ../models/schema.json --export-schema-graphql ../models/schema.gql`
- create a data sources directory: `mkdir models`
- generate JSON and GraphQL schema files: `yarn postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-json models/schema.json --export-schema-graphql models/schema.gql`
- run the GraphQL server (in the background): `yarn postgraphile -c postgres://test:testpass@/toh -s public -o &`
- optionally you can click on the link to GraphiQL generated by the above, and explore the server by entering queries such as: `{allHeroes{nodes{nodeId,id,name}}}`
- install Vue v3 beta plugin: `vue add vue-next`
Expand All @@ -121,9 +122,9 @@ The `opinionate meta` sub-command can be used in a number of ways to help manage

### Example usage of the opinionate meta command

You can generate an overlay file with default metadata using `opinionate meta --overlay-out ../models/overlayOut.json`, similarly you can generate a SQL script to create table and field comments containing metadata using `opinionate meta --comments-out ../models/comments.pgsql`. These assume there is a `../models/schema.json` file describing the GraphQL schema (as produced by PostGraphile), otherwise you should use the `--schema` option to specify an alternative location.
You can generate an overlay file with default metadata using `opinionate meta --overlay-out models/overlayOut.json`, similarly you can generate a SQL script to create table and field comments containing metadata using `opinionate meta --comments-out models/comments.pgsql`. These assume there is a `models/schema.json` file describing the GraphQL schema (as produced by PostGraphile), otherwise you should use the `--schema` option to specify an alternative location.

You can define your own metadata structure (e.g. to extend the metadata available to the code generating templates), and this can include code to specify default values. This can simplify the templates, and has the added benefit of allowing some of those settings to be further customized (e.g. in the overlay file) before the final code is generated. Remember though, to add the metadata parameter to both commands, e.g. `opinionate meta --default-meta ../models/customMeta` and `opinionate gen --default-meta ../models/customMeta`. Note that the metadata file is NOT in standard JSON format.
You can define your own metadata structure (e.g. to extend the metadata available to the code generating templates), and this can include code to specify default values. This can simplify the templates, and has the added benefit of allowing some of those settings to be further customized (e.g. in the overlay file) before the final code is generated. Remember though, to add the metadata parameter to both commands, e.g. `opinionate meta --default-meta models/customMeta` and `opinionate gen --default-meta models/customMeta`. Note that the metadata file is NOT in standard JSON format.

Note that you can use `opinionate meta -h` for more help, especially on defaults and options for the locations of files to read and write.

Expand Down
4 changes: 2 additions & 2 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
overwrite: true
schema: "../models/schema.gql"
schema: "models/schema.gql"
generates:
../models/types.d.ts:
models/types.d.ts:
plugins:
- "typescript"
2 changes: 1 addition & 1 deletion dist/bin/opinionate-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppGen {
.version(this.package.version)
.option("-t, --template <dir>", "folder from which to fetch the template(s)", "./template")
.option("-w, --target <dir>", "folder in which to write the generated code file(s)", "./")
.option("-s, --schema <file>", "JSON file to read (base) schema from", "../models/schema.json")
.option("-s, --schema <file>", "JSON file to read (base) schema from", "models/schema.json")
.option("-o, --overlay <file>", "JSON file to read, defining additions to the schema.data.__schema.types (especially metadata)")
.option("-d, --default-meta <file>", "ES6 template file defining the default metadata (used for each type, in the absence of any other sources)")
.parse(process.argv);
Expand Down
2 changes: 1 addition & 1 deletion dist/bin/opinionate-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AppMetaMerge {
initialize() {
this.program
.version(this.package.version)
.option("-s, --schema <file>", "JSON file to read (base) schema from", "../models/schema.json")
.option("-s, --schema <file>", "JSON file to read (base) schema from", "models/schema.json")
.option("-o, --overlay <file>", "JSON file to read, defining additions to the schema.data.__schema.types (especially metadata)")
.option("-d, --default-meta <file>", "ES6 template file defining the metadata structure and default values (used for each type, in the absence of any other sources)")
.option("-m, --schema-out <file>", "JSON file to write, defining schema with merged in metadata")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opinionate",
"version": "0.3.4",
"version": "0.3.5",
"description": "Automated full stack application code generator",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/opinionate-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AppGen {
.version(this.package.version)
.option("-t, --template <dir>", "folder from which to fetch the template(s)", "./template")
.option("-w, --target <dir>", "folder in which to write the generated code file(s)", "./")
.option("-s, --schema <file>", "JSON file to read (base) schema from", "../models/schema.json")
.option("-s, --schema <file>", "JSON file to read (base) schema from", "models/schema.json")
.option("-o, --overlay <file>", "JSON file to read, defining additions to the schema.data.__schema.types (especially metadata)")
.option("-d, --default-meta <file>","ES6 template file defining the default metadata (used for each type, in the absence of any other sources)")
.parse(process.argv);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/opinionate-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AppMetaMerge {
public initialize() {
this.program
.version(this.package.version)
.option("-s, --schema <file>", "JSON file to read (base) schema from", "../models/schema.json")
.option("-s, --schema <file>", "JSON file to read (base) schema from", "models/schema.json")
.option("-o, --overlay <file>", "JSON file to read, defining additions to the schema.data.__schema.types (especially metadata)")
.option("-d, --default-meta <file>","ES6 template file defining the metadata structure and default values (used for each type, in the absence of any other sources)")
.option("-m, --schema-out <file>", "JSON file to write, defining schema with merged in metadata")
Expand Down
8 changes: 4 additions & 4 deletions templates/primevue/package~.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"path": "/graphql"
},
"scripts": {
"gen-json": "postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-json ../models/schema.json",
"gen-gql": "postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-graphql ../models/schema.gql",
"gen-json": "postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-json models/schema.json",
"gen-gql": "postgraphile -c postgres://test:testpass@/toh -s public -X --export-schema-graphql models/schema.gql",
"gen-json-a": "apollo client:download-schema --endpoint=$npm_package_gql_api_hostname models/schema.json",
"gen-gql-a": "apollo client:download-schema --endpoint=$npm_package_gql_api_hostname models/schema.gql",
"gen-json-g": "get-graphql-schema $npm_package_gql_api_hostname -j > models/schema.json",
"gen-gql-g": "get-graphql-schema $npm_package_gql_api_hostname > models/schema.gql",
"gen-ts": "graphql-codegen --config node_modules/opinionate/codegen.yml",
"gen-overlay": "opinionate meta --overlay-out ../models/overlayOut.json",
"gen": "opinionate gen --template node_modules/opinionate/templates/primevue -o ../models/overlayOut.json",
"gen-overlay": "opinionate meta --overlay-out models/overlayOut.json",
"gen": "opinionate gen --template node_modules/opinionate/templates/primevue -o models/overlayOut.json",
"backend": "postgraphile -c postgres://test:testpass@/i2k -s dbo --enhance-graphiql -n $npm_package_gql_api_hostname -p $npm_package_gql_api_port -q $npm_package_gql_api_path",
"dev": "vite",
"build": "vuedx-typecheck . && vite build",
Expand Down

0 comments on commit da16f79

Please sign in to comment.