kamilkisiela / loona

🌕 Application State Management done with GraphQL
https://loonajs.com
MIT License
265 stars 14 forks source link

default ng build --prod generating console errors #273

Open agborkowski opened 5 years ago

agborkowski commented 5 years ago

ng build --prod generates

main.568598cebae93c2a7f2b.js:1 Uncaught Error: 
        In order to initialize Apollo Client, you must specify link & cache properties on the config object.
        This is part of the required upgrade when migrating from Apollo Client 1.0 to Apollo Client 2.0.
        For more information, please visit:
          https://www.apollographql.com/docs/react/basics/setup.html
        to help you get started.
ceelian commented 5 years ago

Got the same error in production. Any workaround we can get loona run in angular prod mode?

ceelian commented 5 years ago

Found the source of the problem and a solution:

const uri = '/graphql/';

export function apolloFactory(
  httpLink: HttpLink,
  loonaLink: LoonaLink,
  cache: InMemoryCache
): ApolloClientOptions<any> {
  const link = loonaLink.concat(
    httpLink.create({
      uri: uri
    })
  );

  return {
    link,
    cache
  };
}

@NgModule({
  imports: [
    ApolloModule,
    LoonaModule.forRoot([FooState, BarState])
  ],
  exports: [ApolloModule, HttpLinkModule],
  providers: [
    {
      provide: LOONA_CACHE,
      useValue: new InMemoryCache() // <-- works in prod mode (with aot)
      // useFactory() {  <-- does NOT work with aot (in prod mode)
      //   return new InMemoryCache();
      // }
    },
    {
      provide: APOLLO_OPTIONS,
      useFactory: apolloFactory,
      deps: [HttpLink, LoonaLink, LOONA_CACHE]
    }
  ]
})
export class GraphqlModule {
}