|
1 |
| -# create-svelte |
| 1 | +# Apollo Server Integration for SvelteKit |
2 | 2 |
|
3 |
| -Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte). |
| 3 | +[](https://badge.fury.io/js/apollo-server-integration-svelte) |
4 | 4 |
|
5 |
| -Read more about creating a library [in the docs](https://kit.svelte.dev/docs/packaging). |
| 5 | +This package provides an integration for using [Apollo Server](https://www.apollographql.com/docs/apollo-server/) with [SvelteKit](https://kit.svelte.dev/). It allows you to easily set up a GraphQL server within your SvelteKit application. |
6 | 6 |
|
7 |
| -## Creating a project |
| 7 | +## Installation |
8 | 8 |
|
9 |
| -If you're seeing this, you've probably already done this step. Congrats! |
| 9 | +To install the package, run the following command: |
10 | 10 |
|
11 | 11 | ```bash
|
12 |
| -# create a new project in the current directory |
13 |
| -npm create svelte@latest |
| 12 | +npm install apollo-server-integration-svelte |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +1. Create a new file named `+server.ts` inside the `src/routes/graphql` directory of your SvelteKit project. |
| 18 | + |
| 19 | +2. Import the necessary modules and define your GraphQL schema and resolvers: |
14 | 20 |
|
15 |
| -# create a new project in my-app |
16 |
| -npm create svelte@latest my-app |
| 21 | +```typescript |
| 22 | +import { startServerAndCreateSvelteKitHandler } from 'apollo-server-integration-svelte'; |
| 23 | +import { ApolloServer } from '@apollo/server'; |
| 24 | +import type { BaseContext } from '@apollo/server'; |
| 25 | +import { typeDefs } from '$lib/graphql/schema'; |
| 26 | +import { resolvers } from '$lib/graphql/resolvers'; |
| 27 | +import type { RequestHandler } from './$types'; |
| 28 | + |
| 29 | +const server = new ApolloServer<BaseContext>({ |
| 30 | + typeDefs, |
| 31 | + resolvers, |
| 32 | +}); |
| 33 | + |
| 34 | +export const POST: RequestHandler = startServerAndCreateSvelteKitHandler(server); |
| 35 | +export const GET: RequestHandler = startServerAndCreateSvelteKitHandler(server); |
17 | 36 | ```
|
18 | 37 |
|
19 |
| -## Developing |
| 38 | +3. Create your GraphQL schema and resolvers in separate files, such as `src/lib/graphql/schema.ts` and `src/lib/graphql/resolvers.ts`. |
20 | 39 |
|
21 |
| -Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: |
| 40 | +4. Start your SvelteKit development server: |
22 | 41 |
|
23 | 42 | ```bash
|
24 | 43 | npm run dev
|
25 |
| - |
26 |
| -# or start the server and open the app in a new browser tab |
27 |
| -npm run dev -- --open |
28 | 44 | ```
|
29 | 45 |
|
30 |
| -Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app. |
| 46 | +5. Access your GraphQL endpoint at `http://localhost:5173/graphql` (or the appropriate URL based on your SvelteKit configuration). |
31 | 47 |
|
32 |
| -## Building |
| 48 | +## API |
33 | 49 |
|
34 |
| -To build your library: |
| 50 | +### `startServerAndCreateSvelteKitHandler(server, options?)` |
35 | 51 |
|
36 |
| -```bash |
37 |
| -npm run package |
38 |
| -``` |
| 52 | +This function takes an instance of `ApolloServer` and optional `options` and returns a request handler that can be used as a SvelteKit server route handler. |
39 | 53 |
|
40 |
| -To create a production version of your showcase app: |
| 54 | +- `server`: An instance of `ApolloServer` configured with your GraphQL schema and resolvers. |
| 55 | +- `options` (optional): An object containing additional options for the handler. |
| 56 | + - `context` (optional): A function that returns a custom context object for each request. |
41 | 57 |
|
42 |
| -```bash |
43 |
| -npm run build |
44 |
| -``` |
| 58 | +## Contributing |
45 | 59 |
|
46 |
| -You can preview the production build with `npm run preview`. |
| 60 | +Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the [GitHub repository](https://github.com/pabl-o-ce/apollo-server-integration-svelte). |
47 | 61 |
|
48 |
| -> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment. |
| 62 | +## License |
49 | 63 |
|
50 |
| -## Publishing |
| 64 | +This package is licensed under the [MIT License](https://opensource.org/licenses/MIT). |
51 | 65 |
|
52 |
| -Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)). |
| 66 | +## Acknowledgements |
53 | 67 |
|
54 |
| -To publish your library to [npm](https://www.npmjs.com): |
| 68 | +This package is inspired by the official [apollo-server-integration-next](https://www.npmjs.com/package/apollo-server-integration-next) package and adapted for SvelteKit. |
55 | 69 |
|
56 |
| -```bash |
57 |
| -npm publish |
58 |
| -``` |
| 70 | +Special thanks to the Apollo Server and SvelteKit communities for their excellent work. |
0 commit comments