Apollo-client: Is it possible to use apollo-client through a CDN?

Created on 18 Mar 2019  ·  9Comments  ·  Source: apollographql/apollo-client

To me it seems like a very basic thing to do but I'm not seeing any documentation or obvious way to do it. I don't see any file in jsDelivr or UNPKG that is designed to be included this way. I tried bundle.umd.js but it produces various errors for the different libraries (apollo-client, apollo-link, apollo-link-http, apollo-cache-inmemory, ...). Could someone provide a working example of this?

idea ⁉️ question 💬 discussion 📦 bundle size 🧞‍♂️ enhancement

Most helpful comment

are there any alternatives or workaround to use this via CDN ?

All 9 comments

What errors does this url: https://unpkg.com/[email protected]/bundle.umd.js provide for you?

If you can give some tips on this I might be able to fix it

Simply adding
<script src="https://unpkg.com/[email protected]/bundle.umd.js"></script>
produces
Uncaught TypeError: Cannot read property 'utilities' of undefined at bundle.umd.js:4 at bundle.umd.js:5
This can be tested in JSFiddle.
I tried importing apollo-utilities through a CDN too, but the error kept appearing.
<script src="https://unpkg.com/[email protected]/lib/bundle.umd.js"></script>
Then I saw that in apollo-client the reference to utilities is
global.apollo.utilities
While apollo-utilities registers itself as
global['apollo.utilities']
So I managed to fix this issue by editing the bundle file manually, but then more errors piled up and I soon gave up.

From my understanding it is not actually possible to achieve this.

The apollo bundle depends on graphql-js which has a similar issue to this one.
https://github.com/graphql/graphql-js/issues/1639

One of the answers there is:

We distribute GraphQL as CJS/MJS package on NPM.
This library is huge (ATM. ~700kb and growing with each release) but highly modular so we expect that frontend apps will use only necessary parts. That's why we don't release it as a single file bundle and expect developers to use it together with bundlers.

A possible solution for my case would be to bundle apollo+graphql as a single file and host it on a cdn. That way I will be able to reuse it across multiple projects.

Timely! I was hoping to do this yesterday, too... «sigh»

Although I/we have some exciting ideas for better ways to deliver the Apollo Client libraries (from a CDN, separate from the main application JS bundle, with better caching), I'm afraid @MartinManev's comment is exactly correct for the time being. In general, only libraries that have no external dependencies can be (easily) loaded as UMD bundles from unpkg.com.

are there any alternatives or workaround to use this via CDN .

are there any alternatives or workaround to use this via CDN ?

are there any alternatives or workaround to use this via CDN ?

Yes here's the example from Akryum https://jsfiddle.net/Akryum/oyejk2qL/

Was this page helpful?
0 / 5 - 0 ratings