From cab98ae75ac5f2af8251d64c524e8aa23dfbd842 Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Thu, 25 Jul 2019 12:24:26 +0200 Subject: [PATCH] Add a fetcher prop --- src/Explorer.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Explorer.js b/src/Explorer.js index 4f5f039..a2ffd4d 100644 --- a/src/Explorer.js +++ b/src/Explorer.js @@ -24,6 +24,8 @@ import { isScalarType, isUnionType, isWrappingType, + buildClientSchema, + getIntrospectionQuery, parse, print, } from 'graphql'; @@ -67,6 +69,7 @@ type MakeDefaultArg = ( type Props = { query: string, width?: number, + fetcher?: any => Promise<{data: any}>, schema?: ?GraphQLSchema, onEdit: string => void, getDefaultFieldNames?: ?(type: GraphQLObjectType) => Array, @@ -1425,12 +1428,14 @@ class RootView extends React.PureComponent { } } -class Explorer extends React.PureComponent { +class Explorer extends React.PureComponent { static defaultProps = { getDefaultFieldNames: defaultGetDefaultFieldNames, getDefaultScalarArgValue: defaultGetDefaultScalarArgValue, }; + state = {schema: this.props.schema}; + _ref: ?any; _resetScroll = () => { const container = this._ref; @@ -1438,13 +1443,32 @@ class Explorer extends React.PureComponent { container.scrollLeft = 0; } }; + _fetchSchema = () => { + const {fetcher} = this.props; + + if (fetcher) { + fetcher({ + query: getIntrospectionQuery() + }).then(result => { + if (this.state.schema !== undefined) { + return; + } + + this.setState({ schema: buildClientSchema(result.data) }); + }); + } + } componentDidMount() { + if (this.state.schema === undefined) { + this._fetchSchema(); + } this._resetScroll(); } _onEdit = (query: string): void => this.props.onEdit(query); render() { - const {schema, query, makeDefaultArg} = this.props; + const {query, makeDefaultArg} = this.props; + const {schema} = this.state; if (!schema) { return (