Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async / await ... how to build with babel #5

Closed
tomitrescak opened this issue May 17, 2016 · 17 comments
Closed

async / await ... how to build with babel #5

tomitrescak opened this issue May 17, 2016 · 17 comments

Comments

@tomitrescak
Copy link

Hi, I'm trying to build an NPM package that would provide some helper functions for meteor users. The issue is thta no matter how I build it from ES6/7 code, the async await functions always complain about fibers and they do not work.

What is the babel config that would work?
Here is my .bablerc

{
  "presets": ["es2015", "stage-0"],
  "plugins": [
    ["transform-runtime", {
      "polyfill": false,
      "regenerator": true
    }]
  ]
}
@stubailo
Copy link
Contributor

Where are you putting the async await functions? The npm package is pre-built before publishing to NPM, so it's not really a problem with this package.

BTW @lorensr was thinking of working on a Meteor integration package as well!

@tomitrescak
Copy link
Author

For example in following code it says: "Can't wait without fiber" on line with Meteor.user retrieval

graphQLServer.use('/graphql', apolloServer(async(req) => {
    let user = null; 
    if (req.headers.authorization) {
      const token = req.headers.authorization;
      check(token, String);
      const hashedToken = Accounts._hashLoginToken(token);
      user = await Meteor.users.findOne({
        "services.resume.loginTokens.hashedToken": hashedToken
      });
    }

    return {
      graphiql: true,
      pretty: true,
      schema,
      resolvers,
      context: {
        // The current user will now be available on context.user in all resolvers
        user,
      }
    };
  }));

@stubailo
Copy link
Contributor

It works fine here: https://github.com/apollostack/meteor-starter-kit/blob/master/server/main.js

I guess you put this code into an NPM package, and it doesn't work anymore?

The async fiber support relies on Meteor's Promise polyfill, so you should make sure you aren't including any other promise polyfills in your NPM package.

@tomitrescak
Copy link
Author

The previous code works flawlessly in Meteor, but when built as Npm package it fails.

@tomitrescak
Copy link
Author

Ha! The Meteor polyfill will be the problem ... would you be able to point me out how can I use it with babel?

@stubailo
Copy link
Contributor

You could just not use async/await in your package btw - that would be the easiest choice. Also, I'd suggest using an Atmosphere package instead of NPM for the short term, until Meteor 1.5 makes it possible for NPM packages to take advantage of more Meteor features.

@tomitrescak
Copy link
Author

I'll try with atmosphere package. Good idea.

I cannot work without async/await as all update / insert queries fail complaining that they do not run in Meteor Fiber.

@lorensr
Copy link
Contributor

lorensr commented May 17, 2016

@tomitrescak Hi Tomas, here's the shell I made for the atmosphere package:

https://github.com/apollostack/meteor-integration

I'd be interested to hear your thoughts on the API:

apollographql/meteor-integration#2

And would be happy to work on PRs with you if you'd like to combine efforts 😄

@tomitrescak
Copy link
Author

@lorensr looks great! I'll give it a go. My effort is not going very well. Just created the meteor package but am still getting "Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment." error.

Wht I miss in the package is the possibility to incrementally add resolvers. In a complex application with 50+ queries it would be great to have a possibility to split queries and mutations by modules. That is one of the things I tried to implement, but am hitting the wall with async.

I tried to look at how you've done it, but looks like you currently have only a shell, no implementation.

So, when you start to implement "useMeteorAccounts" you probably will run into very same issue. There the error is "Cannot await when not in Fiber".

@tomitrescak
Copy link
Author

Also, would be great if you would hide all NPM packages into the Meteor package so no Npm install would be necessary.

My repo is here:

https://github.com/tomitrescak/apollo-redux-tools.git

@stubailo
Copy link
Contributor

stubailo commented May 17, 2016

Also, would be great if you would hide all NPM packages into the Meteor package so no Npm install would be necessary.

Definitely DO NOT do this. The whole idea is that you can update the NPM package as necessary without waiting for the Meteor package maintainer to get around to updating their wrapper.

@stubailo
Copy link
Contributor

Wht I miss in the package is the possibility to incrementally add resolvers.

You can use _.merge to combine as many resolver objects as you want.

@tomitrescak
Copy link
Author

@stubailo ... just bumped into Meteor recommendation to NOT do that ;)

@stubailo
Copy link
Contributor

stubailo commented May 17, 2016

What do you mean?

@tomitrescak
Copy link
Author

I confirmed what you just said. I was checking the Meteor recommendations for package authors.

@tomitrescak
Copy link
Author

tomitrescak commented May 17, 2016

The moment I moved all npm dependencies form package to root level the async problem disappeared, yet another one appeared as "context" is always null in my queries.

This is how I init the server:

const res = {
      graphiql: true,
      pretty: true,
      schema,
      resolvers,
      context: {
        // The current user will now be available on context.user in all resolvers
        user,
      }
    };

I'm sure that user is filled in. Then in my mutation:

addComment(root: any, { postId, comment }: any, context: any) {
    console.log("adding: " + context);
    const id = Comments.insert({ postId: postId, text: comment, createdAt: new Date().getTime(), author: context.user._id });
    return Posts.findOne(id);
  }

The context is null. Do you have any idea why this could happen?

[EDIT] Nevermind ... all solved.

@tomitrescak
Copy link
Author

@lorensr I like the API, most of it is there. I'll try to do the PR tomorrow with what I've done. You'll see how much of it is trash.

evans added a commit that referenced this issue Jul 3, 2018
* Initial commit

* 0.0.3

* Replace endOffset with duration in resolver calls

* 0.0.4

* Fix duration

* 0.0.5

* Remove unnecessary schema level resolve function and return schema

* Update README

* 0.0.6

* Update README

* Update dependencies

* 0.0.7

* Update README

* set package.json to point to this repository (#3)

* Update dependencies

Fixes #4.

* 0.0.8

* Add asynciterable support to tsconfig.json

* Skip trace collection when context or context._traceCollector is undefined

Fixes #5.

* 0.0.9

* Rewrite to use graphql-extensions

* 0.0.10

* 0.0.11

* 0.1.0

* Update graphql-extensions dependency and downgrade TS target

* 0.1.1

* Update README

* Update README

* Increase version range for `graphql` peerDependency. (#7)

* 0.1.2

* Update dependencies

* 0.1.3

* [apollo-bot] Update the Issue/PR Templates with auto label (#9)

* Update `graphql` peer dependency range to allow 0.13.x.

* dev: Update TypeScript to latest version, v2.7.2.

* dev: Update jest & dependencies to latest versions.

* dev: Update type definitions for `graphql`, `node` and `jest`.

* Allow `undefined` to return from `format`. (#12)

* Allow `undefined` to return from `format`.

TypeScript 2.7 introduced new "Strict Class Instantiation" rules which,
as the name suggests, require properties which are intended to be set
(eventually, to a type) be set during construction.

Before this change, the `TracingExtension` class was deferred setting these
private properties (namely, `startWallTime`, `endWallTime`, `startHrTime`
and `duration`), but not during instantiation which required setting
marking them as optional and guarding their usage in other methods which
might use those (temporarily `undefined`, if even for a tick) properties.
For example, the expectation that `format` is _only_ called after
`requestDidStart` is not guaranteed with this configuration, even if it is
expected under normal operation.

Therefore, this change adds the additional guarding and updates the `format`
method to return `undefined` in the event that it doesn't have the appropriate
data.

* Update `graphql-extensions` dependency to `~0.0.9`.

Specifically, to take advantage of a type which landed in
`graphql-extensions@0.0.9` thanks to
https://github.com/apollographql/graphql-extensions/pull/10.

* 0.1.4

* Update for graphql-extensions@0.1.0 API (#13)

* Upgrade to TypeScript 2.8

* Add tslint and prettier

* Update for graphql-extensions@0.1.0 API

* 0.2.0-beta.0

* Make work with newest API usage

format() now gets called before the requestDidStart() EndHandler.

* 0.2.0-beta.1

* remove unused files from tracing package

* upgrade packages, fix compilation bugs, and add test
abernix added a commit that referenced this issue Jan 25, 2019
trevor-scheer added a commit that referenced this issue May 6, 2020
trevor-scheer added a commit that referenced this issue May 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants