question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Broken promise behaviour when types used in list have async field resolve functions.

See original GitHub issue

Okay, I’m still digging into this, and trying to simplify my examples, but it looks like something funky is going on with field resolve behaviour when promises are returned.

Here’s example code which demonstrates the behaviour (as well as the results) https://gist.github.com/latentflip/f1c9520ac0d83b5237fc note how in the results, projectPromises differs from projectResults.

Okay, to explain further, the basic setup I have is:

type Org {
  id: String
  projects: [Project]
}

type Project {
  id: String
  versions: ProjectVersion
}

type ProjectVersion {
  id: String
}

and my root query just returns one org, so the query I’m executing is:

query Foo {
  org {
    id,
    projects {
      versions
    }
  }
}

which should return the org, all it’s projects, and all their versions.

However, it seems if my projects.versions resolve function returns a promise, instead of immediately returning an array, something breaks. To demonstrate, in the actual code linked above, I’ve got a ProjectReturnType which just returns an array for it’s versions and a ProjectPromiseType which returns the same array, but wrapped in a Promise.resolve(). The latter seems to break the projectPromises in the response completely, and it just returns {}.

As I understand it, a resolve function returning a value, or a resolve function returning a promise of that same value, should be exactly equivalent, right?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sarkistltcommented, Oct 3, 2016

Having almost same problem. I try to use type of “Customer” which fetch some data from SQL and some from MongoDb.

export let customerType = new GraphQLObjectType({
    name: 'customerType',
    description: 'Customer schema',
    fields: {
        id: {type: GraphQLInt},
        createdAt: {type: GraphQLString},
        updatedAt: {type: GraphQLString},
        deletedAt: {type: GraphQLString},
        firstName: {type: GraphQLString},
        lastName: {type: GraphQLString},
        email: {type: GraphQLString},
        password: {type: GraphQLString},
        ...
        ...
        ...
        creditCardExpDate: {type: GraphQLString},
        numLogins: {type: GraphQLInt},
        quiz: {
            type: new GraphQLList(quizType),
            resolve: (THIS) => Customers.quiz({id: THIS.id})
        }
    }
});

All fields comes from SQL besides “quiz”, resolve method for “quiz” return Mongoose promise which then return array of object. But on this query:

{
  getCustomers(id: 45) {
    id
    firstName
    quiz {
      message
      recommendedProducts
    }
  }
}

I get:

{
  "data": {
    "getCustomers": [
      {
        "id": 45,
        "firstName": "Genesis",
        "quiz": [
          {
            "message": null,
            "recommendedProducts": null
          }
        ]
      }
    ]
  }
}

Any solution so far?

0reactions
leebyroncommented, Jul 6, 2015

Thank YOU for playing with it in such an early state and investigating. I may not have found this issue otherwise.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async await strange behaviour with functions - Stack Overflow
In the first test's loop, await causes the loop to pause at the getSomePromise() function, until the promise has resolved. In the second...
Read more >
Finding Broken Promises in Asynchronous ... - People
a promise can only be settled once. Each promise object is equipped with two functions, resolve and reject, that are used to to...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
An async function allows you to handle asynchronous code in a manner that appears synchronous. async functions still use promises under the hood ......
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
A Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught...
Read more >
Finding Broken Promises in Asynchronous ... - Frank Tip
Each promise object is equipped with two functions, resolve and reject, that are used to to fulfill or reject the promise. Promises enable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found