authLink Setup?

With this code:

let theWsLinkUrl = `wss://localhost:3000/graphql`
let appApiUrl = `http://localhost:3000/graphql`;
let errorLink = `http://localhost:3000/myPageThatShowsGraphQLerrors`;


const authLinkContext = setContext((_, { headers }) => {
    // return the headers to the context so httpLink can read them
    return {
        headers: {
            ...headers,
            authorization: token || "",
        },
    };
});

const authLink = ApolloLink.from([
    errorLink,
    authLinkContext.concat(httpAuthLink),
]);

…I’m getting an authLink that looks like this:

What am I missing?

Hi @vikr00001 :wave: I recommend taking a closer look at our Apollo Link documentation overall:

Your errorLink variable is a string, where it should be an instance of ApolloLink. Hope that helps point you in the right direction!

1 Like

If I’m creating an authLink, is an errorLink required?

I’m planning to use it here:

export default new ApolloClient({
    link: ApolloLink.split(
        ({ query }) => {
            const definition = getMainDefinition(query);
            return (
                definition.kind === "OperationDefinition" &&
                definition.operation === "subscription"
            );
        },
        wsLink ? wsLink : appLink,
        ApolloLink.split(
            operation => operation.getContext().clientName === "auth",
            authLink, // <= apollo will send to this if clientName is "auth"
            appLink, // <= otherwise will send to this
        ),
    ),
    cache: new InMemoryCache({

    }),
});

That authLink at this point is just a variable name that you assigned to it.

What you have is a link that adds an authorization header, but that’s all the functionality it has - there is no implicit or hidden logic added on top of that.

If you want to handle any kind of errors, you will probably need an ErrorLink.

But again, giving a variable a name doesn’t make something magical happen. You will need to actually use an ErrorLink instance, not just a variable with that name and a string as a content.

Variable names hold no meaning in JavaScript, and Apollo Client has no way of knowing what names you assign to variables. Apollo Client only see the values inside of the variables.
The variable names are just there so you can read the code better, and choosing the names is up to you, but has no runtime consequence.

1 Like