28 |
29 |
30 | ```
31 |
32 | ## How it works.
33 |
34 | `svql` uses a [fetchql]() singleton to talk to GraphQL. You can configure it through the `setupClient()` method.
35 |
36 | Both `query` and `mutation` helpers will take the GQL and return a promise (or function that returns a promise, respectively).
37 |
38 | ### `query(gql[, data[, callback]]): Promise`
39 |
40 | > Queries are indexed so you can refer to them as `from={MY_GQL_QUERY}`. `data` is optional, as is the `callback` function. Any truthy value returned by this callback will be used in-place of the regular response.
41 |
42 | Accessing those values can be done through `` components as shown above, or by watching the returned promises:
43 |
44 | ```html
45 |
49 |
50 | ```
51 |
52 | Refetching of queries can be done through reactive statements:
53 |
54 | ```html
55 |
60 | ```
61 |
62 | Each time `name` changes, the query re-executes.
63 |
64 | ### `mutation(gql[, callback]): Function`
65 |
66 | > The callback will receive a `commit` function that accepts variables-input as first argument, and optionally a second function to handle the response. Values returned by this function are also promises.
67 |
68 | Mutations are functions that could result in more work, so you need to be sure and `commit` once you're ready for the actual request:
69 |
70 | ```html
71 |
83 |
Email:
84 |
Password:
85 |
86 | ```
87 |
88 | Since `mutation()` returns a function, there's no need to setup reactive statements to _refetch_ it. Just calling the generated function is enough.
89 |
90 | ## Components
91 |
92 | You can access `svql` stores as `conn` and `state` respectively. However, it is better to use the following components to handle state. :sunglasses:
93 |
94 | ### ``
95 |
96 | No longer shipped, use a separate `Failure` component from [smoo](https://github.com/pateketrueke/smoo).
97 |
98 | ### ``
99 |
100 | This takes a `from={promise}` value, then renders its progress, catches the failure, etc.
101 |
102 | Available props:
103 |
104 | - `{from}` — Promise-like value to handle status changes
105 | - `{label}` — Label used for `{:catch error}` handling with ``
106 | - `{fixed}` — Setup `` container as fixed, positioned at `left:0;bottom:0` by default
107 | - `{pending}` — Message while the promise is being resolved...
108 | - `{otherwise}` — Message while once promise has resolved successfully
109 |
110 | > With `fixed` you can provide offsets, e.g. ``
111 |
112 | Available slots:
113 |
114 | - `pending` — Replace the `{:await}` block, default is an ``
115 | - `otherwise` — Replace the `{:then}` block, default is an ``; it receives `let:result`
116 | - `exception` — Replace the `{:catch}` block, default is ``; it receives `let:error`
117 |
118 | ### ``
119 |
120 | Use this component to access data `from={promise}` inside, or `from={GQL}` to extract it from resolved state.
121 |
122 | Available props:
123 |
124 | - `{nostatus}` — Boolean; its presence disables the `` render
125 | - `{loading}` — Message while the promise is being resolved...
126 | - `{...}` — Same props from ``
127 | - `let:data` — Unbound `data` inside
128 |
129 | Available slots:
130 |
131 | - `status` — Replaces the `` render with custom markup; it receives the same props as ``
132 | - `loading` — Replace the `{:then}` block, default is an ``; it receives `let:result`
133 | - `failure` — Replace the `{:catch}` block, default is ``; it receives `let:error`
134 |
135 | ### ``
136 |
137 | No longer shipped, use a separate `Fence` component from [smoo](https://github.com/pateketrueke/smoo).
138 |
139 | > Loading states should be bound as `...` to properly block the UI.
140 |
141 | ## Public API
142 |
143 | - `setupClient(options[, key])` — Configure a `FetchQL` singleton with the given `options`, `key` is used for session loading
144 | - `useClient(options[, key])` — Returns a `FetchQL` instance with the given `options`, `key` is used for session loading
145 | - `useToken(value[, key])` — Update the session-token used for Bearer authentication, `key` is used for session loading
146 | - `saveSession(data[, key])` — Serializes any given value as the current session, it MUST be a plain object or null
147 | - `read(gql|key)` — Retrieve current value from `state` by key, a shorthand for `$state[key]` values
148 | - `key(gql)` — Returns a valid `key` from GQL-strings, otherwise the same value is returned
149 | - `$state` — Store with all resolved state by the `fetchql` singleton
150 | - `$conn` — Store with connection details during `fetchql` requests
151 |
152 | > `sqvl` use **Bearer authentication** by default, so any token found in the session will be sent forth-and-back.
153 |
154 | If you want to change your client's authorization token, you may call `client.setToken()` — or `useToken()` globally.
155 |
--------------------------------------------------------------------------------