npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-server-integration-svelte

v0.0.15

Published

This package provides an integration for using Apollo Server with SvelteKit. It allows you to easily set up a GraphQL server within your SvelteKit application.

Downloads

128

Readme

Apollo Server Integration for SvelteKit

NPM Version

This package provides an integration for using Apollo Server with SvelteKit. It allows you to easily set up a GraphQL server within your SvelteKit application.

Installation

To install the package, run the following command:

npm install apollo-server-integration-svelte

Getting started

  1. Create a new file named +server.ts inside the src/routes/api/graphql directory of your SvelteKit project.

  2. Import the necessary modules and define your GraphQL schema and resolvers:

import { ApolloServer } from '@apollo/server';
import { startServerAndCreateSvelteKitHandler } from 'apollo-server-integration-svelte';
import type { RequestHandler } from 'apollo-server-integration-svelte';
import { gql } from 'graphql-tag';

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  resolvers,
  typeDefs,
});

export const GET: RequestHandler = startServerAndCreateSvelteKitHandler(server);
export const POST: RequestHandler = startServerAndCreateSvelteKitHandler(server);

You may also pass a context function to startServerAndCreateSvelteKitHandler as such:

export const GET: RequestHandler = startServerAndCreateSvelteKitHandler(server, {
  context: async (event) => ({ event, user: await getLoggedInUser(event) }),
});

export const POST: RequestHandler = startServerAndCreateSvelteKitHandler(server, {
  context: async (event) => ({ event, user: await getLoggedInUser(event) }),
});

The SvelteKit RequestEvent object is passed along to the context function.

Typescript

When using this integration with SvelteKit, you can specify the type of the context object using the generic type parameter:

import type { RequestEvent } from '@sveltejs/kit';

// The context object will have the type { event: RequestEvent, user: User }
const handler = startServerAndCreateSvelteKitHandler<{ event: RequestEvent; user: User }>(server, {
  context: async (event) => ({ event, user: await getLoggedInUser(event) }),
});

This ensures that the context object has the correct type signature.

API

startServerAndCreateSvelteKitHandler(server, options?)

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.

  • server: An instance of ApolloServer configured with your GraphQL schema and resolvers.
  • options (optional): An object containing additional options for the handler.
    • context (optional): A function that returns a custom context object for each request.

Contributing

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.

License

This package is licensed under the MIT License.

Acknowledgements

This package is inspired by the official apollo-server-integration-next package and adapted for SvelteKit.

Special thanks to the Apollo Server and SvelteKit communities for their excellent work.