Skip to content

Commit

Permalink
Merge pull request #8 from gatewayapps/cradle-0.4.0
Browse files Browse the repository at this point in the history
Made changes to comply with cradle@0.4.0
  • Loading branch information
danielgary authored Jan 14, 2019
2 parents fe11af8 + 91f736b commit 121a3b1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gatewayapps/cradle-apollo-emitter",
"version": "0.3.1",
"version": "0.4.0",
"description": "",
"main": "./dist/index.js",
"scripts": {
Expand All @@ -19,7 +19,7 @@
},
"homepage": "https://github.com/gatewayapps/cradle-apollo-emitter#readme",
"devDependencies": {
"@gatewayapps/cradle": "^0.2.2",
"@gatewayapps/cradle": "^0.4.0",
"@types/colors": "^1.2.1",
"@types/fs-extra": "^5.0.4",
"@types/lodash": "^4.14.117",
Expand Down
52 changes: 26 additions & 26 deletions src/ApolloEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class ApolloModel {

export default class ApolloEmitter implements ICradleEmitter {
public console?: IConsole
public options!: EmitterOptions<IApolloEmitterOptions>
public options!: IApolloEmitterOptions

public getOperation
private Models: ApolloModel[] = []
private filesEmitted: string[] = []
public async prepareEmitter(options: EmitterOptions<IApolloEmitterOptions>, console: IConsole) {
constructor(options: IApolloEmitterOptions, console: IConsole) {
this.console = console
this.options = options
}
Expand All @@ -38,19 +38,19 @@ export default class ApolloEmitter implements ICradleEmitter {
if (this.shouldEmitModel(model)) {
this.writeTypeDefsForModel(model)

if (this.shouldGenerateResolvers(model) && this.options.options.shouldOutputResolverFiles !== false) {
if (this.shouldGenerateResolvers(model) && this.options.shouldOutputResolverFiles !== false) {
this.writeResolversForModel(model)
}
}
})

// write index.ts file for schema.

if (this.options.options.onComplete !== undefined) {
if (this.options.options.verbose) {
if (this.options.onComplete !== undefined) {
if (this.options.verbose) {
this.console!.log(`Calling onComplete with [ ${this.filesEmitted.join(', ') || 'Empty Array'} ]`)
}
this.options.options.onComplete(this.filesEmitted)
this.options.onComplete(this.filesEmitted)
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ ${localFields.join('\n')}
case 'String':
return `String${requiredToken}`
case 'UniqueIdentifier': {
if (this.options.options.useMongoObjectIds) {
if (this.options.useMongoObjectIds) {
return `ObjectID${requiredToken}`
} else {
return `ID${requiredToken}`
Expand All @@ -205,19 +205,19 @@ ${localFields.join('\n')}
}

private shouldEmitModel(model: CradleModel): boolean {
if (this.options.options.shouldEmitModel) {
return this.options.options.shouldEmitModel(model)
} else if (this.options.options.isModelToplevel) {
return this.options.options.isModelToplevel(model)
if (this.options.shouldEmitModel) {
return this.options.shouldEmitModel(model)
} else if (this.options.isModelToplevel) {
return this.options.isModelToplevel(model)
}
return true
}

private getDirectiveForResolver(model: CradleModel, resolverType: string, resolverName: string): string {
if (!this.options.options.getDirectiveForResolver) {
if (!this.options.getDirectiveForResolver) {
return ''
}
return this.options.options.getDirectiveForResolver(model, resolverType, resolverName)
return this.options.getDirectiveForResolver(model, resolverType, resolverName)
}

private isBaseType(typeName: string) {
Expand All @@ -232,7 +232,7 @@ ${localFields.join('\n')}
return true
}
default: {
if (typeName === 'ObjectID' && this.options.options.useMongoObjectIds) {
if (typeName === 'ObjectID' && this.options.useMongoObjectIds) {
return true
}
return false
Expand Down Expand Up @@ -300,7 +300,7 @@ ${resultParts.join('\n')}

this.getIncludedReferencesNames(model).forEach((rn) => {
const r = model.References[rn]
const objectIdType = this.options.options.useMongoObjectIds ? 'ObjectID' : 'ID'
const objectIdType = this.options.useMongoObjectIds ? 'ObjectID' : 'ID'
switch (r.RelationType) {
case RelationTypes.SingleOn:
case RelationTypes.Single: {
Expand Down Expand Up @@ -341,9 +341,9 @@ ${resultParts.join('\n')}
const singularQueryNameBase = _.camelCase(pluralize(model.Name, 1))
const pluralQueryName = _.camelCase(pluralize(model.Name, 2))

const outputFilename = this.options.options.outputType === 'typescript' ? 'resolvers.ts' : 'resolvers.js'
const outputFilename = this.options.outputType === 'typescript' ? 'resolvers.ts' : 'resolvers.js'

const modelResolverFilePath = path.join(this.options.options.outputDirectory, model.Name, outputFilename)
const modelResolverFilePath = path.join(this.options.outputDirectory, model.Name, outputFilename)
const queries: string[] = [pluralQueryName, `${pluralQueryName}Meta`, singularQueryNameBase]
const mutations: string[] = []
const references: string[] = []
Expand All @@ -366,7 +366,7 @@ ${resultParts.join('\n')}
})
}

const exportClause = this.options.options.outputType === 'typescript' ? 'export default' : 'module.exports ='
const exportClause = this.options.outputType === 'typescript' ? 'export default' : 'module.exports ='

const resolverBody = `${exportClause} {
Query: {
Expand All @@ -384,7 +384,7 @@ ${resultParts.join('\n')}
}

private shouldGenerateResolvers(model: CradleModel) {
return !this.options.options.shouldGenerateResolvers || this.options.options.shouldGenerateResolvers(model)
return !this.options.shouldGenerateResolvers || this.options.shouldGenerateResolvers(model)
}

private writeTypeDefsForModel(model: CradleModel) {
Expand All @@ -396,24 +396,24 @@ ${resultParts.join('\n')}

const apolloSchema = _.compact([modelTypeDefs, modelQueries, modelMutations]).join('\n\n')

const typeDefsPath = join(this.options.options.outputDirectory, model.Name, 'typedefs.graphql')
const typeDefsPath = join(this.options.outputDirectory, model.Name, 'typedefs.graphql')

this.writeContentsToFile(apolloSchema, typeDefsPath)
}

private getIncludedPropertiesNames(model: CradleModel): string[] {
const fieldNames = Object.keys(model.Properties)
if (this.options.options.shouldTypeIncludeProperty) {
const filterFunc = this.options.options.shouldTypeIncludeProperty
if (this.options.shouldTypeIncludeProperty) {
const filterFunc = this.options.shouldTypeIncludeProperty
return fieldNames.filter((name) => filterFunc(model, name, model.Properties[name]))
}
return fieldNames
}

private getIncludedReferencesNames(model: CradleModel): string[] {
const referenceNames = model.References ? Object.keys(model.References) : []
if (this.options.options.shouldTypeIncludeReference) {
const filterFunc = this.options.options.shouldTypeIncludeReference
if (this.options.shouldTypeIncludeReference) {
const filterFunc = this.options.shouldTypeIncludeReference
return referenceNames.filter((name) => filterFunc(model, name, model.References[name]))
}

Expand All @@ -438,10 +438,10 @@ ${resultParts.join('\n')}
* @param path path to file
*/
private writeContentsToFile(contents: string, filePath: string) {
if (existsSync(filePath) && !this.options.options.overwriteExisting) {
if (existsSync(filePath) && !this.options.overwriteExisting) {
this.console!.warn(colors.gray(`Not writing ${filePath} as it already exists and overwrite existing is set to false.`))
} else {
if (this.options.options.verbose) {
if (this.options.verbose) {
this.console!.log(colors.green(`Writing to ${filePath}`))
this.console!.log(colors.yellow(contents))
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ApolloEmitter from './ApolloEmitter'
import { IApolloEmitterOptions } from './IApolloEmitterOptions'

export = ApolloEmitter

0 comments on commit 121a3b1

Please sign in to comment.