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 Jul 25, 2019
1 parent 0f482d0 commit cab98ae
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 @@ -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<string>,
Expand Down Expand Up @@ -1425,26 +1428,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 cab98ae

Please sign in to comment.