-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hydra-cli): export all model files from a single module (#348)
* feat(hydra-cli): export all model files from a single module affects: @dzlzv/hydra-cli all generated model classes are now exported from a separate 'model' module * fix(hydra-cli): add model to the module to the compiled package affects: @dzlzv/hydra-cli
- Loading branch information
Showing
16 changed files
with
219 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Debug from 'debug' | ||
import { ObjectType, WarthogModel } from '../model' | ||
import { GeneratorContext } from './SourcesGenerator' | ||
import { withNames } from './utils' | ||
|
||
const debug = Debug('qnode-cli:model-index-context') | ||
|
||
export function withModelNames( | ||
model: WarthogModel | ||
): { modelClasses: GeneratorContext[] } { | ||
const entities = [...model.interfaces, ...model.entities] | ||
return { | ||
modelClasses: entities.map((e) => withNames(e)), | ||
} | ||
} | ||
|
||
export function withEnumNames( | ||
model: WarthogModel | ||
): { enums: GeneratorContext[] } { | ||
return { enums: model.enums.map((en) => withNames(en)) } | ||
} | ||
|
||
export function withUnionNames( | ||
model: WarthogModel | ||
): { unions: GeneratorContext[] } { | ||
return { unions: model.unions.map((u) => withNames(u)) } | ||
} | ||
|
||
export function withVariantNames( | ||
model: WarthogModel | ||
): { variants: GeneratorContext[] } { | ||
return { variants: model.variants.map((v: ObjectType) => withNames(v)) } | ||
} | ||
|
||
export function indexContext(model: WarthogModel): GeneratorContext { | ||
const out = { | ||
...withModelNames(model), | ||
...withEnumNames(model), | ||
...withUnionNames(model), | ||
...withVariantNames(model), | ||
} | ||
debug(`Index context: ${JSON.stringify(out, null, 2)}`) | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
packages/hydra-cli/src/templates/entities/model-all.ts.mst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{#modelClasses}} | ||
import { {{className}} } from './{{kebabName}}/{{kebabName}}.model'; | ||
export { {{className}} }; | ||
{{/modelClasses}} | ||
|
||
|
||
{{#enums}} | ||
import { {{name}} } from './enums/enums'; | ||
export { {{name}} }; {{! we need to re-export enums for type-GraphQL to resolve types correctly }} | ||
{{/enums}} | ||
|
||
{{#variants}} | ||
import { {{name}} } from './variants/variants.model' | ||
export { {{name}} }; | ||
{{/variants}} | ||
|
||
{{#unions}} | ||
import { {{name}} } from './variants/variants.model' | ||
export { {{name}} }; | ||
{{/unions}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/hydra-cli/src/templates/scaffold/mappings/mappings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/hydra-cli/test/helpers/model-index-context.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { expect } from 'chai' | ||
import { SourcesGenerator } from '../../src/generate/SourcesGenerator' | ||
import { fromStringSchema } from './model' | ||
import { compact as c } from '../../src/generate/utils' | ||
|
||
describe('model index render', () => { | ||
before(() => { | ||
// bypass the annoying warthog checks... | ||
process.env.WARTHOG_APP_HOST = 'test' | ||
process.env.WARTHOG_APP_PORT = 'test' | ||
process.env.WARTHOG_DB_HOST = 'test' | ||
process.env.DRY_RUN = 'true' | ||
}) | ||
|
||
it('should render index file', () => { | ||
const model = fromStringSchema(` | ||
enum Network { | ||
BABYLON | ||
ALEXANDRIA | ||
ROME | ||
} | ||
union MyUnion = Var1 | Var2 | ||
type Var1 @variant { | ||
f1: BigInt! | ||
} | ||
type Var2 @variant { | ||
f2: String! | ||
} | ||
type MyEntity @entity { | ||
f3: String! | ||
} | ||
interface MyInterface @entity { | ||
f4: String! | ||
} | ||
`) | ||
|
||
const rendered = new SourcesGenerator(model).generateModelIndex() | ||
|
||
expect(c(rendered)).to.include( | ||
c(`import { MyUnion } from './variants/variants.model`), | ||
'should import union types' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`import { Var1 } from './variants/variants.model`), | ||
'should import variant' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`import { Network } from './enums/enums`), | ||
'should import enums' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`export { Network }`), | ||
'should export enums' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`import { MyEntity } from './my-entity/my-entity.model`), | ||
'should import entities' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`export { MyEntity } `), | ||
'should export entities' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`import { MyInterface } from './my-interface/my-interface.model`), | ||
'should import interfaces' | ||
) | ||
expect(c(rendered)).to.include( | ||
c(`export { MyInterface } `), | ||
'should export interfaces' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.