Skip to content

Commit

Permalink
working structured example
Browse files Browse the repository at this point in the history
  • Loading branch information
derekjwilliams committed Mar 28, 2020
1 parent 9e4008e commit e52f8d5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
4 changes: 2 additions & 2 deletions data-services/html-source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Uses simple web scrapping technique with Cherrio using the mimi.pnca.edu web site (for now)

## JavaScript and a TypeScript examples
## JavaScript examples

The two examples are found in mimi-server and mimi-server__typescript. See the READMEs in those directories for instructions on installing and running, and example data.
The two examples are found in mimi-server and mimi-server__structured. See the READMEs in those directories for instructions on installing and running, and example data.

## Example HTML

Expand Down
3 changes: 3 additions & 0 deletions data-services/html-source/mimi-server--structured/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
12 changes: 10 additions & 2 deletions data-services/html-source/mimi-server--structured/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# This simple example performs the same functionality as the example found in ../mimi-server, but uses graphql-tools from Apollo
# This simple example is the same as ../mimi-server but structured

This is a fairly standard structure, with the schema and resolvers in to their own files.

## Other changes from ../mimi-server

### import and export instead of require

[esm](https://www.npmjs.com/package/esm) is used to provide support for standard JavaScript import and export, which are not supported natively in node yet (as of 13.9). The `--experimental-support` flag could also be used if one does not want to use esm.


See https://www.apollographql.com/docs/graphql-tools/ for documentation

30 changes: 30 additions & 0 deletions data-services/html-source/mimi-server--structured/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fetch = require('node-fetch');
import cheerio from 'cheerio';


const getCollections = async(kind) => {
return fetch(`https://mimi.pnca.edu/f/${kind}`, {"credentials":"exclude","headers":{"accept":"text/html,application/xhtml+xml","accept-language":"en-US","cache-control":"no-cache","pragma":"no-cache"},"method":"GET"})
.then((response) => {
return response.text()
}).then((html) => {
const collections = []
const $ = cheerio.load(html)
$('a[data-no-turbolink="false"] h3').each(function (i, e) {
const collectionsTitle = $(this).text();
if (collectionsTitle !== 'Collections') {
const item = {}

//TODO a bit of a hack parsing the html, which is prone to failure, add some checks on elements
const style = $(this).parent().attr('style')
const backgroundImage = style.indexOf("''") === -1 ?
(style.split('background-image:url(\'')[1]).split('?')[0]: ''
item['href'] = $(this).parent().parent().attr('href')
item['backgroundImage'] = backgroundImage
item['title'] = collectionsTitle.trim()
collections.push(item);
}
});
return collections
})
}
export { getCollections }
12 changes: 8 additions & 4 deletions data-services/html-source/mimi-server--structured/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { ApolloServer, gql } = require('apollo-server');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const { resolverMap } = require('./resolvers');
// const schema = require('schema');
import resolverMap from './resolvers';
import typeDefs from './schema';

const server = new ApolloServer({ typeDefs, resolvers: resolverMap });

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand All @@ -12,6 +13,7 @@
"dependencies": {
"apollo-server": "^2.11.0",
"cheerio": "^1.0.0-rc.3",
"esm": "^3.2.25",
"graphql": "^14.6.0",
"node-fetch": "^2.6.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCollections } from './collections'
const resolverMap = {
Query: {
collections: async(_, args) => {
Expand Down

0 comments on commit e52f8d5

Please sign in to comment.