yuri
11/20/2017, 2:18 PMagartha
11/20/2017, 2:28 PMrequire('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.yuri
11/20/2017, 2:33 PM{"error":"The resolver function allPosts is not nullable, but the function returned null."}
yuri
11/20/2017, 2:33 PM'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 };
};
yuri
11/20/2017, 2:35 PMagartha
11/20/2017, 2:36 PMyuri
11/20/2017, 2:37 PMagartha
11/20/2017, 2:39 PMimport fetch from 'isomorphic-fetch'
agartha
11/20/2017, 2:40 PMyuri
11/20/2017, 2:41 PMyuri
11/20/2017, 2:41 PMtype AllPostsPayload {
_id: Int!
title: String!
text: String!
}
extend type Query {
allPosts: [AllPostsPayload!]!
}
agartha
11/20/2017, 2:41 PMagartha
11/20/2017, 2:41 PMagartha
11/20/2017, 2:42 PMyuri
11/20/2017, 2:42 PMimport fetch from 'isomorphic-fetch';
and still doesn’t work 😞 I have isomorphic-fetch installed from npmagartha
11/20/2017, 2:43 PMyuri
11/20/2017, 2:43 PMimport 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 };
};
agartha
11/20/2017, 2:44 PMagartha
11/20/2017, 2:44 PMconst response = await response.json()
yuri
11/20/2017, 2:46 PMagartha
11/20/2017, 2:47 PMyuri
11/20/2017, 2:47 PMagartha
11/20/2017, 2:47 PMyuri
11/20/2017, 2:48 PMagartha
11/20/2017, 2:48 PMAnd 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 toimport fetch from 'isomorphic-fetch'
yuri
11/20/2017, 2:48 PMagartha
11/20/2017, 2:48 PMyuri
11/20/2017, 2:50 PMalexanbj
11/20/2017, 8:14 PMagartha
11/20/2017, 8:15 PMagartha
11/20/2017, 8:18 PM