Skip to content

Instantly share code, notes, and snippets.

@rauljordan
Created September 27, 2017 02:47
Show Gist options
  • Save rauljordan/f7495a3b714f8b4e9ec0f9cdfcb61606 to your computer and use it in GitHub Desktop.
Save rauljordan/f7495a3b714f8b4e9ec0f9cdfcb61606 to your computer and use it in GitHub Desktop.
export const HtmlPage = new GraphQLObjectType({
name: 'HtmlPage',
fields: () => ({
url: {
type: GraphQLString,
resolve(root, args, context) {
return root.url;
}
},
hostname: {
type: GraphQLString,
resolve(root, args, context) {
const match = root.url.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
return match && match[3];
}
},
title: {
type: GraphQLString,
resolve: async (root, args, context) => {
const res = await fetch(root.url);
const $ = cheerio.load(await res.text());
return $('title').text();
}
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment