简体   繁体   English

在graphql查询或突变上异步/等待

[英]async/await on graphql query or mutation

I made API server with graphql-yoga . 我用graphql-yoga制作了API服务器。 (a nodejs library) (一个nodejs库)

Before searching Google, I just use query/mutation like this. 在搜索Google之前,我只使用这样的查询/变异。

[First case] [第一种情况]

Query: {
    movies: () => { return Movies.all();}
}

But after searching I found some code that use await/async on query/mutation. 但是在搜索之后,我发现一些在查询/变异上使用await / async的代码。

[Second case] [第二种情况]

Query: {
    movies: async () => { return await Movies.all(); }
}

By my little knowledge, second case is more safe and better case. 据我所知,第二种情况更安全,更好。

But I'm new at graphql and es6. 但是我是graphql和es6的新手。

Is there any process related async/await already defined in graphql? 在graphql中已经定义了与异步/等待相关的任何进程吗?

Or, do not have to consider about it? 或者,不必考虑吗?

Or, use async/await is better? 或者,使用async / await更好?

Any suggestions would be appreciated :) 任何建议,将不胜感激 :)

Thanks. 谢谢。

async/await is an ES6 paradigm unrelated to GraphQL . async/await与GraphQL不相关的ES6范例。

  • It is syntactic sugar supposed to make Promises based code "easier" to read. 它是语法糖,应该使基于Promises的代码“更容易阅读”。
  • It doesn't provide better performance nor overhead. 它既没有提供更好的性能,也没有开销。

You should keep working with promises until you understand it enough to feel the need for and async/await migration. 您应该继续遵守Promise,直到您对Promise足够了解,从而感觉到需要进行async/await迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM