RisingStack / graffiti

⚠️ DEVELOPMENT DISCONTINUED - Node.js GraphQL ORM
https://risingstack-graffiti.signup.team/
MIT License
1.01k stars 50 forks source link

regeneratorRuntime is not defined #13

Closed jontonsoup closed 8 years ago

jontonsoup commented 8 years ago

I'm using babel 6 with the normal 2015 presets.

When I try to make any queries on my schema,

{
  user(id: "563d7ad3a802cba51904958a"){
    email
  }
}

I get this result:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "regeneratorRuntime is not defined"
}

I fixed this using by adding the babel polyfill to my code.

http://babeljs.io/docs/usage/polyfill/

This seems like it should be a dependency of graffiti-- not my application?

Thanks! Jon

tothandras commented 8 years ago

I am pretty sure it's not a problem with graffiti. It's published as transpiled ES5.

jontonsoup commented 8 years ago

@tothandras I think theres a runtime dependency if you use any generators

You need to include the regenerator runtime or the 6to5 polyfill. It's not possible to implement generators without a runtime as the code to manage execution flow is quite lengthy.

From: https://github.com/babel/babel/issues/303

It could be something graffiti depends on?

tothandras commented 8 years ago

koa, maybe?

jontonsoup commented 8 years ago

I'm not running any of the koa code, so I'm not sure.

tothandras commented 8 years ago

co-body? I am really not sure.

jontonsoup commented 8 years ago

Yea, it could be. I just wanted to log the error in case anyone else ran into it.

tothandras commented 8 years ago

:+1:

ghost commented 8 years ago

Could be koa, but I've got a feeling I've seen this with one of the facebook modules before. Could you get a stack trace?

tothandras commented 8 years ago

@burkhardr I've checked it since, koa is only a dev dependency.

jontonsoup commented 8 years ago

Its funny -- it bubbles up through the response so its not a transpile - time error -- its caught somewhere during runtime.

ghost commented 8 years ago

Yeah, I think one of the fb modules is using a generator (don't ask me why). Could you put

var gm = require('@risingstack/graffiti-mongoose');

var schema = gm.getSchema([/* your models */]);

into a file and run it?

jontonsoup commented 8 years ago

@burkhardr I added those lines to a new file and I did not get an error. I was getting this error at runtime during the graphql query.

ghost commented 8 years ago

Try to run that query in your test file:

graphql(schema, query).then(function (result) {
  console.log(result);
});
jontonsoup commented 8 years ago
var gm =  require('@risingstack/graffiti-mongoose')
var user = require('./build/models/user');
var schema = gm.getSchema([user]);
var graphql = require('graphql');

var query = "{users{email}}";

graphql.graphql(schema, query)
  .then(result => { console.log(result); })
  .catch((result) => { console.log(result);});

Strangely not getting any output now on a valid query. On an invalid query, it throws an error. Might be something is wrong with my testfile, but its hard to tell.

As an aside, I'm using babel 6 with the 2015 package but running my code with normal node. I've been having sporadic issues between packages that depend on babel 5 and how they interact with my code-- not sure if this is related or not really, though.

ghost commented 8 years ago

Make sure you connect to mongodb!

jontonsoup commented 8 years ago

@burkhardr whoops good catch!

jontonsoup commented 8 years ago

Here's the error I was expecting, now that mongo is connected.

{ data: { users: [ [Object] ] },
  errors: [ { [Error: regeneratorRuntime is not defined] message: 'regeneratorRuntime is not defined' } ] }
ghost commented 8 years ago

Still pretty sure this is coming from one of the fb modules. Have you tried adding import babelPolyfill from 'babel-core/polyfill'; to the top of your entry script?

My .babelrc:

{
  "stage": 0,
  "optional": [
      "es7.objectRestSpread",
      "es7.decorators",
      "es7.classProperties",
      "es7.asyncFunctions",
      "runtime",
  ],
}
jontonsoup commented 8 years ago

haha -- I thought you were trying to debug the problem on your end with graffiti. Yes, that actually is the solution that fixed my problem. I created an issue because I didn't need the polyfil before I started using griffiti, even though I was using graphql before that with no problem. It seemed strange to me that installing a library would require me to use the polyfil / break other libraries.

ghost commented 8 years ago

I retract everything I might've said about that mysterious fb module and claim the opposite :grin:

Looks like it's coming from babel generated code that transpiles async await and expects regeneratorRuntime to be in the global namespace. Looks like there's no way around the polyfill. https://babeljs.io/repl/#?experimental=false&evaluate=true&loose=false&spec=false&code=async%20function%20blah()%20%7B%0A%20%20await%20blub()%3B%0A%7D%0A

@tothandras Something for the docs perhaps?

jontonsoup commented 8 years ago

hahaha no worries :)

cilindrox commented 8 years ago

Running on this very same issue, sample code here: https://gist.github.com/cilindrox/dae6fdc21da902613aa9

tothandras commented 8 years ago

@cilindrox babel-polyfill is required at this point. I am still migrating to babel 6 (having problem with some mocks in tests), I don't know if it has the same requirements.

cilindrox commented 8 years ago

Gotcha. That seemed to do the trick. Thanks!

tothandras commented 8 years ago

babel-polyfill has been added to the peer dependencies of graffiti-mongoose in version 5.x.x.

LucianoGanga commented 8 years ago

Hi! I'm getting the same error, but I'm not using babel.

The thing is that I'm using Node 4, so this functions should be already integrated.

The only workaround that I found for this was adding "regenerator" like this

global.regeneratorRuntime = require('regenerator/runtime');

Do you thing that there's a better way to fix this?

Thanks!

tothandras commented 8 years ago

@LucianoGanga Hi! babel-polyfill has been added to the peer dependencies of graffiti-mongoose, so it's not a fix, but necessary to require the babel-polyfill at the top of the entry point in your application.

LucianoGanga commented 8 years ago

Thanks @tothandras ! Just calling require('babel-polyfill'); did the work, as you said.

Have a nice day! :D

johackim commented 8 years ago

I have add import 'babel-polyfill';