Created
September 27, 2017 02:47
-
-
Save rauljordan/f7495a3b714f8b4e9ec0f9cdfcb61606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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