Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support for Svelte 4 #131

Open
Serator opened this issue Jul 17, 2023 · 2 comments
Open

Feature Request: Support for Svelte 4 #131

Serator opened this issue Jul 17, 2023 · 2 comments

Comments

@Serator
Copy link

Serator commented Jul 17, 2023

Svelte 4 is out now, would love to see it supported. Thanks!

@alexyeskin
Copy link

Svelte 4 was released 3 month ago and this package updated more than 1 year ago...
What if Svelte 4 will never be supported?

@misha-otto
Copy link

misha-otto commented Jul 28, 2023

You can dump this package and use codegen.ts with the following settings.

import type {CodegenConfig} from '@graphql-codegen/cli'

const config: CodegenConfig = {
  schema: './schema.graphql',
  documents: './src/**/*.graphql',
  generates: {
    './src/lib/graphql/generated.ts': {
      plugins: ['typescript', 'typescript-operations', 'graphql-codegen-svelte-apollo'],
      config: {
        clientPath: '../common/helpers/apollo-client',
      },
    },
  },
}

export default config

Then set the Apollo client to the Svelte context at the top level (+layout.svelte)

<script lang="ts">
  setContext(Symbol('client'), apolloClient)
</script>

And use the created queries in your components

<script lang="ts">
  import {TodosSubscription, AddTodo} from '$lib/graphql/generated'

  $: todos = TodosSubscription({})
  let name = ''

  function addTodo() {
    AddTodo({variables: {name}})
      .then(() => (name = ''))
      .catch((error) => {
        console.error(error)
      })
  }
</script>

<div style="text-align:center">
  <h2>Todoer</h2>

  <form on:submit|preventDefault={addTodo}>
    <input placeholder="new todo" bind:value={name} />
    <button type="submit">Submit</button>
  </form>

  {#if !$todos}
    <p>.. loading</p>
  {:else}
    {#each $todos?.data?.todos ?? [] as todo}
      <p class:done={todo.isDone}>{todo.name}</p>
    {/each}
  {/if}
</div>

full repo with my experiments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants