From daef7806c836fbfc991fdd31ba7d2f0e39b0f14f Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 18 Mar 2020 12:46:45 -0700 Subject: [PATCH] Move GraphQL default value into separate component --- .../graph-ql-default-value.js | 27 +++++++++++++++++++ .../graph-ql-explorer-field.js | 14 ++-------- .../graph-ql-explorer-type.js | 25 +++-------------- 3 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-default-value.js diff --git a/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-default-value.js b/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-default-value.js new file mode 100644 index 0000000000..dedc0e64ca --- /dev/null +++ b/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-default-value.js @@ -0,0 +1,27 @@ +// @flow +import * as React from 'react'; +import type { GraphQLField } from 'graphql'; +import { astFromValue, print } from 'graphql'; + +type Props = { + field: GraphQLField, +}; + +class GraphQLDefaultValue extends React.PureComponent { + render() { + const { field } = this.props; + + // Make Flow happy :/ + const fieldO: Object = field; + + if ('defaultValue' in fieldO && fieldO.defaultValue !== undefined) { + const ast = astFromValue(fieldO.defaultValue, fieldO.type); + const strDefault = ast ? print(ast) : ''; + return {`= ${strDefault}`}; + } else { + return null; + } + } +} + +export default GraphQLDefaultValue; diff --git a/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-field.js b/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-field.js index 4823e9497a..9f97e94c57 100644 --- a/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-field.js +++ b/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-field.js @@ -3,19 +3,13 @@ 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'; +import GraphQLDefaultValue from './graph-ql-default-value'; type Props = { onNavigateType: (type: GraphQLType) => void, field: GraphQLField, }; -const printDefault = ast => { - if (!ast) { - return ''; - } - return print(ast); -}; class GraphQLExplorerField extends React.PureComponent { renderDescription() { const { field } = this.props; @@ -44,15 +38,11 @@ class GraphQLExplorerField extends React.PureComponent {

Arguments

    {field.args.map(a => { - let defaultValue = ''; - if ('defaultValue' in a && a.defaultValue !== undefined) { - defaultValue = = {printDefault(astFromValue(a.defaultValue, a.type))}; - } return (
  • {a.name}:{' '} - {defaultValue} + {a.description && }
  • ); diff --git a/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-type.js b/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-type.js index 753ef3305e..fdc58346da 100644 --- a/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-type.js +++ b/packages/insomnia-app/app/ui/components/graph-ql-explorer/graph-ql-explorer-type.js @@ -4,14 +4,9 @@ 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, - astFromValue, - print, -} from 'graphql'; -import type { GraphQLType, GraphQLField, GraphQLSchema } from 'graphql'; +import type { GraphQLField, GraphQLSchema, GraphQLType } from 'graphql'; +import { GraphQLInterfaceType, GraphQLObjectType, GraphQLUnionType } from 'graphql'; +import GraphQLDefaultValue from './graph-ql-default-value'; type Props = { onNavigateType: (type: Object) => void, @@ -20,12 +15,6 @@ type Props = { schema: GraphQLSchema | null, }; -const printDefault = ast => { - if (!ast) { - return ''; - } - return print(ast); -}; @autobind class GraphQLExplorerType extends React.PureComponent { _handleNavigateType(type: Object) { @@ -121,17 +110,11 @@ class GraphQLExplorerType extends React.PureComponent { ); - let defaultValue = ''; - if ('defaultValue' in field && field.defaultValue !== undefined) { - defaultValue = ( - = {printDefault(astFromValue(field.defaultValue, field.type))} - ); - } const description = field.description; return (
  • {fieldLink} - {argLinks}: {typeLink} {defaultValue} + {argLinks}: {typeLink} {description && (