-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
release/0.5.0 #284
release/0.5.0 #284
Changes from all commits
babca10
8d1529a
ad9515b
7b581a2
537133a
cb8742c
491707e
b746b81
6ace1f2
10edc61
ff3f170
319b76d
d8b9ee1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
}); | ||
}; |
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; |
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 | ||
} | ||
} |
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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
query { | ||
graph { | ||
id, | ||
title, | ||
link, | ||
filePath, | ||
fileName, | ||
template | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this need some logic to scale, or if I am reading this right, this only supports one level of nesting, right? Maybe related to #273 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, only one nested level |
||
item { | ||
label, | ||
link | ||
} | ||
} | ||
} | ||
} | ||
} |
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 | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice if we could remove the dependency on this. will track in Trello