apollo-link
                                
                                
                                
                                    apollo-link copied to clipboard
                            
                            
                            
                        Support async schema in apollo-link-schema
I'm attempting to use apollo-link-schema along with schema stitching, but this doesn't appear possible since the schema option doesn't support promises. Using graphql purely client-side is a common use case, but the inability to pass in a promise (the creation of a merged schema) prevents me from using this package. :/ Can we add the ability to pass in a promise?
For now, I'm able to do the following:
const clientLink = new ApolloLink(operation => {
  return new Observable(observer => {
    Promise.resolve(createSchema())
      .then(data => {
        return execute(
          data,
          operation.query,
          null,
          operation.getContext(),
          operation.variables,
          operation.operationName,
        )
      })
      .then(data => {
        if (!observer.closed) {
          observer.next(data)
          observer.complete()
        }
      })
      .catch(error => {
        if (!observer.closed) {
          observer.error(error)
        }
      })
  })
})
But, I would much prefer to use apollo-link-schema which has a much cleaner and explicit syntax.
Hi, I made a tiny library called apollo-link-lazy just yesterday.
My motivation was to use dynamic imports:
import ApolloClient from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { lazy } from 'apollo-link-lazy';
const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: lazy(() => import('./link')),
});
As it accepts any async functions, you could probably do like this:
const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: lazy(async () => {
    const schema = await buildSchemaAsync();
    return new SchemaLink({ schema });
  }),
});
                                    
                                    
                                    
                                
cool! Thanks @dai-shi I'll give it a try.