···1313all exports of our `@urql/core` core library. This means that if we want to use `urql`'s `Client`
1414imperatively or with Node.js we'd use `@urql/core`'s utilities or the `Client` directly.
15151616-## Getting started
1616+In other words, if we're using framework bindings then writing `import { Client } from "@urql/vue"`
1717+for instance is the same as `import { Client } from "@urql/core"`.
1818+This means that we can use the core utilities and exports that are shared between all bindings
1919+directly or install `@urql/core` separately. We can even use `@urql/core` directly without any
2020+framework bindings.
17211818-Installing `@urql/core` is quick and no other packages are immediately necessary.
2222+## Installation
2323+2424+As we said above, if we are using bindings then those will already have installed `@urql/core` as
2525+they depend on it. They also all re-export all exports from `@urql/core`, so we can use those no
2626+matter which bindings we've installed. However, it's also possible to explicitly install
2727+`@urql/core` or use it standalone, e.g. in a Node.js environment.
19282029```sh
2130yarn add @urql/core graphql
···2332npm install --save @urql/core graphql
2433```
25342626-### One-off Queries and Mutations
2727-2828-When you're using `urql` to send one-off queries or mutations — rather than in full framework code,
2929-where updates are important — it's common to convert the streams that we get to promises. The
3030-`client.query` and `client.mutation` methods have a shortcut to do just that.
3535+Since all bindings and all exchanges depend on `@urql/core`, we may sometimes run into problems
3636+where the package manager installs _two versions_ of `@urql/core`, which is a duplication problem.
3737+This can cause type errors in TypeScript or cause some parts of our application to bundle two
3838+different versions of the package or use slightly different utilities. We can fix this by
3939+deduplicating our dependencies.
31403232-```js
3333-const QUERY = `
3434- query Test($id: ID!) {
3535- getUser(id: $id) {
3636- id
3737- name
3838- }
3939- }
4040-`;
4141-4242-client
4343- .query(QUERY, { id: 'test' })
4444- .toPromise()
4545- .then(result => {
4646- console.log(result); // { data: ... }
4747- });
4141+```sh
4242+npx yarn-deduplicate && yarn
4343+# or
4444+npm dedupe
4845```
49465050-This may be useful when we don't plan on cancelling queries or we don't care about future updates to
5151-this data and are just looking to query a result once.
5252-5353-Similarly there's a way to read data from the cache synchronously, provided that the cache has
5454-received a result for a given query before. The `Client` has a `readQuery` method which is a
5555-shortcut for just that.
5656-5757-```js
5858-const QUERY = `
5959- query Test($id: ID!) {
6060- getUser(id: $id) {
6161- id
6262- name
6363- }
6464- }
6565-`;
6666-6767-const result = client.readQuery(QUERY, { id: 'test' });
6868-6969-result; // null or { data: ... }
7070-```
7171-7272-Since the streams in `urql` operate synchronously, internally this method subscribes to
7373-`client.executeQuery` and unsubscribes immediately. If a result is available in the cache it will be
7474-resolved synchronously prior to the unsubscribe. If not, the query is cancelled and no request will be sent to the GraphQL API.
7575-7676-### gql
4747+## GraphQL Tags
77487849A notable utility function is the `gql` tagged template literal function, which is a drop-in
7950replacement for `graphql-tag`, if you're coming from other GraphQL clients.
80518181-Wherever `urql` accepts a query document, you may either pass a string or a `DocumentNode`. `gql` is
5252+Wherever `urql` accepts a query document, we can either pass a string or a `DocumentNode`. `gql` is
8253a utility that allows a `DocumentNode` to be created directly, and others to be interpolated into
8354it, which is useful for fragments for instance. This function will often also mark GraphQL documents
8455for syntax highlighting in most code editors.
···139110 ${TodoFragment}
140111`;
141112```
113113+114114+This will all look familiar when coming from the `graphql-tag` package. The functionality is
115115+identical and the output is approximately the same. The two packages are also intercompatible.
116116+However, one small change that `@urql/core`'s implementation makes is that your fragment names don't
117117+have to be globally unique, since it's possible to create some one-off fragments every now and then.
118118+It also pre-generates a "hash key" for the `DocumentNode` which is what `urql` does anyway, thus
119119+avoiding some extra work compared to when the `graphql-tag` package is used with `urql`.
120120+121121+## Using the `urql` Client
122122+123123+The `Client` is the main "hub" and store for everything that `urql` does. It is used by all
124124+framework bindings and from the other pages in the "Basics" section we can see that creating a
125125+`Client` comes up across all bindings and use-cases for `urql`.
126126+127127+[Read more about the `Client` and `urql`'s architecture on the "Architecture"
128128+page.](../architecture.md)
129129+130130+### Setting up the `Client`
131131+132132+The `@urql/core` package exports a function called `createClient` which we can use to
133133+create the GraphQL client. This central `Client` manages all of our GraphQL requests and results.
134134+135135+```js
136136+import { createClient } from 'urql';
137137+138138+const client = createClient({
139139+ url: 'http://localhost:3000/graphql',
140140+});
141141+```
142142+143143+At the bare minimum we'll need to pass an API's `url` when we create a `Client` to get started.
144144+145145+Another common option is `fetchOptions`. This option allows us to customize the options that will be
146146+passed to `fetch` when a request is sent to the given API `url`. We may pass in an options object or
147147+a function returning an options object.
148148+149149+In the following example we'll add a token to each `fetch` request that our `Client` sends to our
150150+GraphQL API.
151151+152152+```js
153153+const client = createClient({
154154+ url: 'http://localhost:3000/graphql',
155155+ fetchOptions: () => {
156156+ const token = getToken();
157157+ return {
158158+ headers: { authorization: token ? `Bearer ${token}` : '' },
159159+ };
160160+ },
161161+});
162162+```
163163+164164+### The `Client`s options
165165+166166+As we've seen above, the most important option for the `Client` is `url`, since it won't work
167167+without it. However, another important option on the `Client` is the `exchanges` option.
168168+169169+This option passes a list of exchanges to the `Client`, which tell it how to execute our requests
170170+and how to cache data in a certain order. By default this will be populated with the list of
171171+`defaultExchanges`.
172172+173173+```js
174174+import { createClient, defaultExchanges } from 'urql';
175175+176176+const client = createClient({
177177+ url: 'http://localhost:3000/graphql',
178178+ // the default:
179179+ exchanges: defaultExchanges,
180180+ // the same as:
181181+ exchanges: [dedupExchange, cacheExchange, fetchExchange]
182182+});
183183+```
184184+185185+Later, [in the "Advanced" section](../advanced/README.md) we'll see many more features that `urql`
186186+supports by adding new exchanges to this list. On [the "Architecture" page](../architecture.md)
187187+we'll also learn more about what exchanges are and why they exist.
188188+189189+For now it's sufficient for us to know that our requests are executed using the logic in the
190190+exchanges in order. First, the `dedupExchange` deduplicates requests if we send the same queries
191191+twice, the `cacheExchange` implements the default "document caching" behaviour (as we'll learn about
192192+on the ["Document Caching"](./document-caching.md) page), and lastly the `fetchExchange` is
193193+responsible for sending our requests to our GraphQL API.
194194+195195+### One-off Queries and Mutations
196196+197197+When you're using `urql` to send one-off queries or mutations — rather than in full framework code,
198198+where updates are important — it's common to convert the streams that we get to promises. The
199199+`client.query` and `client.mutation` methods have a shortcut to do just that.
200200+201201+```js
202202+const QUERY = `
203203+ query Test($id: ID!) {
204204+ getUser(id: $id) {
205205+ id
206206+ name
207207+ }
208208+ }
209209+`;
210210+211211+client
212212+ .query(QUERY, { id: 'test' })
213213+ .toPromise()
214214+ .then(result => {
215215+ console.log(result); // { data: ... }
216216+ });
217217+```
218218+219219+In the above example we're executing a query on the client, are passing some variables and are
220220+calling the `toPromise()` method on the return value to execute the request immediately and get the
221221+result as a promise. This may be useful when we don't plan on cancelling queries or we don't
222222+care about future updates to this data and are just looking to query a result once.
223223+224224+The same can be done for mutations by calling the `client.mutation` method instead of the
225225+`client.query` method.
226226+227227+Similarly there's a way to read data from the cache synchronously, provided that the cache has
228228+received a result for a given query before. The `Client` has a `readQuery` method which is a
229229+shortcut for just that.
230230+231231+```js
232232+const QUERY = `
233233+ query Test($id: ID!) {
234234+ getUser(id: $id) {
235235+ id
236236+ name
237237+ }
238238+ }
239239+`;
240240+241241+const result = client.readQuery(QUERY, { id: 'test' });
242242+243243+result; // null or { data: ... }
244244+```
245245+246246+In the above example we call `readQuery` and receive a result immediately. This result will be
247247+`null` if the `cacheExchange` doesn't have any results cached for the given query.
248248+249249+### Subscribing to Results
250250+251251+GraphQL Clients are by their nature "reactive", meaning that when we execute a query, we expect to
252252+get future results for this query. [On the "Document Caching" page](./document-caching.md) we'll
253253+learn how mutations can invalidate results in the cache. This process (and others just like it) can
254254+cause our query to be refetched.
255255+256256+In essence, if we're subscribing to results rather than using a promise, like we've seen above, then
257257+we're able to see future changes for our query's results. If a mutation causes a query to be
258258+refetched from our API in the background then we'll see a new result. If we execute a query
259259+somewhere else then we'll get notified of the new API result as well, as long as we're subscribed.
260260+261261+```js
262262+import { pipe, subscribe } from 'wonka';
263263+264264+const QUERY = `
265265+ query Test($id: ID!) {
266266+ getUser(id: $id) {
267267+ id
268268+ name
269269+ }
270270+ }
271271+`;
272272+273273+const { unsubscribe } = pipe(
274274+ client.query(QUERY, { id: 'test' }),
275275+ subscribe(result => {
276276+ console.log(result); // { data: ... }
277277+ })
278278+);
279279+```
280280+281281+This code example is similar to the one before. However, instead of sending a one-off query, we're
282282+subscribing to the query. Internally, this causes the `Client` to do the exact same, but the
283283+subscription means that our callback may be called repeatedly. We may get future results as well as
284284+the first one.
285285+286286+This also works synchronously. As we've seen before `client.readQuery` can give us a result
287287+immediately if our cache already has a result for the given query. The same principle applies here!
288288+Our callback will be called synchronously if the cache already has a result.
289289+290290+Once we're not interested in any results anymore we need to clean up after ourselves by calling
291291+`unsubscribe`. This stops the subscription and makes sure that the `Client` doesn't actively update
292292+the query anymore or refetches it. We can think of this pattern as being very similar to events or
293293+event hubs.
294294+295295+We're using [the Wonka library for our streams](https://wonka.kitten.sh/basics/background) which
296296+we'll learn more about [on the "Architecture" page](./architecture.md). But we can think of this as
297297+React's effects being called over time, or as `window.addEventListener`.
142298143299## Common Utilities in Core
144300