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

Links for @apollo/client #135

Open
mikew opened this issue Sep 12, 2023 · 0 comments
Open

Links for @apollo/client #135

mikew opened this issue Sep 12, 2023 · 0 comments

Comments

@mikew
Copy link

mikew commented Sep 12, 2023

I wanted to use urql, but due to the lack of useLazyQuery, wanted to reach for Apollo. Not sure if I will end up using it, but I needed to write some links for it to work, so here you go.

There's two versions:

ApolloLink

This one uses ApolloLink, which as far as I can tell, is the lowest level of these things.

import { ApolloLink, FetchResult, fromPromise } from '@apollo/client'
import { invoke } from '@tauri-apps/api/tauri'
import { GraphQLError, print } from 'graphql'

const tauriGraphqlApolloLink = new ApolloLink((operation) => {
  return fromPromise(
    invoke<[string, boolean]>('plugin:graphql|graphql', {
      query: print(operation.query),
      variables: operation.variables,
    })
      .then(([responseStr]) => {
        const parsed = JSON.parse(responseStr)

        return {
          data: parsed.data,
          errors: parsed.errors,
        }
      })
      .catch((err) => {
        return {
          errors: [new GraphQLError(String(err))],
          context: operation.getContext(),
        }
      }),
  )
})

export default tauriGraphqlApolloLink

HttpLink

This one uses HttpLink, but replaces fetch with something that just calls invoke from tauri.

I'm sure it's "slower", because we need to call JSON.parse to get an object to pass to tauri-plugin-graphql along with the JSON.parse that surely happens later on in HttpLink.

But, @apollo/client does seem to set the abort signal, and this uses it. So it might have some UX considerations that tauriGraphqlApolloLink doesn't.

import { createHttpLink } from '@apollo/client'
import { invoke } from '@tauri-apps/api/tauri'

const tauriGraphqlHttpLink = createHttpLink({
  fetch: async (_input, init) => {
    const [responseStr, isOk] = await invoke<[string, boolean]>(
      'plugin:graphql|graphql',
      JSON.parse(String(init?.body)),
    )

    init?.signal?.throwIfAborted()

    return new Response(responseStr, {
      status: isOk ? 200 : 400,
    })
  },
})

export default tauriGraphqlHttpLink
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

1 participant