Differences between new HttpLink and createHttpLink from package apollo-link-http
Asked Answered
W

1

13

In tutorial

https://www.howtographql.com/vue-apollo/1-getting-started/

there is presented new HttpLink syntax, but in official docs

https://www.apollographql.com/docs/link/links/http/

function createHttpLink is applied.

None of these two sources describes the differences between these methods.

Woodhouse answered 19/6, 2019 at 8:19 Comment(0)
L
17

There is no fundamental difference between the two.

If you look at the apollo-link-http package source here, you can see that the exported createHttpLink method returns a new instance of the ApolloLink class initialized with the options you passed to createHttpLink (lines 62-194).

At the end of the same file, you can see that the package also exports the HttpLink class, which extends the ApolloLink class (lines 256-261):

export class HttpLink extends ApolloLink {
  public requester: RequestHandler;
  constructor(opts?: HttpLink.Options) {
    super(createHttpLink(opts).request);
  }
}

As you can see from the code above, when you create an apollo http link by creating a new instance of the HttpLink class, the options you pass to the constructor are internally passed on to createHttpLink, which returns an instance of ApolloLink as mentioned above, and that instance's RequestHandler is passed on to (i.e. copied) to the new HttpLink instance's parent, which is also an instance of ApolloLink (see lines 96-124 here for a peek at ApolloLink's own constructor).

Note that the apollo-link-http package's own docs do NOT mention the new HttpLink syntax, so I would stick to the createHttpLink syntax for future compatibility.

Leia answered 19/6, 2019 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.