Coder Social home page Coder Social logo

Comments (3)

helfer avatar helfer commented on May 14, 2024 1

Thanks @100ideas !

Hm, I think that's going to start causing problems for us somewhat. We can update the devtools, but we'll have to know and check the version of Apollo Client to know which option to use.

Actually... we can probably just pass both options, because currently Apollo Client just ignores options that it doesn't know about 🤔

@100ideas would you be willing to make a PR to make this change?

from apollo-client-devtools.

100ideas avatar 100ideas commented on May 14, 2024

Tried hacking the Explorer component in Explorer.js to set fetchPolicy instead of noFetch, but I don't think this is working (still makes network requests if the query results haven't been cached?)

export default class Explorer extends Component {
  constructor(props, context) {
    super(props, context);

    this.state = {
      noFetch: true,
      fetchPolicy: 'cache-only',
      query: this.props.query,
      variables: this.props.variables,
    };

    try {
      evalInPage(
        `
        window.__APOLLO_CLIENT__.makeGraphiqlQuery = (payload) => {
          return window.__APOLLO_CLIENT__.query({
            query: payload.query,
            variables: payload.variables,
            fetchPolicy: payload.fetchPolicy,
          }).catch(e => ({
            errors: e.graphQLErrors,
          }));
        };
        `, (result, isException) => {}
      );
      console.log(`Explorer.js constructor eval'd makeGraphiqlQuery w/ fetchPolicy: '${this.state.fetchPolicy}'`)
    } catch(e) {
      console.warn(e);
    }

    this.graphQLFetcher = (graphQLParams) => {
      const { fetchPolicy } = this.state;
      console.log("Explorer.js:graphQLFetcher fetchPolicy: " + fetchPolicy)

      return createPromise(
        "window.__APOLLO_CLIENT__.makeGraphiqlQuery(" + JSON.stringify({
          operationName: graphQLParams.operationName,
          query: parse(graphQLParams.query),
          variables: graphQLParams.variables,
          fetchPolicy: `'${fetchPolicy}'`
        }) + ")"
      );
    };
  }

  componentDidMount() {
    if (ga) ga('send', 'pageview', 'GraphiQL');
    if (this.props.query) {
      this.graphiql.handleRunQuery();
    }
  }

  clearDefaultQueryState() {
    this.setState({ query: undefined, variables: undefined });
  }

  render() {
    const { noFetch } = this.state;
    const graphiql = (
      <GraphiQL
        fetcher={this.graphQLFetcher}
        query={this.state.query}
        onEditQuery={() => { this.clearDefaultQueryState() }}
        onEditVariables={() => { this.clearDefaultQueryState() }}
        variables={this.state.variables}
        ref={r => {this.graphiql = r}}
      >
        <GraphiQL.Toolbar>
          <label>
            <input
              type="checkbox"
              checked={noFetch}
              onChange={() => {
                let fetchPol = noFetch ? 'network-only' : 'cache-only';
                this.setState({ noFetch: !noFetch, fetchPolicy: fetchPol, query: undefined, variables: undefined });
              }}
            />
            Load from cache
          </label>
        </GraphiQL.Toolbar>
      </GraphiQL>
    );

    return <div className="body">{graphiql}</div>;
  }
}

from apollo-client-devtools.

100ideas avatar 100ideas commented on May 14, 2024

It's unlikely I'll spend time on this in the near future, but I will remember this thread if I do.

from apollo-client-devtools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.