SvelteKit Quickstart
This quickstart guide will show you how to use WunderGraph with SvelteKit and goes into a couple of details like server side rendering and TypeScript operations.
Creating a new WunderGraph project with SvelteKit
We'll use the SvelteKit example to get started. This example is a basic SvelteKit application that uses WunderGraph to fetch data from the SpaceX GraphQL API .
Start SvelteKit and WunderGraph
WunderGraph will now do some code generation and start the WunderNode and the SvelteKit dev server. A new browser window will open at http://localhost:3000 . You should see the WunderGraph & SvelteKit example homepage with the JSON result of the Dragons operation.
Configure WunderGraph
WunderGraph lives in the .wundergraph
directory by default. This is where you can configure your WunderGraph application and write your operations.
Let's take a look at the default configuration open .wundergraph/wundergraph.config.ts
.
You can see that we have a single API configured, which is the SpaceX GraphQL API .
The API is introspected and added to the WunderGraph virtual graph, as you can see here:
Operations
Operations are written in the .wundergraph/operations
directory. They can be written in Graphql or TypeScript. Let's check out the Dragons operation, open .wundergraph/operations/Dragons.graphql
.
This simply fetches the name and active status of all the SpaceX dragons.
Calling the operation in SvelteKit
Open src/routes/Dragons.svelte
, there you will find the following code:
The operation name is the name of the file in the operations directory, without the extension. The createQuery
function will return the result of the operation.
Let's modify the Dragons operation and add a limit parameter and return extra fields. Open .wundergraph/operations/Dragons.graphql
and change it to:
The WunderGraph server will automatically pick up on the changes and re-generate the types. Go back to src/routes/Dragons.svelte
and you will see that the Dragons
operation now has a required limit
input parameter.
Refresh the page in your browser, the result will look like this: