Skip to content

Commit

Permalink
added support for various query name case strategies. (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
newhouse authored Apr 5, 2022
1 parent bf78ff9 commit 84e138c
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
12 changes: 12 additions & 0 deletions config-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ introspection:
# Default: false
removeTrailingPeriodFromDescriptions: false

# What manipulation of the query/mutation/subscription name would you like to perform to determine
# the name in query examples? Possible values:
#
# - none: will use same exact name as query)
# - capitalizeFirst: will capitalize the first letter and leave the rest.
# - capitalize: will capitalize the first letter and convert the rest to lower case.
# - camelCase
# - snakeCase
# - upperCase: all letters to upper case.
# - lowerCase: all letters to lower case.
queryNameStategy: none

#
#
##############################################
Expand Down
1 change: 1 addition & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ introspection:
schemaFile: ./examples/data/schema.gql
metadataFile: ./examples/data/metadata.json
dynamicExamplesProcessingModule: ./examples/customizations/examples
queryNameStategy: capitalizeFirst

extensions:
graphqlScalarExamples: true
Expand Down
39 changes: 37 additions & 2 deletions src/spectaql/generate-graphql-example-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@ import {
introspectionArgsToVariables,
introspectionQueryOrMutationToResponse,
} from '../lib/common'
import { capitalizeFirstLetter } from './utils'
import {
capitalizeFirstLetter,
capitalize,
camelCase,
snakeCase,
upperCase,
lowerCase,
} from './utils'

const QUERY_NAME_STATEGY_NONE = 'none'
const QUERY_NAME_STATEGY_CAPITALIZE_FIRST = 'capitalizeFirst'
const QUERY_NAME_STATEGY_CAPITALIZE = 'capitalize'
const QUERY_NAME_STATEGY_CAMELCASE = 'camelCase'
const QUERY_NAME_STATEGY_SNAKECASE = 'snakeCase'
const QUERY_NAME_STATEGY_UPPERCASE = 'upperCase'
const QUERY_NAME_STATEGY_LOWERCASE = 'lowerCase'

// Create a sane/friendly indentation of args based on how many there are, and the depth
function friendlyArgsString({ args, depth }) {
Expand All @@ -31,7 +46,10 @@ export function generateQuery({
introspectionResponse,
graphQLSchema,
extensions,
// queryNameStategy = QUERY_NAME_STATEGY_NONE,
queryNameStategy,
}) {
console.log({ queryNameStategy })
const introspectionManipulator = new IntrospectionManipulator(
introspectionResponse
)
Expand All @@ -51,7 +69,24 @@ export function generateQuery({

const cleanedQuery = queryResult.query.replace(/ : [\w![\]]+/g, '')

const query = `${prefix} ${capitalizeFirstLetter(field.name)}${
let queryName = field.name
if (!queryNameStategy || queryNameStategy === QUERY_NAME_STATEGY_NONE) {
// no op
} else if (queryNameStategy === QUERY_NAME_STATEGY_CAPITALIZE_FIRST) {
queryName = capitalizeFirstLetter(queryName)
} else if (queryNameStategy === QUERY_NAME_STATEGY_CAPITALIZE) {
queryName = capitalize(queryName)
} else if (queryNameStategy === QUERY_NAME_STATEGY_CAMELCASE) {
queryName = camelCase(queryName)
} else if (queryNameStategy === QUERY_NAME_STATEGY_SNAKECASE) {
queryName = snakeCase(queryName)
} else if (queryNameStategy === QUERY_NAME_STATEGY_UPPERCASE) {
queryName = upperCase(queryName)
} else if (queryNameStategy === QUERY_NAME_STATEGY_LOWERCASE) {
queryName = lowerCase(queryName)
}

const query = `${prefix} ${queryName}${
argStr ? `${argStr}` : ''
} {\n${cleanedQuery}}`

Expand Down
14 changes: 10 additions & 4 deletions src/spectaql/graphql-loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ export const loadIntrospectionResponseFromUrl = ({ headers, url }) => {
export const graphQLSchemaFromIntrospectionResponse = (
introspectionResponse
) => {
return buildClientSchema(
normalizeIntrospectionQueryResult(introspectionResponse),
{ assumeValid: true }
)
try {
return buildClientSchema(
normalizeIntrospectionQueryResult(introspectionResponse),
{ assumeValid: true }
)
} catch (err) {
console.log('Here is your Introspection Query Response:')
console.log(JSON.stringify(introspectionResponse))
throw err
}
}

// For some reason, if there's a non-standard queryType or mutationType
Expand Down
3 changes: 2 additions & 1 deletion src/spectaql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function run(opts) {
const { logo, favicon, specData: spec, themeDir } = opts

const {
introspection: { url: introspectionUrl },
introspection: { url: introspectionUrl, queryNameStategy },
extensions = {},
servers = [],
info = {},
Expand Down Expand Up @@ -69,6 +69,7 @@ function run(opts) {
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})

const data = {
Expand Down
32 changes: 30 additions & 2 deletions src/spectaql/pre-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export default function preProcess({
introspectionResponse,
graphQLSchema,
extensions = {},
queryNameStategy,
}) {
handleItems(items, {
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
}

Expand All @@ -24,6 +26,7 @@ function handleItems(
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
} = {}
) {
if (!Array.isArray(items)) {
Expand All @@ -37,13 +40,21 @@ function handleItems(
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
}
}

function handleItem(
item,
{ depth, names, introspectionResponse, graphQLSchema, extensions }
{
depth,
names,
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
}
) {
if (!item) {
return
Expand All @@ -68,6 +79,7 @@ function handleItem(
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
}

Expand All @@ -76,14 +88,21 @@ function handleItem(
let anchorPrefix
if (item.isQuery) {
anchorPrefix = 'query'
addQueryToItem({ item, introspectionResponse, graphQLSchema, extensions })
addQueryToItem({
item,
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
} else if (item.isMutation) {
anchorPrefix = 'mutation'
addMutationToItem({
item,
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
} else if (item.isSubscription) {
anchorPrefix = 'subscription'
Expand All @@ -92,6 +111,7 @@ function handleItem(
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
} else {
// It's a definition
Expand All @@ -112,13 +132,15 @@ function addQueryToItem({
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
}) {
return _addQueryToItem({
item,
flavor: 'query',
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
}

Expand All @@ -127,13 +149,15 @@ function addMutationToItem({
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
}) {
return _addQueryToItem({
item,
flavor: 'mutation',
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
}

Expand All @@ -142,13 +166,15 @@ function addSubscriptionToItem({
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
}) {
return _addQueryToItem({
item,
flavor: 'subscription',
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
}

Expand All @@ -158,13 +184,15 @@ function _addQueryToItem({
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
}) {
const stuff = generateQueryExample({
prefix: flavor,
field: item,
introspectionResponse,
graphQLSchema,
extensions,
queryNameStategy,
})
const { query, variables, response } = stuff

Expand Down
21 changes: 21 additions & 0 deletions src/spectaql/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import fs from 'fs'
import _ from 'lodash'

const cwd = process.cwd()

Expand Down Expand Up @@ -142,3 +143,23 @@ export function relative(from, to) {
export function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}

export function capitalize(string) {
return _.capitalize(string)
}

export function camelCase(string) {
return _.camelCase(string)
}

export function snakeCase(string) {
return _.snakeCase(string)
}

export function upperCase(string) {
return string.toUpperCase()
}

export function lowerCase(string) {
return string.toLowerCase()
}

0 comments on commit 84e138c

Please sign in to comment.