Skip to content

Instantly share code, notes, and snippets.

@crucialfelix
Last active October 24, 2016 02:35
Show Gist options
  • Save crucialfelix/2b94528bf0817dfefe1c3258977fa478 to your computer and use it in GitHub Desktop.
Save crucialfelix/2b94528bf0817dfefe1c3258977fa478 to your computer and use it in GitHub Desktop.
Make relay-fullstack connect with graphene-django

Make relay-fullstack work with the graphene-django graphql server

React, Relay, webpack. Includes express-graphql server which you will need to turn off in favor of the django one.

Solid ORM migrations, easy configuration of a full graphql server using your django models.

django

Set a path where the schema should be dumped to in your settings:

# settings.py
GRAPHENE = {
    'SCHEMA': 'mattermind.schema.schema',
    'SCHEMA_OUTPUT': 'frontend/schema.json'
}

Dump the schema from django anytime you make changes:

./manage.py graphql_schema

Make the graphql view CSRF exempt:

# urls.py
from django.conf.urls import url
from django.contrib import admin
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True)))
]

relay-fullstack

Edit server/index.js and comment out or delete the graphql server.

  // Launch GraphQL
  // const graphql = express();
  // graphql.use('/', graphQLHTTP({
  //   graphiql: true,
  //   pretty: true,
  //   schema
  // }));
  // graphql.listen(config.graphql.port, () => console.log(chalk.green(`GraphQL is listening on port ${config.graphql.port}`)));

// but keep the proxy through to 8000 (which will now be served by django)
  // Launch Relay by using webpack.config.js
  const relayServer = new WebpackDevServer(webpack(webpackConfig), {
    contentBase: '/build/',
    proxy: {
      '/graphql': `http://localhost:${config.graphql.port}`
    },
    stats: {
      colors: true
    },
    hot: true,
    historyApiFallback: true
  });

Change the babelRelayPlugin.js so that it loads the dumped schema.json rather than server/data/schema.json

var jsonFile = path.join(__dirname, '../../schema.json');

(I keep mine at the top level of my frontend app)

This means that your graphql fragments will be validated and will throw compile errors with helpful messages.

Change package.json start command as you don't need or want to dump the schema

    "start": "./node_modules/.bin/babel-node server/index.js",

Now you can run django and then express:

./manage.py runserver
npm run start

If you are starting with the generated app you will see lots of schema validation errors until you rip all that out and build your own app against your django schema.

You can remove server/ entirely but move the babelRelayPlugin.js to somewhere, you'll want that still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment