From b6d71984ef73ecadd4bf500a16a2d692c43179f1 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Sun, 14 Jun 2020 22:48:31 -0700 Subject: [PATCH] feat: Add constant info to graphql schema --- .../src/constantInfo.js | 32 +++++++++++++++++++ .../gatsby-source-newrelic-sdk/src/docInfo.js | 3 ++ 2 files changed, 35 insertions(+) create mode 100644 plugins/gatsby-source-newrelic-sdk/src/constantInfo.js diff --git a/plugins/gatsby-source-newrelic-sdk/src/constantInfo.js b/plugins/gatsby-source-newrelic-sdk/src/constantInfo.js new file mode 100644 index 000000000..43bb1f989 --- /dev/null +++ b/plugins/gatsby-source-newrelic-sdk/src/constantInfo.js @@ -0,0 +1,32 @@ +const IGNORED_PROPERTIES = [ + 'prototype', + 'length', + 'name', + 'propTypes', + 'getDerivedStateFromProps', + 'defaultProps', +]; + +exports.getConstants = (name, sdk) => { + const obj = sdk[name]; + + return Object.getOwnPropertyNames(obj) + .filter( + (member) => + !IGNORED_PROPERTIES.includes(member) && + typeof obj[member] !== 'function' + ) + .map((member) => { + const value = obj[member]; + + return { + name: `${name}.${member}`, + type: Array.isArray(value) ? 'array' : typeof value, + values: Array.isArray(value) + ? value.map((el) => JSON.stringify(el)) + : Object.getOwnPropertyNames(value).map( + (key) => `${key}: ${JSON.stringify(value[key])}` + ), + }; + }); +}; diff --git a/plugins/gatsby-source-newrelic-sdk/src/docInfo.js b/plugins/gatsby-source-newrelic-sdk/src/docInfo.js index 8b3b0aaee..be92ce9a1 100644 --- a/plugins/gatsby-source-newrelic-sdk/src/docInfo.js +++ b/plugins/gatsby-source-newrelic-sdk/src/docInfo.js @@ -2,6 +2,7 @@ const { getExamples } = require('./exampleInfo'); const { getTypeDefs } = require('./typeDefInfo'); const { getPropTypes } = require('./propTypeInfo'); const { getMethods } = require('./methodInfo'); +const { getConstants } = require('./constantInfo'); exports.getComponentDoc = (name, sdk) => { const component = sdk[name]; @@ -21,6 +22,7 @@ exports.getComponentDoc = (name, sdk) => { return { name, usage: `import { ${name} } from 'nr1'`, + constants: getConstants(name, sdk), description: component.__docs__.text, examples: getExamples(name, sdk), propTypes: getPropTypes(component), @@ -41,6 +43,7 @@ exports.getApiDoc = (name, sdk) => { return { name, usage: `import { ${name} } from 'nr1'`, + constants: getConstants(name, sdk), description: api.__docs__.text, examples: getExamples(name, sdk), methods: getMethods(name, sdk),