graphql-markdown
Advanced tools
Comparing version 3.0.0-0 to 3.1.0-0
{ | ||
"name": "graphql-markdown", | ||
"version": "3.0.0-0", | ||
"version": "3.1.0-0", | ||
"description": "Generate documentation for your GraphQL schema in Markdown", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -83,11 +83,17 @@ [![npm version](https://img.shields.io/npm/v/graphql-markdown.svg)](https://www.npmjs.com/package/graphql-markdown) | ||
Given a schema JSON object (the output of the introspection query, an object | ||
with a `__schema` property), render the schema to a string. | ||
with a `__schema` property), render the schema to the console or the provided | ||
`printer` function. | ||
| Option | Description | | ||
| ---------- | ----------- | | ||
| `title` | The title of the document, defaults to “Schema Types”. | | ||
| `prologue` | Markdown content to include after the title. | | ||
| `epilogue` | Markdown content to include at the end of the document. | | ||
| `printer` | A function to handle each line of output, defaults to `console.log`. | | ||
##### Options | ||
* **`title`**: The title of the document, defaults to “Schema Types”. | ||
* **`prologue`**: Markdown content to include after the title. | ||
* **`epilogue`**: Markdown content to include at the end of the document. | ||
* **`printer`**: A function to handle each line of output, defaults to `console.log`. | ||
* **`unknownTypeURL`**: A string or function to determine the URL for linking to | ||
types that aren’t found in the schema being rendered. This may be the case if | ||
you’re rendering the result of `diffSchema()`, for example. String values will | ||
have `#${type.name.toLowerCase()}` appended, and function values will be | ||
called with the type object for full control. | ||
#### diffSchema(oldSchema: object, newSchema: object, options: object) | ||
@@ -100,6 +106,7 @@ | ||
| Option | Description | | ||
| ----------------- | ----------- | | ||
| `processTypeDiff` | A function to add or modify fields on each type that will be output. | | ||
##### Options | ||
* **`processTypeDiff`**: A function to add or modify fields on each type that | ||
will be output. | ||
## Output | ||
@@ -106,0 +113,0 @@ |
@@ -13,10 +13,11 @@ 'use strict' | ||
function renderType (type) { | ||
function renderType (type, options) { | ||
if (type.kind === 'NON_NULL') { | ||
return renderType(type.ofType) + '!' | ||
return renderType(type.ofType, options) + '!' | ||
} | ||
if (type.kind === 'LIST') { | ||
return `[${renderType(type.ofType)}]` | ||
return `[${renderType(type.ofType, options)}]` | ||
} | ||
return `<a href="#${type.name.toLowerCase()}">${type.name}</a>` | ||
const url = options.getTypeURL(type) | ||
return url ? `<a href="${url}">${type.name}</a>` : type.name | ||
} | ||
@@ -28,2 +29,3 @@ | ||
const printer = options.printer || console.log | ||
const getTypeURL = options.getTypeURL | ||
@@ -53,3 +55,3 @@ if (!skipTitle) { | ||
) | ||
printer(`<td valign="top">${renderType(field.type)}</td>`) | ||
printer(`<td valign="top">${renderType(field.type, { getTypeURL })}</td>`) | ||
if (field.description || field.isDeprecated) { | ||
@@ -77,3 +79,3 @@ printer('<td>') | ||
printer(`<td colspan="2" align="right" valign="top">${arg.name}</td>`) | ||
printer(`<td valign="top">${renderType(arg.type)}</td>`) | ||
printer(`<td valign="top">${renderType(arg.type, { getTypeURL })}</td>`) | ||
if (arg.description) { | ||
@@ -100,2 +102,3 @@ printer('<td>') | ||
const printer = options.printer || console.log | ||
const unknownTypeURL = options.unknownTypeURL | ||
@@ -107,2 +110,15 @@ if (schema.__schema) { | ||
const types = schema.types.filter(type => !type.name.startsWith('__')) | ||
const typeMap = schema.types.reduce((typeMap, type) => { | ||
return Object.assign(typeMap, { [type.name]: type }) | ||
}, {}) | ||
const getTypeURL = type => { | ||
const url = `#${type.name.toLowerCase()}` | ||
if (typeMap[type.name]) { | ||
return url | ||
} else if (typeof unknownTypeURL === 'function') { | ||
return unknownTypeURL(type) | ||
} else if (unknownTypeURL) { | ||
return unknownTypeURL + url | ||
} | ||
} | ||
@@ -163,3 +179,3 @@ const queryType = schema.queryType | ||
) | ||
renderObject(query, { skipTitle: true, printer }) | ||
renderObject(query, { skipTitle: true, printer, getTypeURL }) | ||
} | ||
@@ -169,3 +185,3 @@ | ||
printer('\n## Objects') | ||
objects.forEach(type => renderObject(type, { printer })) | ||
objects.forEach(type => renderObject(type, { printer, getTypeURL })) | ||
} | ||
@@ -229,3 +245,3 @@ | ||
printer('\n## Interfaces\n') | ||
interfaces.forEach(type => renderObject(type, { printer })) | ||
interfaces.forEach(type => renderObject(type, { printer, getTypeURL })) | ||
} | ||
@@ -232,0 +248,0 @@ |
164187
468
119