[Question]Why are no `async` on some resolver methods?
Hi, I'm very new to Apollo graphql and reading through the tutorial.
I noticed that launch method in resolver.js does not have async and wondering why even though the request is asynchronous. In fact, if I add async it works as expected. Could someone tell me why async is not required in this case?
module.exports = {
Query: {
launches: async (_, __, { dataSources }) =>
dataSources.launchAPI.getAllLaunches(),
launch: (_, { id }, { dataSources }) => // no async
dataSources.launchAPI.getLaunchById({ launchId: id }),
me: async (_, __, { dataSources }) =>
dataSources.userAPI.findOrCreateUser(),
},
};
@tomoima525 Hello! I'm not sure if you ever found the answer here, but just in case:
https://github.com/apollographql/fullstack-tutorial/blob/7948b85d747300fd4d1596b6173b9ee783431c75/final/server/src/datasources/launch.js#L35-L38
In the launch.js file, the getLaunchById function is designated as async. Because that is being directly returned in the handler for the launch query, it's effectively async already because that function returns a Promise.
This is actually true of both getAllLaunches and findOrCreateUser too — if you remove the async calls from the launches and me handlers, they'll continue working the same as well!