Skip to content

Commit 3899789

Browse files
committed
add readme
1 parent 06fb73e commit 3899789

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

Diff for: README.md

+44-32
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
1-
# create-svelte
1+
# Apollo Server Integration for SvelteKit
22

3-
Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).
3+
[![npm version](https://badge.fury.io/js/apollo-server-integration-svelte.svg)](https://badge.fury.io/js/apollo-server-integration-svelte)
44

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.
66

7-
## Creating a project
7+
## Installation
88

9-
If you're seeing this, you've probably already done this step. Congrats!
9+
To install the package, run the following command:
1010

1111
```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:
1420

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);
1736
```
1837

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`.
2039

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:
2241

2342
```bash
2443
npm run dev
25-
26-
# or start the server and open the app in a new browser tab
27-
npm run dev -- --open
2844
```
2945

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).
3147

32-
## Building
48+
## API
3349

34-
To build your library:
50+
### `startServerAndCreateSvelteKitHandler(server, options?)`
3551

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.
3953

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.
4157

42-
```bash
43-
npm run build
44-
```
58+
## Contributing
4559

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).
4761

48-
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
62+
## License
4963

50-
## Publishing
64+
This package is licensed under the [MIT License](https://opensource.org/licenses/MIT).
5165

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
5367

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.
5569

56-
```bash
57-
npm publish
58-
```
70+
Special thanks to the Apollo Server and SvelteKit communities for their excellent work.

0 commit comments

Comments
 (0)