Skip to content

Commit cab98ae

Browse files
committed
Add a fetcher prop
1 parent 0f482d0 commit cab98ae

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

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';
@@ -67,6 +69,7 @@ type MakeDefaultArg = (
6769
type Props = {
6870
query: string,
6971
width?: number,
72+
fetcher?: any => Promise<{data: any}>,
7073
schema?: ?GraphQLSchema,
7174
onEdit: string => void,
7275
getDefaultFieldNames?: ?(type: GraphQLObjectType) => Array<string>,
@@ -1425,26 +1428,47 @@ class RootView extends React.PureComponent<RootViewProps, {}> {
14251428
}
14261429
}
14271430

1428-
class Explorer extends React.PureComponent<Props, State> {
1431+
class Explorer extends React.PureComponent<Props, {schema: ?GraphQLSchema}> {
14291432
static defaultProps = {
14301433
getDefaultFieldNames: defaultGetDefaultFieldNames,
14311434
getDefaultScalarArgValue: defaultGetDefaultScalarArgValue,
14321435
};
14331436

1437+
state = {schema: this.props.schema};
1438+
14341439
_ref: ?any;
14351440
_resetScroll = () => {
14361441
const container = this._ref;
14371442
if (container) {
14381443
container.scrollLeft = 0;
14391444
}
14401445
};
1446+
_fetchSchema = () => {
1447+
const {fetcher} = this.props;
1448+
1449+
if (fetcher) {
1450+
fetcher({
1451+
query: getIntrospectionQuery()
1452+
}).then(result => {
1453+
if (this.state.schema !== undefined) {
1454+
return;
1455+
}
1456+
1457+
this.setState({ schema: buildClientSchema(result.data) });
1458+
});
1459+
}
1460+
}
14411461
componentDidMount() {
1462+
if (this.state.schema === undefined) {
1463+
this._fetchSchema();
1464+
}
14421465
this._resetScroll();
14431466
}
14441467
_onEdit = (query: string): void => this.props.onEdit(query);
14451468

14461469
render() {
1447-
const {schema, query, makeDefaultArg} = this.props;
1470+
const {query, makeDefaultArg} = this.props;
1471+
const {schema} = this.state;
14481472

14491473
if (!schema) {
14501474
return (

0 commit comments

Comments
 (0)