Giter Site home page Giter Site logo

Comments (9)

philiptzou avatar philiptzou commented on May 2, 2024 10

I encountered the same problem recently. After dived into the source code of CodeMirror and fixed my issue I think it is better to share my experience here. In case someone else get stuck by this really weird, tricky issue.

TL;DR: Find a way to ensure graphiql.css is loaded before <GraphiQL /> get rendered. Be very careful when playing with Webpack code-splitting and GraphiQL at the same time.

My problem is highly related with the differences of the style processing between dev and prod of a customized version of start-react-app. I also used "code splitting" technique to split the GraphiQL and CodeMirror from the main bundle.

In general, in dev mode all styles are loaded in inline <style> block by style-loader and there's no problem. However, in prod mode, instead of being added to main.css, the graphiql.css was added to the chunk file which contained CodeMirror and GraphiQL.

It took me a lot of hours trying to upgrade my old react-relay to the newest version (since apparently the graphiql package required graphql@^0.10.5 via codemirror-graphql via graphql-language-service-interface) therefore I can upgrade graphiql to the newest one. However after the upgrading I found the problem existing with the newest version. Then I started to set breakpoints and debug CodeMirror. Then I found without the proper style, .CodeMirror-gutter.CodeMirror-foldgutter became "display: block" instead of "display: inline-block". The width of that element cascaded to all its parent elements and caused the disaster. Even graphiql.css was loaded immediately after the initialization, the mess didn't go away.

Once found the problem, I changed my code to ensure that graphiql.css must be loaded before <GraphiQL /> get rendered. The problem was gone.

from graphiql.

leebyron avatar leebyron commented on May 2, 2024

Whoa this is crazy, what browser/OS are you using? Can you also verify that you have the graphiql.css file loaded into the browser?

from graphiql.

gnoff avatar gnoff commented on May 2, 2024

nothing crazy. chrome on osx.

I guess I took the readme example a little to literally and used just

import React from 'react';
import GraphiQL from 'graphiql';
import fetch from 'isomorphic-fetch';

function graphQLFetcher(graphQLParams) {
  return fetch(window.location.origin + '/graphql', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(graphQLParams),
  }).then(response => response.json());
}

React.render(<GraphiQL fetcher={graphQLFetcher} />, document.body);

and an index.html of

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>GraphiQL on localhost</title>
</head>
<body>
  <script src="/static/bundle.js"></script>
</body>
</html>

and served the above with webpack-dev-server

I now see you have examples using static asset css files. I'll introduce that to my index and I'm sure it'll fix things... I'll confirm in a bit

from graphiql.

gnoff avatar gnoff commented on May 2, 2024

I was assuming all inline styles in the <GraphiQL /> component :)

from graphiql.

gnoff avatar gnoff commented on May 2, 2024

Ok just needed to add css-loader to webpack.config and all is good again.

from graphiql.

mikefowler avatar mikefowler commented on May 2, 2024

@philiptzou do you have recommendations as to how you ensure the stylesheet is loaded first? Or could you share how you went about this?

from graphiql.

philiptzou avatar philiptzou commented on May 2, 2024

@mikefowler

I used setTimeout() to delay the rendering of <GraphiQL /> element. I know it's not 100% guaranteed but it is enough for my case. If you need to ensure the loading sequence, I think you can use setInterval() to check if the codemirror.css was loaded, then setState() to render <GraphiQL /> element and clearInterval().

from graphiql.

hotgazpacho avatar hotgazpacho commented on May 2, 2024

@philiptzou any guidance on what value for setInterval() worked for you?

from graphiql.

philiptzou avatar philiptzou commented on May 2, 2024

@hotgazpacho I think any value larger than 0 for setInterval() is acceptable as long as the delay isn't perceptible to the user. How about 100?

from graphiql.

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.