Skip to content

Commit

Permalink
Improved label formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
oojacoboo committed Oct 14, 2023
1 parent c04e6b4 commit 1d0561f
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ let cachedSchema;


/**
* A hacked JSON.stringify that will preserve functions and regexes, etc.
* This isn't perfect for sure and could use improvment, but is sufficient for single layer sample
* data at the moment. It's really nasty, I'm aware.
* A hacked JSON.stringify that will allow us to print raw javascript expressions without quoting
* them as strings, essentially allowing us to add dynamic execution of data.
*
* @param {Object|Array} input
*
Expand Down Expand Up @@ -77,6 +76,23 @@ const indentString = (string, spaces = 0) => {
}


/**
* Cleans up the label with better formatting.
*
* @param {String} label
*
* @returns {String}
*/
const inflectLabel = (label) => {
label = inflection.transform(label, ['underscore', 'titleize']);

// Remove any trailing " Id" or " Key" from the label
label = label.replace(/ (Id|Key)$/, '');

return label;
}


/**
* Gets the GraphQL request configuration and performs necessary validation.
*
Expand Down Expand Up @@ -428,7 +444,7 @@ const getInputFields = (
inputFields.push(new InputField({
key: `${fieldDepth.length > 1 ? fieldDepth.slice(-1) + '__' : ''}${typeDetails.fieldName}`,
...(forGQL && {field: typeDetails.fieldName}),
label: inflection.transform(typeDetails.fieldName, ['underscore', 'titleize']),
label: inflectLabel(typeDetails.fieldName),
type: typeDetails.scalarType,
// typeDetails won't be processed for outermost type on children, since the getTypeDetails
// attempts to get the innermost type, while gathering important information, like isRequired.
Expand All @@ -452,7 +468,7 @@ const getInputFields = (
inputFields.push(new InputField({
key: `${fieldDepth.length > 1 ? fieldDepth.join('.') + '.' : ''}${typeDetails.fieldName}`,
...(forGQL && {field: typeDetails.fieldName}),
label: inflection.transform(typeDetails.fieldName, ['underscore', 'titleize']),
label: inflectLabel(typeDetails.fieldName),
// typeDetails won't be processed for outermost type on children, since the getTypeDetails
// attempts to get the innermost type, while gathering important information, like isRequired.
// To get the isRequired value, we just check the outer field (typeDetails in this case).
Expand Down Expand Up @@ -535,7 +551,7 @@ const getOutputFields = (type, forGQL = false) => {

return [new OutputField({
key: typeDetails.typeName === 'ID' ? 'id' : 'value', // GraphQL implicity returns don't have a field - default to 'value'
label: inflection.transform(type.name, ['underscore', 'titleize']),
label: inflectLabel(type.name),
type: typeDetails.scalarType,
...(typeDetails.enumValues.length && {choices: typeDetails.enumValues}),
// required: typeDetails.isRequired, // Will require sample data
Expand All @@ -560,7 +576,7 @@ const getOutputFields = (type, forGQL = false) => {

outputFields.push(new OutputField({
key: typeDetails.fieldName,
label: inflection.transform(field.name, ['underscore', 'titleize']),
label: inflectLabel(field.name),
type: typeDetails.scalarType,
...(typeDetails.enumValues.length && {choices: typeDetails.enumValues}),
// required: typeDetails.isRequired, // Will require sample data
Expand Down

0 comments on commit 1d0561f

Please sign in to comment.