-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from ProjectEvergreen/release/0.5.0
release/0.5.0
- Loading branch information
Showing
90 changed files
with
3,644 additions
and
526 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { ApolloClient } = require('apollo-client'); | ||
const createHttpLink = require('apollo-link-http').createHttpLink; | ||
const crypto = require('crypto'); | ||
const fetch = require('node-fetch'); | ||
const fs = require('fs-extra'); | ||
const { gql } = require('apollo-server'); | ||
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache; | ||
const path = require('path'); | ||
|
||
/* Extract cache server-side */ | ||
module.exports = async (req, context) => { | ||
|
||
return new Promise(async(resolve, reject) => { | ||
try { | ||
const client = await new ApolloClient({ | ||
link: createHttpLink({ | ||
uri: 'http://localhost:4000?q=internal', /* internal flag to prevent looping cache on request */ | ||
fetch | ||
}), | ||
cache: new InMemoryCache() | ||
}); | ||
|
||
/* Take the same query from request, and repeat the query for our server side cache */ | ||
const { query, variables } = req.body; | ||
|
||
let { data } = await client.query({ | ||
query: gql`${query}`, | ||
variables | ||
}); | ||
|
||
if (data) { | ||
const cache = JSON.stringify(client.extract()); | ||
const md5 = crypto.createHash('md5').update(cache).digest('hex'); | ||
|
||
/* Get the requests entire (full) route and rootRoute to use as reference for designated cache directory */ | ||
const { origin, referer } = req.headers; | ||
const fullRoute = referer.substring(origin.length, referer.length); | ||
const rootRoute = fullRoute.substring(0, fullRoute.substring(1, fullRoute.length).indexOf('/') + 1); | ||
const targetDir = path.join(context.publicDir, rootRoute); | ||
const targetFile = path.join(targetDir, `${md5}-cache.json`); | ||
|
||
await fs.mkdirs(targetDir, { recursive: true }); | ||
await fs.writeFile(path.join(targetFile), cache, 'utf8'); | ||
} | ||
resolve(); | ||
} catch (err) { | ||
console.error('create cache error', err); | ||
reject(err); | ||
} | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ApolloClient } from 'apollo-client'; | ||
import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
import { HttpLink } from 'apollo-link-http'; | ||
|
||
const APOLLO_STATE = window.__APOLLO_STATE__; // eslint-disable-line no-underscore-dangle | ||
const client = new ApolloClient({ | ||
cache: new InMemoryCache().restore(APOLLO_STATE), | ||
link: new HttpLink({ | ||
uri: 'http://localhost:4000' | ||
}) | ||
}); | ||
const backupQuery = client.query; | ||
|
||
client.query = (params) => { | ||
|
||
if (APOLLO_STATE) { | ||
// __APOLLO_STATE__ defined, in "SSG" mode... | ||
const root = window.location.pathname.split('/')[1]; | ||
const rootSuffix = root === '' | ||
? '' | ||
: '/'; | ||
|
||
return fetch(`/${root}${rootSuffix}cache.json`) | ||
.then(response => response.json()) | ||
.then((response) => { | ||
// mock client.query response | ||
return { | ||
data: new InMemoryCache().restore(response).readQuery(params) | ||
}; | ||
}); | ||
} else { | ||
// __APOLLO_STATE__ NOT defined, in "SPA" mode | ||
return backupQuery(params); | ||
} | ||
}; | ||
|
||
export default client; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
query($parent: String!) { | ||
children(parent: $parent) { | ||
id, | ||
title, | ||
link, | ||
filePath, | ||
fileName, | ||
template | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
query { | ||
config { | ||
devServer { | ||
port, | ||
host | ||
}, | ||
meta { | ||
name, | ||
rel, | ||
content, | ||
property, | ||
value, | ||
href | ||
}, | ||
publicPath, | ||
title, | ||
workspace | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
query { | ||
graph { | ||
id, | ||
title, | ||
link, | ||
filePath, | ||
fileName, | ||
template | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
query($name: String, $route: String, $order: MenuOrderBy) { | ||
menu(name: $name, pathname: $route, orderBy: $order) { | ||
item { | ||
label, | ||
link | ||
} | ||
children { | ||
item { | ||
label, | ||
link | ||
}, | ||
children { | ||
item { | ||
label, | ||
link | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const gql = require('graphql-tag'); | ||
|
||
const getConfiguration = async (root, query, context) => { | ||
return context.config; | ||
}; | ||
|
||
// https://www.greenwoodjs.io/docs/configuration | ||
const configTypeDefs = gql` | ||
type DevServer { | ||
port: Int, | ||
host: String | ||
} | ||
type Meta { | ||
name: String, | ||
value: String, | ||
content: String, | ||
rel: String, | ||
property: String, | ||
href: String | ||
} | ||
type Config { | ||
devServer: DevServer, | ||
meta: [Meta], | ||
publicPath: String, | ||
title: String, | ||
workspace: String | ||
} | ||
extend type Query { | ||
config: Config | ||
} | ||
`; | ||
|
||
const configResolvers = { | ||
Query: { | ||
config: getConfiguration | ||
} | ||
}; | ||
|
||
module.exports = { | ||
configTypeDefs, | ||
configResolvers | ||
}; |
Oops, something went wrong.