Giter VIP home page Giter VIP logo

Comments (7)

tegandbiscuits avatar tegandbiscuits commented on July 18, 2024 10

For what it's worth, it's possible to do this setup but pulling from a CDN.

I have this in graphiql/index.html.erb.

<html>
<head>
  <title>GraphiQL</title>
  <link href="https://unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
</head>
<body style="margin: 0;">
<div id="graphiql" style="height: 100vh;"></div>

<script crossorigin src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script crossorigin src="https://unpkg.com/graphiql/graphiql.min.js"></script>

<script>
  const graphQLFetcher = graphQLParams =>
    fetch('<%= graphql_path %>', {
      method: 'post',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(graphQLParams),
    })
      .then(response => response.json())
      .catch(() => response.text());
  ReactDOM.render(
    React.createElement(GraphiQL, { fetcher: graphQLFetcher }),
    document.getElementById('graphiql'),
  );
</script>
</body>
</html>

from graphiql-rails.

cowholio4 avatar cowholio4 commented on July 18, 2024 3

@tegandbiscuits thanks for the example. i added the subresource integrity fields suggested by @connorshea And added support for CSRF tokens.

I have this in show.html.slim

html
  head
    ==csrf_meta_tags
    link(
        href="https://unpkg.com/[email protected]/graphiql.min.css"
        rel="stylesheet"
        integrity="sha384-wsOzV0Eph4ZUUJS3E5lWAGWDmvPwKGlSAbUqoAxGs/4Xbiu5go013CHeyiHviRjk"
        crossorigin="anonymous"
    )

  body style="margin: 0;"
    #graphiql style="height: 100vh;"

script(
    crossorigin="anonymous"
    src="https://unpkg.com/[email protected]/umd/react.production.min.js"
    integrity="sha384-3fvehj2TEwajh8B8aLpPfx4Ea4bzuasHai/dxAyVlV2uoAcpu9mqz+lVRhBB/YFf"
)
script(
    crossorigin="anonymous"
    src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"
    integrity="sha384-dDaAwdTa5kDnVQlxu7h9UFJ4lR4NIxDNgG+/6iRqTRWhYny4iDtL+t+4qObQVtZ/"
)
script(
    crossorigin="anonymous"
    src="https://unpkg.com/[email protected]/graphiql.min.js"
    integrity="sha384-j7sKHA2Xh03DItLy//iDOH3EXXcIb8TZ4SmCIkPjdVZorF1a40xA9xK90NdDJGNg"
)
javascript:
    const graphQLFetcher = graphQLParams =>
        fetch('/graphql', {
        method: 'post',
        headers: { 'Content-Type': 'application/json', "X-CSRF-Token": "#{form_authenticity_token}"},
        body: JSON.stringify(graphQLParams),
    })
        .then(response => response.json())
        .catch(() => response.text());
    ReactDOM.render(
        React.createElement(GraphiQL, { fetcher: graphQLFetcher }),
        document.getElementById('graphiql'),
    )

from graphiql-rails.

connorshea avatar connorshea commented on July 18, 2024 1

@tegandbiscuits FWIW if you load those files from a CDN I strongly encourage you to use Subresource Integrity to make sure that - if the CDN is breached - you won't load malicious files for your users. https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

from graphiql-rails.

connorshea avatar connorshea commented on July 18, 2024 1

I forgot I made this issue :D It's worth noting that - for whatever reason - GraphiQL's most recent minor release breaks my setup (it just won't load the component so the page is blank). graphiql 1.7.1 works fine but 1.8.x doesn't.

If anyone runs into this problem using my setup, you could try downgrading.

from graphiql-rails.

fintroductions avatar fintroductions commented on July 18, 2024

Awesome stuff. @connorshea, any suggestions on how to accomplish this in Rails-API mode?
Devise auth tokens + GraphiQL don't play too nicely just yet.

from graphiql-rails.

connorshea avatar connorshea commented on July 18, 2024

@fintroductions sorry for the lack of response, I'm not very familiar with API-only Rails apps unfortunately.

from graphiql-rails.

fintroductions avatar fintroductions commented on July 18, 2024

from graphiql-rails.

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.