Skip to content

Commit 58c647b

Browse files
committed
Add a fetcher prop
1 parent cbcd4d1 commit 58c647b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/Explorer.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
isScalarType,
2525
isUnionType,
2626
isWrappingType,
27+
buildClientSchema,
28+
getIntrospectionQuery,
2729
parse,
2830
print,
2931
} from 'graphql';
@@ -68,6 +70,7 @@ type Props = {
6870
query: string,
6971
width?: number,
7072
title?: string,
73+
fetcher?: any => Promise<{data: any}>,
7174
schema?: ?GraphQLSchema,
7275
onEdit: string => void,
7376
getDefaultFieldNames?: ?(type: GraphQLObjectType) => Array<string>,
@@ -1428,26 +1431,47 @@ class RootView extends React.PureComponent<RootViewProps, {}> {
14281431
}
14291432
}
14301433

1431-
class Explorer extends React.PureComponent<Props, State> {
1434+
class Explorer extends React.PureComponent<Props, {schema: ?GraphQLSchema}> {
14321435
static defaultProps = {
14331436
getDefaultFieldNames: defaultGetDefaultFieldNames,
14341437
getDefaultScalarArgValue: defaultGetDefaultScalarArgValue,
14351438
};
14361439

1440+
state = {schema: this.props.schema};
1441+
14371442
_ref: ?any;
14381443
_resetScroll = () => {
14391444
const container = this._ref;
14401445
if (container) {
14411446
container.scrollLeft = 0;
14421447
}
14431448
};
1449+
_fetchSchema = () => {
1450+
const {fetcher} = this.props;
1451+
1452+
if (fetcher) {
1453+
fetcher({
1454+
query: getIntrospectionQuery()
1455+
}).then(result => {
1456+
if (this.state.schema !== undefined) {
1457+
return;
1458+
}
1459+
1460+
this.setState({ schema: buildClientSchema(result.data) });
1461+
});
1462+
}
1463+
}
14441464
componentDidMount() {
1465+
if (this.state.schema === undefined) {
1466+
this._fetchSchema();
1467+
}
14451468
this._resetScroll();
14461469
}
14471470
_onEdit = (query: string): void => this.props.onEdit(query);
14481471

14491472
render() {
1450-
const {schema, query, makeDefaultArg} = this.props;
1473+
const {query, makeDefaultArg} = this.props;
1474+
const {schema} = this.state;
14511475

14521476
if (!schema) {
14531477
return (

0 commit comments

Comments
 (0)