From ec6f12a525748a804ee850e7051e5e683af6158f Mon Sep 17 00:00:00 2001 From: Eric Dobbertin Date: Mon, 16 Dec 2019 16:47:05 -0600 Subject: [PATCH] Several fixes and improvements (#7) * feat: alphabetize definitions Signed-off-by: Eric Dobbertin * fix: remove duplicate request body contents Signed-off-by: Eric Dobbertin * feat: update to latest graphql-2-json-schema Adds rendering of built-in and custom scalars Signed-off-by: Eric Dobbertin * fix: fix extra params being shown in some examples Signed-off-by: Eric Dobbertin * fix: guard against undefined definitions Signed-off-by: Eric Dobbertin --- app/dociql/generate-example.js | 16 +++++++++++----- app/lib/preprocessor.js | 10 ++++++++++ app/views/partials/swagger/request-body.hbs | 2 +- package.json | 2 +- yarn.lock | 8 ++++---- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/dociql/generate-example.js b/app/dociql/generate-example.js index c9d75cf3..822557e5 100644 --- a/app/dociql/generate-example.js +++ b/app/dociql/generate-example.js @@ -16,8 +16,14 @@ function generateQueryInternal(field, expandGraph, arguments, depth, typeCounts const space = ' '.repeat(depth) var queryStr = space + field.name + // It's important to clone the array here. Otherwise we would + // be pushing arguments into the array passed by reference, + // which results in arguments from one query being incorrectly + // shown on another query's example. + const fieldArgs = [...arguments]; + if (field.args.length > 0) { - arguments.push(...field.args); + fieldArgs.push(...field.args); const argsStr = field.args.map(arg => `${arg.name}: $${arg.name}`).join(', '); queryStr += `(${argsStr})`; } @@ -31,7 +37,7 @@ function generateQueryInternal(field, expandGraph, arguments, depth, typeCounts if (!expandedField) return { query: "", - args: arguments + args: fieldArgs }; if (depth > 1) { @@ -53,7 +59,7 @@ function generateQueryInternal(field, expandGraph, arguments, depth, typeCounts return generateQueryInternal( childFields[key], expandGraph, - arguments, + fieldArgs, depth + 1, typeCounts).query }).join(""); @@ -63,7 +69,7 @@ function generateQueryInternal(field, expandGraph, arguments, depth, typeCounts return { query: queryStr + "\n", - args: arguments + args: fieldArgs }; } @@ -106,7 +112,7 @@ function generateExampleSchema(name, type, expandGraph, depth) { if (type instanceof GraphQLNonNull) return generateExampleSchema(name, type.ofType, expandGraph, depth + 1); if (type instanceof GraphQLList) { - var schema = generateExampleSchema(name, type.ofType, expandGraph, depth) // do not increment depth + var schema = generateExampleSchema(name, type.ofType, expandGraph, depth) // do not increment depth return schema ? { type: 'array', items: schema diff --git a/app/lib/preprocessor.js b/app/lib/preprocessor.js index 5f107919..780f64d7 100644 --- a/app/lib/preprocessor.js +++ b/app/lib/preprocessor.js @@ -77,6 +77,16 @@ module.exports = function(options, specData) { var replaceRefs = require("./resolve-references").replaceRefs; replaceRefs(path.dirname(copy["x-spec-path"]), copy, copy, "") + if (copy.definitions) { + var names = Object.keys(copy.definitions); + names.sort(); + var sortedDefinitions = {}; + for (const name of names) { + sortedDefinitions[name] = copy.definitions[name]; + } + copy.definitions = sortedDefinitions; + } + return copy; } diff --git a/app/views/partials/swagger/request-body.hbs b/app/views/partials/swagger/request-body.hbs index a9abee40..e2571ca7 100644 --- a/app/views/partials/swagger/request-body.hbs +++ b/app/views/partials/swagger/request-body.hbs @@ -40,7 +40,7 @@ {{else if schema.description}} {{md schema.description}} {{/if}} - {{>json-schema/body schema}} + {{!-- {{>json-schema/body schema}} --}} {{/if}} {{/with}} {{/if}} diff --git a/package.json b/package.json index 5d4380c1..de443849 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "commander": "*", "foundation-sites": "^6.5.0-rc.4", "graphql": "^14.1.1", - "graphql-2-json-schema": "^0.1.1", + "graphql-2-json-schema": "^0.2.0", "graphql-json-schema": "^0.1.2", "grunt": "^1.0.4", "grunt-compile-handlebars": "^2.0.2", diff --git a/yarn.lock b/yarn.lock index ca44c27f..5cc95f70 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1348,10 +1348,10 @@ graphlib@^2.1.7: dependencies: lodash "^4.17.15" -graphql-2-json-schema@^0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/graphql-2-json-schema/-/graphql-2-json-schema-0.1.1.tgz#77e1f9b2b9513bd38ac9e751e7e6f1096399a89f" - integrity sha512-Vy4gafvd1se+06QJgoVqjBwGrdpcp+MQ4t08U9Bb9FHvLJm3ZauISp72yXVyT9bj+BUkomaBeayGyedGiFfUOA== +graphql-2-json-schema@^0.2.0: + version "0.2.0" + resolved "https://registry.npmjs.org/graphql-2-json-schema/-/graphql-2-json-schema-0.2.0.tgz#ef02d7d422071be199bfb4d3c6719f3431005661" + integrity sha512-INEySjzTCb+oI8RTVBGQDHBgOiXtHlcR47MKvyWtaiJbptUBUXHlfO1njI89NeU+KIF6cUnAqmKgyYgyJVYwbA== dependencies: functional-json-schema "0.0.2-3" lodash "^4.17.10"