{"id":25035111,"url":"https://github.com/liinkiing/next-ts-graphql-apollo-starter","last_synced_at":"2025-10-17T11:54:12.145Z","repository":{"id":47904413,"uuid":"255759748","full_name":"Liinkiing/next-ts-graphql-apollo-starter","owner":"Liinkiing","description":"An opiniated Next powered starter which include support for Apollo with GraphQL SSR support, codegen, styled component / system, framer motion and Cypress","archived":false,"fork":false,"pushed_at":"2022-06-08T21:27:10.000Z","size":830,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T10:06:06.046Z","etag":null,"topics":["apollo","codegen","cypress","framer-motion","graphql","next","react","ssr","starter","styled-component","styled-system","template","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Liinkiing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-15T00:20:17.000Z","updated_at":"2024-01-25T06:23:54.000Z","dependencies_parsed_at":"2022-09-19T02:13:17.966Z","dependency_job_id":null,"html_url":"https://github.com/Liinkiing/next-ts-graphql-apollo-starter","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":"Liinkiing/next-ts-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liinkiing%2Fnext-ts-graphql-apollo-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liinkiing%2Fnext-ts-graphql-apollo-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liinkiing%2Fnext-ts-graphql-apollo-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liinkiing%2Fnext-ts-graphql-apollo-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Liinkiing","download_url":"https://codeload.github.com/Liinkiing/next-ts-graphql-apollo-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248766738,"owners_count":21158309,"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":["apollo","codegen","cypress","framer-motion","graphql","next","react","ssr","starter","styled-component","styled-system","template","typescript"],"created_at":"2025-02-05T23:45:19.090Z","updated_at":"2025-10-17T11:54:07.112Z","avatar_url":"https://github.com/Liinkiing.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Next GraphQL Apollo Starter\n\nA starter to bootstrap your **Next** application (nice pun gg) with some noice GraphQL\n(**🎊 With SSR support 🎊**) with Apollo and [GraphQL code generator](https://graphql-code-generator.com/)\n\n## Usage\n\n```bash\n$ yarn\n# install dependencies\n\n$ yarn dev\n# launch concurrently gql-gen:watch and dev:next\n\n$ yarn gql-gen\n# launch GraphQL code generation based on codegen.yaml\n\n$ yarn gql-gen:watch\n# same as above, with watch mode\n\n$ yarn dev:next\n# launch Next dev script\n\n$ yarn build\n# launch Next build script\n\n$ yarn start\n# launch Next start script\n\n$ yarn test\n# launch test suite\n\n$ yarn cy:open\n# open cypress (you first need to install deps in cypress folder)\n\n$ yarn ts:check\n# check TypeScript\n\n$ yarn lint\n# run ESLint\n```\n\n## GraphQL support\n\nThe starter comes by default with Apollo@3. All Apollo related code and config is located under `~/apollo` folder.  \nIt uses environment variables to define the API endpoint, so you have to copy/paste the `.env.sample`\nfile and rename it to `.env` (not committed). The variable name is `NEXT_PUBLIC_GRAPHQL_API`\n\nTo make the codegen work, you must have a `schema.graphql` at the root (can be\nmodified in the `codegen.yaml`, see [the configuration reference](https://graphql-code-generator.com/docs/getting-started/codegen-config)).\nBased on this and your \\*.graphql files in `~/graphql/**`, it will auto generate\ncorresponding hooks.\n\nWhen writing \\*.graphql files, you can also import other \\*.graphql files by using\ncomments, but this is no longer necessary because last versions of graphql codegen inline\n\\*.graphql docs.\n\n`~/graphql/fragments/ProjectCard_project.graphql`\n\n```graphql\nfragment ProjectCard_project on Project {\n  title\n  body\n}\n```\n\n`~/graphql/queries/Projects.graphql`\n\n```graphql\nquery Projects {\n  projects {\n    id\n    ...ProjectCard_project\n  }\n}\n```\n\n### Using Apollo in server side\n\nIf you want to fetch your data with Apollo from your server, you must use the\nprovided functions `initializeApollo` and `addApolloState`. They are used to\nget apollo either from an already initialized client or a new one. They are\nalso responsible for populating the Apollo store and restore it on the client side.\n\nHere is an example on how to use it\n\n```tsx\nimport { gql } from '@apollo/client'\nimport type { GetServerSideProps, NextPage } from 'next'\nimport { useHelloQuery } from '~/__generated__/graphql'\nimport { addApolloState, initializeApollo } from '~/apollo/client'\nconst Page: NextPage = () =\u003e {\n   data will already be available on first render\n  // be\n  se it has been fetched on your server side and been \n  // po\n  ated into the apollo cache store.\n  const\n  const { data } = useHelloQuery()\n  return \u003cdiv\u003e{data.hello}\u003c/div\u003e\n}\nexport const getServerSideProps: GetServerSideProps = async ctx =\u003e {\n  const client = initializeApollo()\n  const { data } = await client.query({\n    query: gql`\n      query Hello {\n        hello\n      }\n    `,\n  })\n  if (!data.hello) {\n    return { notFound: true }\n  }\n  return addApolloState(client, { props: {} })\n}\n```\n\nYou can also use the provided hook `useApollo` to get your apollo client instance with the\ncorrect store cache if you made a query on your server side.\n\n### Configuration\n\nAll configuration related files are located in the `codegen.yaml` file ([more informations here](https://graphql-code-generator.com/docs/getting-started/codegen-config))\nA `.graphqlconfig` file is also provided, if you use a GraphQL extension in your IDE, it will allow you\nto introspect the schema of a given endpoint and writing it in a `schema.graphql` file.\nYou must enter your API url here\n\n### Examples\n\nExample usage for a given query\n\n```graphql\nquery Projects {\n  projects {\n    id\n    title\n    body\n  }\n}\n```\n\nwhich generate a hook and a component and could be used like this :\n\n```typescript jsx\nimport React, { FC } from 'react'\nimport { useProjectsQuery } from '~/__generated__/graphql'\n file is generated by gql-codegen\n\nconst\nconst Projects: FC = () =\u003e {\n  const { data, loading, error } = useHelloQuery()\n  if (error) {\n    return \u003cdiv\u003eError\u003c/div\u003e\n  }\n  if (loading) {\n    return \u003cdiv\u003eLoading...\u003c/div\u003e\n  }\n  if (data.projects) {\n      }\n  }\n  return null\n}\nconst App: FC = () =\u003e {\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003cmain\u003e\n        \u003cProjects /\u003e\n      \u003c/main\u003e\n    \u003c/div\u003e\n  )\n}\nexport default App\n```\n\nand you get all the nice **autocompletion** from your IDE thanks to Typescript!\nAnd if you change any of \\*.graphql files to add a new field for a GraphQL query,\nit will be automatically generated and you will be always in sync with your GraphQL files!\n\n## Next config\n\nIt comes already configured with some nice plugin. You can see in `next.config.js` what is\nused. In short, it allows support of importing images files and fonts within webpack.\nIt also comes with **NProgress** support, by default so it shows a small loading bar in top of\nthe page when loading. You can find the component in `~/components/NProgress.tsx`, and it is used in the\ncustom `_app.tsx`\n\n## Styled component\n\nThe template comes with [styled-components](https://github.com/styled-components/styled-components).\nAgain, you can either choose to not use it, this is a personal choice.\nYou can also find a `styles` folder, which contains many related\nstyled-components files to keep things organized. It's also includes all themes-related stuff in here.\nIt's again a personal convention that I follow, feel free to annihilate this directory if you want 😢\n\n## Styled system\n\nIt also comes with [styled-system](https://github.com/styled-system/styled-system). It is a great way to\nbuild reusable UI blocks with a great props API and consistent spaces / typography.\nA lot comes from the theme, provided in `~/styles/themes/base.ts` where we define some\nbreakpoints, spacings and typography stuff. It allows then the custom `AppBox` component (`~/ui/AppBox`)\nto be aware of your theme and then build something amazing with the primitives.\nBy default, this starter provides some basic examples components that uses this pattern, for\nexample the `AppNav` component (`~/components/layout/AppNav`).\n\n🔥 It also supports and provides autocomplete for props that takes source from the theme (e.g `bg`, `zIndex`, `border`...). 🔥\n\n**TypeScript \u003e 4.1** is mandatory because I'm using the new [Template Litteral Types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#template-literal-types) to\nprovide autocompletion of the color prop based of the nested `colors` object, so when using\n`bg` prop for exemple, you should have autocompletion for `red.xxx, blue.xxx` or anything defined in your `colors.ts`.\nIt supports nested elements with a dot notation! (that's why TS 4.1 is required)\n\n## Framer motion\n\nAgain, personal preference here, but the starter comes with framer motion already configured\nto handle Next pages changes and enable some smooth transitions when navigating. You\ncan find the default variant used for the page transitions in `~/common/framer.ts`.\n\n## Testing\n\n[Jest](https://github.com/facebook/jest) and [@testing-library/react](https://github.com/testing-library/react-testing-library) is used to run your tests. It comes preconfigured\nwith [ts-jest](https://github.com/kulshekhar/ts-jest) so your tests also checks your types.\nYou can look the **jest.config.js** and the file **setupTest.ts** to see what's in there.\n[jest-styled-components](https://github.com/styled-components/jest-styled-components) is also used to have deterministic classNames\nwithin your styled components that you are testing.\n[Cypress](https://cypress.io) starter for e2e tests. Take a look at Cypress config file **cypress.json**\n\n### Cypress\n\nThis starter comes by default with [Cypress](https://cypress.io) and some sensible defaults and a custom plugin which enable you to\ninject your .env\\* files in the Cypress.env helper. It also add support for a per-environment configuration file.\nJust add a cypress.\u003cenv\u003e.json. It uses by default the `cypress.json` and then extend the configuration if you have\na `cypress.\u003cenv\u003e.json`. Take a look at the various configuration file in the `cypress` folder and the custom plugin in `cypress/plugins/index.ts`.\nThis folder is independant and lives by itself, so it has his own dependencies / scripts / tsconfig and do not pollute the\nglobal namespace. [See why an isolated folder](https://basarat.gitbook.io/typescript/intro-1/cypress#tip-creating-page-objects)\n\nIt also have an opiniated way of interacting with some of your webpages in Cypress. As your application\nwill grow (and pages would become complex), it's recommended to have some sort of \"Page objects\" which allows\nyou to work in a more conveniant way for some complex page. See more here https://basarat.gitbook.io/typescript/intro-1/cypress#tip-creating-page-objects.\n\nIt also comes with two custom commands (`cypress/support/commands.ts`) that are autocompleted (see `cypress/@types/index.d.ts`)\n\n```typescript\ncy.server() // start mocking the calls made to the server, needed to mock later the queries\ncy.mockGraphQL() // in your test, allow to start intercepting requests made to your /graphql/ endpoint\ncy.mockGraphQLOperation('ViewerQuery') // internally alias the request, so you can do later in your test cy.wait\n// your test ...\ncy.wait('@ViewerQuery') // waits for the graphql operation to succeed\n```\n\n## Aliases\n\nIt includes by default support for aliases in `tsconfig.json`.\nThey are 1 defaulted alias, ready to use :\n\n```typescript\n// ~ refers to src folder\nimport { something } from '~/file'\n```\n\nYou can also use for your convenience the global `__DEV__` variable, which is\ninjected by webpack with the DefinePlugin (see **next.config.js**).\n\n## @types and extending modules\n\nIt also includes a `@types` directory under **src**, so you can easily\nseparate your types or extends some external modules. They are also included in the `tsconfig.json`\nFor example, if some package named `foo` does not have any types in [DefinitelyTyped](https://definitelytyped.org/), you could\nadd a `index.d.ts` under `src/@types/foo/index.d.ts`. It is just my personal convention, so do as you want!\n\n```typescript\n// src/@types/foo/index.d.ts\n\n\n// to make sure Typescript get the original types from the module (if any\nimport * as foo from 'foo'\n\ndeclare module 'foo' {\n  declare function foo(bar: string): boolean\n}\n```\n\nBecause the `@types` directory is declared in `typeRoots`, Typescript will no longer complain if you imported your package with missing types\n\n## Tooling\n\nThe template includes [Prettier](https://prettier.io/), [ESLint](https://eslint.org/) (with [Typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)), [Babel](https://babeljs.io/) and [lint-staged](https://github.com/okonet/lint-staged).\nAll their related configurations are in the `*rc` files (except for lint-staged, which is located in the `package.json`).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliinkiing%2Fnext-ts-graphql-apollo-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliinkiing%2Fnext-ts-graphql-apollo-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliinkiing%2Fnext-ts-graphql-apollo-starter/lists"}