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

Not working within Nuxt 3 #435

Open
Lengo46 opened this issue Jun 23, 2023 · 1 comment
Open

Not working within Nuxt 3 #435

Lengo46 opened this issue Jun 23, 2023 · 1 comment

Comments

@Lengo46
Copy link

Lengo46 commented Jun 23, 2023

For the last week I've trying to make this library work within Nuxt 3. I can't seem to access the multple endpoints

nuxt.config.ts

   ...
  apollo: {
    autoImports: true,
    authType: "XXXXXX",
    authHeader: "Authorization",
    tokenStorage: "cookie",
    proxyCookies: true,
    clients: {
      default: {
        httpEndpoint: "localhost", //placeholder will be replaced at runtime by apollo plugin
        tokenName: "headerAuth",
        httpLinkOptions: {
          credentials: "same-origin",
        },
      },
    },
})

apollo.ts

import { provideApolloClient } from "@vue/apollo-composable"
import { MultiAPILink } from "@habx/apollo-multi-endpoint-link"

export default defineNuxtPlugin((nuxtApp) => {
  const config = useRuntimeConfig()
  let client = new ApolloClient({
    cache: new InMemoryCache(),
    link: ApolloLink.from([
      new MultiAPILink({
        endpoints: {
          exchange: config.public.api,
          trade: config.public.apiTwo,
        },
        createHttpLink: () => createHttpLink(),
        httpSuffix: "",
        getContext: (endpoint) => {
          if (endpoint === "exchange") {
            return {
              headers: {
                Authorization: `Bearer ${config.public.token}`,
              },
            }
          }
          return {
            headers: {
              Authorization: `Bearer ${config.public.token}`,
            },
          }
        },
      }),
    ]),
  })
  provideApolloClient(client)
})

Query

    const query = gql`
      query services {
        services {
          ... on Service {
            name
            identifier {
              externalID
            }
            serviceType {
              name
              identifier {
                externalID
              }
            }
          }
        }
      }
    `

    const { data, error } = await useAsyncQuery(query)

    if (data.value) (this.data = data), (this.loading = false)
    if (error.value) (this.error = error.value), console.log("error: " + error?.value)

    if (data.value) {
      console.log("Exchange: Services")
      console.log(data.value)
    }
  }

It keeps calling the wrong endpoint. I've also tried useQuery. Any suggestions?

@Lengo46
Copy link
Author

Lengo46 commented Jun 26, 2023

```
const link = ApolloLink.from([
  new MultiAPILink({
    endpoints: {
      exchange: config.public.api,
      trade: config.public.apiTwo,
    },
    createHttpLink: () => createHttpLink(),
    httpSuffix: "",
    defaultEndpoint: "exchange",
    getContext: (endpoint) => {
      if (endpoint === "exchange") {
        return {
          headers: {
            Authorization: `Bearer ${config.public.token}`,
          },
        }
      }
      return {
        headers: {
          Authorization: `Bearer ${config.public.tokenTwo}`,
        },
      }
    },
  }),
])

client = $apollo.defaultClient.setLink(link)
provideApolloClient(client)

This solves part of the problem, only the 2nd endpoint doesn't get overwritten and it default to the default endpoint localhost in this case

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