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

Sveltekit + Urql + SSR #614

Closed
kamalkech opened this issue Mar 23, 2021 · 17 comments
Closed

Sveltekit + Urql + SSR #614

kamalkech opened this issue Mar 23, 2021 · 17 comments

Comments

@kamalkech
Copy link

I can get data just in client side,
but for SSR data empty and still in step : fetching

any idea how solve this probleme ?

Tnx

@Conduitry
Copy link
Member

Please use one of the issue templates and please provide a reproduction.

@benmccann
Copy link
Member

This type of question is probably better asked on Discord or StackOverflow

@kamalkech
Copy link
Author

@benmccann already asked in discord by no result, so why you close it hhh

@babichjacob
Copy link
Member

We couldn't help you in the Discord server, or here, because we haven't been provided any of your code to look at. The best I was able to do was point you to solving the only urql + SvelteKit error I knew of:

put

  optimizeDeps: {
       exclude: ["@urql/svelte"]
   }

in your vite config and move @urql/svelte from devDependency to dependency

@kamalkech
Copy link
Author

@babichjacob i already do that, and i get data and no in SSR mode

@babichjacob
Copy link
Member

@babichjacob i already do that, and i get data and no in SSR mode

We still don't know what code you wrote.

@kamalkech
Copy link
Author

kamalkech commented Mar 24, 2021

yesterday i already send to you capture of my code, i try to youse load function in context=module but still probleme, any way plz check this captures :

1
2
3
4

@babichjacob
Copy link
Member

There is nothing in your code that would make server side page rendering wait until the query is resolved. Do work like this in load per SvelteKit's docs.

@benmccann
Copy link
Member

This is a hard way for us to reproduce because we would need to type the code from the screenshot and their may be other code that's important elsewhere in the application that's not included. A repo that reproduces this would be a lot more helpful

@kamalkech
Copy link
Author

@babichjacob with this i get error:
500
Function called outside component initialization

Error: Function called outside component initialization

<script context="module">
  import { initClient, operationStore, query } from '@urql/svelte';

  export async function load({ page, fetch, session, context }) {
    initClient({
      url: 'http://localhost:4000/graphql',
    });

    const articles = operationStore(`
      query {
        articles {
          _id
          title
          article_categories {
            _id
            name
          }
        }
      }
    `);

    query(articles);

    articles.subscribe((d) => console.log(`d`, d));

    return {
      props: {
        articles,
      },
    };
  }
</script>

<script lang="ts">
  export let articles;
</script>

<h2>Graphql</h2>

{#if $articles.fetching}
  <p>Loading...</p>
{:else if $articles.error}
  <p>Oh no... {$articles.error.message}</p>
{:else}
  <ul>
    {#each $articles.data.articles as article}
      <li>
        {article.title}
        <br />
        Categories:
        {#each article.article_categories as category}
          {category.name},
        {/each}
      </li>
      <hr />
    {/each}
  </ul>
{/if}

@kamalkech
Copy link
Author

@benmccann ok understood

@babichjacob
Copy link
Member

babichjacob commented Mar 24, 2021

@babichjacob with this i get error:
500
Function called outside component initialization

Error: Function called outside component initialization

<script context="module">
  import { initClient, operationStore, query } from '@urql/svelte';

  export async function load({ page, fetch, session, context }) {
    initClient({
      url: 'http://localhost:4000/graphql',
    });

    const articles = operationStore(`
      query {
        articles {
          _id
          title
          article_categories {
            _id
            name
          }
        }
      }
    `);

    query(articles);

    articles.subscribe((d) => console.log(`d`, d));

    return {
      props: {
        articles,
      },
    };
  }
</script>

<script lang="ts">
  export let articles;
</script>

<h2>Graphql</h2>

{#if $articles.fetching}
  <p>Loading...</p>
{:else if $articles.error}
  <p>Oh no... {$articles.error.message}</p>
{:else}
  <ul>
    {#each $articles.data.articles as article}
      <li>
        {article.title}
        <br />
        Categories:
        {#each article.article_categories as category}
          {category.name},
        {/each}
      </li>
      <hr />
    {/each}
  </ul>
{/if}

Use createClient and store the value in a client variable instead. You can't use initClient because it implicitly makes a setContext call.

@kamalkech
Copy link
Author

@babichjacob still get empty data in SSR using createClient, can you plz share link or same documentation about this topic, cuz i won't to broth u, tnx bro

@babichjacob
Copy link
Member

@babichjacob still get empty data in SSR using createClient, can you plz share link or same documentation about this topic, cuz i won't to broth u, tnx bro

I don't know whether or not there is any, nor do I know how to use urql. I only picked this up from previous troubleshooting. (Non-sarcastically) good luck!

@kamalkech
Copy link
Author

@babichjacob tnx good luck u too

@kamalkech
Copy link
Author

I found solution, it s simple for SSR mode must to add fetch to createClient function:

export async function load({ page, fetch, session, context }) {
    const client = createClient({
      url: 'http://localhost:4000/graphql',
      fetch,
      exchanges: [
        fetchExchange
      ]
    });
    const articles = await client.query(QUERY).toPromise();

    return {
      props: {
        articles: articles,
      },
    };
  }

@frederikhors
Copy link
Contributor

@babichjacob why cannot I use initClient?

Reproduction here: urql-graphql/urql#1660

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

5 participants