Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @spectaql directive to ingest metadata #439

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions config-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ introspection:
# - path/to/schema/part2.gql
schemaFile: path/to/schema.gql


# Some helpful options for those who are an "SDL-first" shop and/or want to get your metadata into
# SpectaQL during the SDL ingestion.
spectaqlDirective:
# Boolean indicating whether to enable and process the @spectaql directive
#
# Default: true
enable: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's enabled by default, all good if it's not there? Like this wont break existing installs out there, will it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's pretty flexible...


# String indicating the name to use for the SpectaQL directive
#
# Default: 'spectaql'
directiveName: 'spectaql'

# String indicating the name to use for the Type used in the "options" array that can be passed
# to the SpectaQL directive
#
# Default: 'SpectaQLOption'
optionsTypeName: 'SpectaQLOption'

# File containing Introspection Query response in JS module export, or JSON format
introspectionFile: path/to/introspection.js[on]

Expand Down
3 changes: 3 additions & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ introspection:
queryNameStrategy: capitalizeFirst
fieldExpansionDepth: 2

spectaqlDirective:
enable: true

extensions:
graphqlScalarExamples: true

Expand Down
6 changes: 5 additions & 1 deletion examples/data/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ interface MyInterface {

"`Markdown` and reference interpolation like [`[String!]!`]({{Types.String}}) and [`myQuery`]({{Queries.myQuery}}) and [`myMutation`]({{Mutations.myMutation}}) are supported"
type MyType implements MyInterface {
id: String!,
"I am an ID that will have my example value specified by the `@spectaql` directive"
id: String! @spectaql(options: [{key: "example", value: "\"idFromDirective\"" }]),
url: URL,
json: JSON,
jso: JSONObject,
Expand All @@ -64,6 +65,9 @@ type MyType implements MyInterface {
requiredArrayField: [String]!

doubleRequiredArrayField: [String!]!

"This field will not show up thanks to the @spectaql directive options"
fieldThatWillNotShowUp: String! @spectaql(options: [{key: "undocumented", value: "true" }])
}

"This is a type that should probably be undocumented. Metadata will indicate this should be hidden"
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const {
loadData,
buildSchemas,
augmentData,
generateSpectaqlSdl,
generateDirectiveSdl,
generateOptionsSdl,
} = require('./dist')

module.exports = run
Expand All @@ -15,3 +18,6 @@ module.exports.resolveOptions = resolveOptions
module.exports.loadData = loadData
module.exports.buildSchemas = buildSchemas
module.exports.augmentData = augmentData
module.exports.generateSpectaqlSdl = generateSpectaqlSdl
module.exports.generateDirectiveSdl = generateDirectiveSdl
module.exports.generateOptionsSdl = generateOptionsSdl
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"@anvilco/grunt-embed": "^0.0.1",
"@graphql-tools/load-files": "^6.3.2",
"@graphql-tools/merge": "^8.1.2",
"@graphql-tools/schema": "^8.5.0",
"@graphql-tools/utils": "^8.8.0",
"cheerio": "^1.0.0-rc.10",
"coffeescript": "^2.6.1",
"commander": "^9.1.0",
Expand All @@ -90,7 +92,7 @@
"json5": "^2.2.0",
"lodash": "^4.17.12",
"marked": "^4.0.12",
"microfiber": "^1.1.0",
"microfiber": "^1.3.0",
"sass": "^1.32.13",
"sync-request": "^6.1.0",
"tmp": "0.2.1"
Expand Down
19 changes: 19 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import {
pathToRoot,
} from './spectaql/utils'

export {
generateSpectaqlSdl,
generateDirectiveSdl,
generateOptionsSdl,
} from './spectaql/directive'

export { default as parseCliOptions } from './cli'

const defaultAppDir = normalizePathFromRoot('dist')
Expand Down Expand Up @@ -64,9 +70,17 @@ const spectaqlOptionDefaults = Object.freeze({
displayAllServers: false,
})

const spectaqlDirectiveDefault = Object.freeze({
enable: true,
directiveName: 'spectaql',
optionsTypeName: 'SpectaQLOption',
})

const introspectionOptionDefaults = Object.freeze({
dynamicExamplesProcessingModule: false,

spectaqlDirective: Object.assign({}, spectaqlDirectiveDefault),

removeTrailingPeriodFromDescriptions: false,

fieldExpansionDepth: 1,
Expand Down Expand Up @@ -213,6 +227,11 @@ export function resolveOptions(cliOptions) {
introspectionOptionDefaults
)

opts.specData.introspection.spectaqlDirective = _.defaults(
opts.specData.introspection.spectaqlDirective,
spectaqlDirectiveDefault
)

opts.specData.extensions = _.defaults(
{},
opts.specData.extensions,
Expand Down
22 changes: 19 additions & 3 deletions src/spectaql/build-schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
graphQLSchemaFromIntrospectionResponse,
} from './graphql-loaders'

import { addMetadataFromFile } from './metadata-loaders'
import {
addMetadataFromFile,
addMetadataFromDirectables,
} from './metadata-loaders'

import {
augmentData,
Expand All @@ -24,6 +27,7 @@ export function buildSchemas(opts) {
const {
introspection: introspectionOptions,
introspection: {
spectaqlDirective: spectaqlDirectiveOptions = {},
url: introspectionUrl,
schemaFile,
introspectionFile,
Expand All @@ -35,10 +39,22 @@ export function buildSchemas(opts) {

let done = false
let introspectionResponse

if (schemaFile) {
const schema = loadSchemaFromSDLFile({ pathToFile: schemaFile })
const { schema, directables, directiveName } = loadSchemaFromSDLFile({
pathToFile: schemaFile,
spectaqlDirectiveOptions,
})
introspectionResponse = introspectionResponseFromSchema({ schema })

if (spectaqlDirectiveOptions.enable) {
introspectionResponse = addMetadataFromDirectables({
...introspectionOptions,
directables,
directiveName,
introspectionQueryResponse: introspectionResponse,
})
}

done = 'loaded GraphQL SDL from file'
}

Expand Down
Loading