Add support for default values in graphql fix #2000 (#2001)

This commit is contained in:
eMerzh 2020-03-18 19:58:37 +01:00 committed by GitHub
parent 2f8b0b6602
commit 51610ac6bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 9 deletions

View File

@ -3,12 +3,19 @@ import * as React from 'react';
import type { GraphQLField, GraphQLType } from 'graphql';
import GraphQLExplorerTypeLink from './graph-ql-explorer-type-link';
import MarkdownPreview from '../markdown-preview';
import { astFromValue, print } from 'graphql';
type Props = {
onNavigateType: (type: GraphQLType) => void,
field: GraphQLField<any, any>,
};
const printDefault = ast => {
if (!ast) {
return '';
}
return print(ast);
};
class GraphQLExplorerField extends React.PureComponent<Props> {
renderDescription() {
const { field } = this.props;
@ -36,13 +43,20 @@ class GraphQLExplorerField extends React.PureComponent<Props> {
<React.Fragment>
<h2 className="graphql-explorer__subheading">Arguments</h2>
<ul className="graphql-explorer__defs">
{field.args.map(a => (
<li key={a.name}>
<span className="info">{a.name}</span>:{' '}
<GraphQLExplorerTypeLink onNavigate={onNavigateType} type={a.type} />
{a.description && <MarkdownPreview markdown={a.description} />}
</li>
))}
{field.args.map(a => {
let defaultValue = '';
if ('defaultValue' in a && a.defaultValue !== undefined) {
defaultValue = <span> = {printDefault(astFromValue(a.defaultValue, a.type))}</span>;
}
return (
<li key={a.name}>
<span className="info">{a.name}</span>:{' '}
<GraphQLExplorerTypeLink onNavigate={onNavigateType} type={a.type} />
{defaultValue}
{a.description && <MarkdownPreview markdown={a.description} />}
</li>
);
})}
</ul>
</React.Fragment>
);

View File

@ -4,7 +4,13 @@ import GraphQLExplorerTypeLink from './graph-ql-explorer-type-link';
import autobind from 'autobind-decorator';
import MarkdownPreview from '../markdown-preview';
import GraphQLExplorerFieldLink from './graph-ql-explorer-field-link';
import { GraphQLUnionType, GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
import {
GraphQLUnionType,
GraphQLInterfaceType,
GraphQLObjectType,
astFromValue,
print,
} from 'graphql';
import type { GraphQLType, GraphQLField, GraphQLSchema } from 'graphql';
type Props = {
@ -14,6 +20,12 @@ type Props = {
schema: GraphQLSchema | null,
};
const printDefault = ast => {
if (!ast) {
return '';
}
return print(ast);
};
@autobind
class GraphQLExplorerType extends React.PureComponent<Props> {
_handleNavigateType(type: Object) {
@ -109,11 +121,17 @@ class GraphQLExplorerType extends React.PureComponent<Props> {
<GraphQLExplorerTypeLink onNavigate={this._handleNavigateType} type={field.type} />
);
let defaultValue = '';
if ('defaultValue' in field && field.defaultValue !== undefined) {
defaultValue = (
<span> = {printDefault(astFromValue(field.defaultValue, field.type))}</span>
);
}
const description = field.description;
return (
<li key={key}>
{fieldLink}
{argLinks}: {typeLink}
{argLinks}: {typeLink} {defaultValue}
{description && (
<div className="graphql-explorer__defs__description">
<MarkdownPreview markdown={description} />