Skip to content

Commit

Permalink
Add a fetcher prop
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Sep 4, 2019
1 parent cbcd4d1 commit 58c647b
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
isScalarType,
isUnionType,
isWrappingType,
buildClientSchema,
getIntrospectionQuery,
parse,
print,
} from 'graphql';
Expand Down Expand Up @@ -68,6 +70,7 @@ type Props = {
query: string,
width?: number,
title?: string,
fetcher?: any => Promise<{data: any}>,
schema?: ?GraphQLSchema,
onEdit: string => void,
getDefaultFieldNames?: ?(type: GraphQLObjectType) => Array<string>,
Expand Down Expand Up @@ -1428,26 +1431,47 @@ class RootView extends React.PureComponent<RootViewProps, {}> {
}
}

class Explorer extends React.PureComponent<Props, State> {
class Explorer extends React.PureComponent<Props, {schema: ?GraphQLSchema}> {
static defaultProps = {
getDefaultFieldNames: defaultGetDefaultFieldNames,
getDefaultScalarArgValue: defaultGetDefaultScalarArgValue,
};

state = {schema: this.props.schema};

_ref: ?any;
_resetScroll = () => {
const container = this._ref;
if (container) {
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 (
Expand Down

0 comments on commit 58c647b

Please sign in to comment.