Do you think the Svelte API / pattern of use could be improved? #997
-
I would argue that the use of context to save the Client reference is brittle and potentially confusing due to svelte requiring that you call this code at a very specific place/time in the component code. // component
<script>
import { queryString } from 'query.graphql.js'
const todosQuery = query({ query: queryString })
$: todos = todosQuery({ pause: true });
const execute = () => todosQuery().then();
</script> I was trying to visualise a setup, where perhaps you construct your own client, and then use it to prepare all your queries / mutations etc. So the client exports 3 methods, query, mutate, and subscribe, that you could use similar to the gql tagging function on your .graphql schemas: // includes/urql.config.js
import { SvelteClient } from '@urql/svelte;
const client = new SvelteClient( { constructor_args })
export default client;
export { client.query as q, client.mutate as m, client.subscribe as s } // graphql/queries.js
import { q } from 'urql.config.js'
export const todos = q`
query {
todos {
id
text
complete
}
}`; // component
<script>
import { todos } from 'query.js'
const execute = () => todos.update({ new_args});
</script>
{#if $todos.fetching}
Loading...
{:else if $todos.error}
Oh no! {$todos.error.message}
{:else if !$todos.data}
No data
{:else}
<ul>
{#each $todos.data.todos as todo}
<li>{todo.text}</li>
{/each}
</ul>
{/if}
<button on:click={execute}>Execute</button> |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 29 replies
-
I might add that this has been thought out in the context of a larger sapper app |
Beta Was this translation helpful? Give feedback.
-
We opt not to wrap What you're describing however is already possible. You can actually use The problem is that whenever it changes it loses all its state and currently we've "hacked" around with a small trick. If you instantiate the query first and then use it separately with another call, we're able to actually preserve the state as we want to. This may change though as we think about what the final API should look like. It's pretty likely we'll actually just drop this and "blame" Svelte's (probably intentional) limitations on why our bindings won't be as fully-featured as the ones for React/Preact 😅 |
Beta Was this translation helpful? Give feedback.
-
So to bring this thread to a more satisfying conclusion; I've fast-tracked the development of a new Svelte bindings API in #1016. This API should protect the users more rigorously from misusing the reactivity model that we had before, and should appear much more intuitive. It also irons out several bugs that we found while experimenting with different APIs. This is still not 100% perfect, but it's the closest we can get to an idiomatic Svelte API that also feels right for urql and GraphQL. |
Beta Was this translation helpful? Give feedback.
We opt not to wrap
urql
'sClient
ever because that introduces confusion around what the client is and what its methods currently are. That being said our Svelte bindings are purely experimental and stand-ins as there are (imho) deep problems with Svelte's reactive model, to the point where most code just opts to useawait
, which isn't optimal.What you're describing however is already possible. You can actually use
query
directly without instantiating it first into a separate variable for instance. The reason why this is done currently is precisely because of Svelte's shortcoming in how subscribables are handledThe problem is that whenever it changes it loses all its state and currently …