{"id":21485712,"url":"https://github.com/jackdclark/graphql-hooks-ssr","last_synced_at":"2025-06-10T16:11:15.076Z","repository":{"id":77695796,"uuid":"173966908","full_name":"jackdclark/graphql-hooks-ssr","owner":"jackdclark","description":"Server-side rendering utils for graphql-hooks","archived":false,"fork":false,"pushed_at":"2019-03-05T14:56:11.000Z","size":32,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T10:08:50.860Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jackdclark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-05T14:57:55.000Z","updated_at":"2021-07-05T08:43:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"d06427f3-e70a-49d9-9aa4-13e19151a27b","html_url":"https://github.com/jackdclark/graphql-hooks-ssr","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdclark%2Fgraphql-hooks-ssr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdclark%2Fgraphql-hooks-ssr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdclark%2Fgraphql-hooks-ssr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdclark%2Fgraphql-hooks-ssr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jackdclark","download_url":"https://codeload.github.com/jackdclark/graphql-hooks-ssr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdclark%2Fgraphql-hooks-ssr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259106728,"owners_count":22805941,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T13:17:11.062Z","updated_at":"2025-06-10T16:11:15.052Z","avatar_url":"https://github.com/jackdclark.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 📣 NOTICE 📣\nThis repository has moved into the [`graphql-hooks` monorepo](https://github.com/nearform/graphql-hooks) and is no longer maintained here.\n\n🏡 New home: [graphql-hooks-ssr](https://github.com/nearform/graphql-hooks/tree/master/packages/graphql-hooks-ssr). \n\nPlease raise any issues for this package in the above repository, thank you.\n\n---\n\n# graphql-hooks-ssr\n\nServer-side rendering utils for `graphql-hooks`\n\n## Install\n\n`npm install graphql-hooks-ssr`\n\nor\n\n`yarn add graphql-hooks-ssr`\n\n## Quick Start\n\nThe below example is for `fastify` but the same principles apply for `express` \u0026 `hapi`.\n\n```js\nconst { GraphQLClient } = require('graphql-hooks')\nconst memCache = require('graphql-hooks-memcache');\nconst { getInitialState } = require('graphql-hooks-ssr');\nconst { ServerLocation } = require('@reach/router');\n// NOTE: use can use any 'fetch' polyfill\nconst fetch = require('isomorphic-unfetch');\n\napp.get('/', async (req, reply) =\u003e {\n  // Step 1: Create the client inside the request handler\n  const client = new GraphQLClient({\n    url: 'https://domain.com/graphql',\n    cache: memCache(),\n    fetch\n  });\n\n  // Step 2: Provide the `client`\n  // Optional: If your app contains a router, you'll need to tell it which route the user is on\n  // based on the request.. this example uses @reach/router\n  const App = (\n    \u003cClientContext.Provider value={client}\u003e\n      \u003cServerLocation url={req.raw.url}\u003e\n        {/* Your App component goes here */}\n      \u003c/ServerLocation\u003e\n    \u003c/ClientContext.Provider\u003e\n  );\n\n  // Step 3: Use the getInitialState method from graphql-hooks-ssr\n  // Pass in App + GraphQL client\n  const initialState = await getInitialState({ App, client });\n\n  // Step 4: Render the your App - all queries will now be cached\n  const content = ReactDOMServer.renderToString(App);\n\n  // Step 5: Serialise the initialState object + include it in the html payload\n  const html = `\n      \u003c!DOCTYPE html\u003e\n      \u003chtml\u003e\n        \u003cbody\u003e\n          \u003cdiv id=\"app-root\"\u003e${content}\u003c/div\u003e\n          \u003cscript type=\"text/javascript\"\u003e\n            window.__INITIAL_STATE__=${JSON.stringify(initialState).replace(\n              /\u003c/g,\n              '\\\\u003c'\n            )};  \n          \u003c/script\u003e\n        \u003c/body\u003e\n      \u003c/html\u003e\n    `;\n\n  reply.type('text/html').send(html);\n});\n```\n\n### API\n\n#### `getInitialState(options)`\n\nReturns the serialisable cache after fetching all queries.\n\n- `options.App`: The react component to render\n- `options.client`: An instance of `GraphQLClient` from `graphql-hooks`\n- `options.render`: A custom render function; defaults to `ReactDOMServer.renderToStaticMarkup`\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackdclark%2Fgraphql-hooks-ssr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjackdclark%2Fgraphql-hooks-ssr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackdclark%2Fgraphql-hooks-ssr/lists"}