I’ve tried to use async/await for resolver functio...
# prisma-whats-new
y
I’ve tried to use async/await for resolver functions and it didn’t work (‘use latest’ added). Some ES6/ES7 features are still not available?
a
Are you using a legacy project for this? In that case, you need
require('babel/polyfill')
as the first line to support async/await. Framework projects run on Node 6, so there async/await is only available if you use Typescript, not in vanilla JS.
y
Hm, still doesn’t work. I get
{"error":"The resolver function allPosts is not nullable, but the function returned null."}
Full code
Copy code
'use latest';
require('babel/polyfill');
require('isomorphic-fetch');

const url = '<https://simple-rest-api-sexbspkvvi.now.sh/posts>';

const apiSanitizer = ({ id, title, text }) => ({
  _id: id,
  title,
  text,
});

export default async () => {
  const response = await fetch(url);
  const posts = response.map(post => apiSanitizer(post));
  return { data: posts };
};
@agartha what is legacy project? Just was doing after examples from official repo
a
Legacy project is where you create your functions in the console. Framework project is where you use the CLI.
y
aha, so I don’t use legacy
a
In that case, did you npm install isomorphic-fetch? And you need to remove the first two lines. And this async-await syntax only works if it's a TS file. And if it's a TS file, you need to change the require to
import fetch from 'isomorphic-fetch'
And what does the SDL look like for this function?
y
you mean graphql types?
Copy code
type AllPostsPayload {
  _id: Int!
  title: String!
  text: String!
}

extend type Query {
  allPosts: [AllPostsPayload!]!
}
a
Okay, that looks good.
Just checkin'...
So then it's just about making those changes to the code
y
Nope, just changed to
import fetch from 'isomorphic-fetch';
and still doesn’t work 😞 I have isomorphic-fetch installed from npm
a
Did you also do the other things I mentioned? What you code like now?
y
Copy code
import fetch from 'isomorphic-fetch';

const url = '<https://simple-rest-api-sexbspkvvi.now.sh/posts>';

const apiSanitizer = ({ id, title, text }) => ({
  _id: id,
  title,
  text,
});

export default async () => {
  const response = await fetch(url);
  const posts = response.map(post => apiSanitizer(post));
  return { data: posts };
};
a
Don't you need to do a JSON.parse() on response first?
const response = await response.json()
y
nope, doesn’t work as well
a
And you made it a Typescript file?
y
no, that’s just vanilla JS
a
Read back, I mentioned that async/await does not work in vanilla JS
y
Aha 😞
a
And this async-await syntax only works if it's a TS file.
And if it's a TS file, you need to change the require to
import fetch from 'isomorphic-fetch'
y
thanks a lot
a
You can just change the extension, and update your graphcool.yml, nothing else needed
y
Got it, thanks a lot! 🙂
👍🏻 1
a
this can’t be true? async await is used in JS files in the templates https://github.com/graphcool/templates/blob/master/auth/auth0/src/auth0Authentication.js
a
@nilan Can you shed some light on this, because now I'm confused
Are JS functions also babeled?