The Wayback Machine - https://web.archive.org/web/20201031023311/https://github.com/vercel/next.js/issues/16320
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

Multiple Graphql API Routes example #16320

Open
samuelcastro opened this issue Aug 18, 2020 · 2 comments
Open

Multiple Graphql API Routes example #16320

samuelcastro opened this issue Aug 18, 2020 · 2 comments

Comments

@samuelcastro
Copy link

@samuelcastro samuelcastro commented Aug 18, 2020

Feature request

Is your feature request related to a problem? Please describe.

It'd be super helpful to have an example of a multiple api routes setup using graphql, all examples I can see are considering just one single api route, but in a real world we usually will need multiple APIs, specially when working with serverless functions. Maybe there is some sort of limitation around multiples api server with graphql that I'm not aware of.

Additional context

The solution described in this article would be helpful to connect multiple graphql endpoints into one single graphql instance, something like:

// Create First Link
const firstLink = new HttpLink({
  uri: 'https://www.firstsource.com/api',
  headers: yourHeadersHere,
  // other link options...
});

// Create Second Link
const secondLink = new HttpLink({
  uri: 'https://www.secondsource.com/api',
  headers: yourHeadersHere
  // other link options...
});

and then:

function create (initialState) {
  return new ApolloClient({
    connectToDevTools: process.browser,
    ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
    link: ApolloLink.split(
      operation => operation.getContext().clientName === "second", // Routes the query to the proper client
      secondLink,
      firstLink
    ),
    cache: new InMemoryCache().restore(initialState || {})
  })
}

What's your thoughts?

@samuelcastro
Copy link
Author

@samuelcastro samuelcastro commented Aug 18, 2020

Another solution would be: https://github.com/habx/apollo-multi-endpoint-link

new ApolloClient({
    link: ApolloLink.from([
      new MultiAPILink({
          endpoints: {
              housings: 'https://housings.api',
              projects: 'https://projects.api',
              ...
          },
          createHttpLink: () => new HttpLink({ ... }),
        }),
    ])
  })

More info here: https://www.habx.com/tech/micro-graphql-schema

@samuelcastro
Copy link
Author

@samuelcastro samuelcastro commented Aug 18, 2020

Or maybe an example showing up how to integrate Apollo Server Federation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.