Catch the highlights of GraphQLConf 2023!Click for recordings.Or check out our recap blog post.
v2
Integrations
SvelteKit
⚠️
This is the documentation for the old GraphQL Yoga version 2. We recommend upgrading to the latest GraphQL Yoga version 5.

Migrate to GraphQL Yoga v5

Integration with SvelteKit

SvelteKit (opens in a new tab) is the fastest way to build svelte apps. It is very simple and lets you build the frontend & backend in a single place.

You can add GraphQL Yoga with a few lines of code and get the benefits of GraphQL & SvelteKit at the same time. Envelop ecosystem (opens in a new tab) for example!

Installation

In a SvelteKit project:

Terminal
yarn add @graphql-yoga/common graphql

Example

Create your GraphQL Endpoint

Create the following file:

src/routes/api/graphql.ts
import { createServer } from '@graphql-yoga/common'
import type { RequestEvent } from '@sveltejs/kit'
 
const yogaApp = createServer<RequestEvent>({
  schema: {
    typeDefs: /* GraphQL */ `
      type Query {
        hello: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'SvelteKit - GraphQL Yoga'
      }
    }
  },
  graphiql: {
    endpoint: '/api/graphql'
  }
})
 
export { yogaApp as get, yogaApp as post }
💡

Simple example on our GitHub repository here (opens in a new tab).

🪄

More examples with our KitQL library here (opens in a new tab). The best of all GraphQL ecosystem for SvelteKit.