realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.8k stars 576 forks source link

Web SDK constructs the Apollo GraphQL `HttpLink` #4944

Open mongodben opened 2 years ago

mongodben commented 2 years ago

Problem

right now when i use the Realm Web SDK with the Apollo GraphQL JS client, i must write the following boilerplate code to construct the HttpLink instance:

const client = new ApolloClient({
    link: new HttpLink({
      uri: process.env.NEXT_PUBLIC_GRAPHQL_API_ENDPOINT,
      // We get the latest access token on each request
      fetch: async (uri, options) => {
        const accessToken = app?.currentUser?.accessToken;
        options.headers.Authorization = `Bearer ${accessToken}`;
        return fetch(uri, options);
      },
    }),
    cache: new InMemoryCache(),
  });

i would like if this boilerplate could be replaced with a built-in member of the realm-web package.

Solution

implement something like User.ApolloHttpLink, so i could replace the above code with something like:

const client = new ApolloClient({
    link: new user.ApolloHttpLink(),
    cache: new InMemoryCache(),
  });

How important is this improvement for you?

I'd like to see it, but have a workaround

kneth commented 2 years ago

@mongodben Thanks for the suggestion.