
Product
Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
graphql-hooks-ssr
Advanced tools
Server-side rendering utils for graphql-hooks
npm install graphql-hooks-ssr
or
yarn add graphql-hooks-ssr
The below example is for fastify
but the same principles apply for express
& hapi
.
const { GraphQLClient, ClientContext } = require('graphql-hooks')
const memCache = require('graphql-hooks-memcache')
const { getInitialState } = require('graphql-hooks-ssr')
const { ServerLocation } = require('@reach/router')
// NOTE: use can use any 'fetch' polyfill
const fetch = require('isomorphic-unfetch')
app.get('/', async (req, reply) => {
// Step 1: Create the client inside the request handler
const client = new GraphQLClient({
url: 'https://domain.com/graphql',
cache: memCache(), // NOTE: a cache is required for SSR
fetch
})
// Step 2: Provide the `client`
// Optional: If your app contains a router, you'll need to tell it which route the user is on
// based on the request.. this example uses @reach/router
const App = (
<ClientContext.Provider value={client}>
<ServerLocation url={req.raw.url}>
{/* Your App component goes here */}
</ServerLocation>
</ClientContext.Provider>
)
// Step 3: Use the getInitialState method from graphql-hooks-ssr
// Pass in App + GraphQL client
const initialState = await getInitialState({ App, client })
// Step 4: Render the your App - all queries will now be cached
const content = ReactDOMServer.renderToString(App)
// Step 5: Serialise the initialState object + include it in the html payload
const html = `
<!DOCTYPE html>
<html>
<body>
<div id="app-root">${content}</div>
<script type="text/javascript">
window.__INITIAL_STATE__=${JSON.stringify(initialState).replace(
/</g,
'\\u003c'
)};
</script>
</body>
</html>
`
reply.type('text/html').send(html)
})
getInitialState(options)
Returns the serialisable cache after fetching all queries.
options.App
: The react component to renderoptions.client
: An instance of GraphQLClient
from graphql-hooks
options.render
: A custom render function; defaults to ReactDOMServer.renderToStaticMarkup
FAQs
Server side rendering utils for graphql-hooks
The npm package graphql-hooks-ssr receives a total of 28 weekly downloads. As such, graphql-hooks-ssr popularity was classified as not popular.
We found that graphql-hooks-ssr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.