Skip to content

Commit

Permalink
Add allowFieldArgs
Browse files Browse the repository at this point in the history
Fields with arguments are ignored by default by the generator. With
--allowFieldArgs one can have approriate definitions generated for
such fields as well.
  • Loading branch information
beepsoft committed Aug 30, 2019
1 parent 0839161 commit 3ce41bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
23 changes: 13 additions & 10 deletions generator/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { existsSync } = require("fs");
const { resolve } = require("path");
const cosmiconfig = require('cosmiconfig');
const { existsSync } = require("fs")
const { resolve } = require("path")
const cosmiconfig = require("cosmiconfig")

const explorer = cosmiconfig('mst-gql');
const explorer = cosmiconfig("mst-gql")

const defaultConfig = {
excludes: [],
Expand All @@ -13,15 +13,16 @@ const defaultConfig = {
outDir: "src/models",
roots: [],
noReact: false,
allowFieldArgs: false
}

exports.getConfig = function getConfig() {
try {
const result = explorer.searchSync();
return result ? result.config : defaultConfig;
const result = explorer.searchSync()
return result ? result.config : defaultConfig
} catch (e) {
console.error(e.message);
return defaultConfig;
console.error(e.message)
return defaultConfig
}
}

Expand All @@ -35,9 +36,10 @@ exports.mergeConfigs = function mergeConfigs(args, config) {
const excludes = args["--excludes"]
? args["--excludes"].split(",").map(s => s.trim())
: config.excludes
const modelsOnly =!!args["--modelsOnly"] || config.modelsOnly
const modelsOnly = !!args["--modelsOnly"] || config.modelsOnly
const forceAll = !!args["--force"] || config.force
const noReact = !!args["--noReact"] || config.noReact
const allowFieldArgs = !!args["--allowFieldArgs"] || config.allowFieldArgs

return {
format,
Expand All @@ -46,6 +48,7 @@ exports.mergeConfigs = function mergeConfigs(args, config) {
roots,
excludes,
modelsOnly,
forceAll
forceAll,
allowFieldArgs
}
}
5 changes: 3 additions & 2 deletions generator/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function generate(
excludes = [],
generationDate = "a long long time ago...",
modelsOnly = false,
noReact = false
noReact = false,
allowFieldArgs = false
) {
excludes.push(...buildInExcludes)

Expand Down Expand Up @@ -294,7 +295,7 @@ ${generateFragments(name, primitiveFields, nonPrimitiveFields)}
let modelProperties = ""
if (type.fields) {
modelProperties = type.fields
.filter(field => field.args.length === 0)
.filter(field => (allowFieldArgs ? true : field.args.length === 0))
.map(field => handleField(field))
.join("\n")
}
Expand Down
20 changes: 16 additions & 4 deletions generator/mst-gql-scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require("fs")
const child_process = require("child_process")
const graphql = require("graphql")

const { getConfig, mergeConfigs } = require('./config');
const { getConfig, mergeConfigs } = require("./config")
const { generate, writeFiles } = require("./generate")

const definition = {
Expand All @@ -16,7 +16,8 @@ const definition = {
"--modelsOnly": Boolean,
"--force": Boolean,
"--noReact": Boolean,
"--separate": Boolean
"--separate": Boolean,
"--allowFieldArgs": Boolean
}

function main() {
Expand All @@ -33,7 +34,17 @@ function main() {
throw e
}

const { format, outDir, input, roots, excludes, modelsOnly, forceAll, noReact } = mergeConfigs(args, config);
const {
format,
outDir,
input,
roots,
excludes,
modelsOnly,
forceAll,
noReact,
allowFieldArgs
} = mergeConfigs(args, config)
const separate = !!args["--separate"]

console.log(
Expand Down Expand Up @@ -93,7 +104,8 @@ function main() {
excludes,
new Date().toUTCString(),
modelsOnly,
noReact
noReact,
allowFieldArgs
)
writeFiles(outDir, files, format, forceAll, true, separate)
}
Expand Down

0 comments on commit 3ce41bd

Please sign in to comment.