-
Notifications
You must be signed in to change notification settings - Fork 474
Add Solid & SolidStart API documentation section #3855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davedbase
wants to merge
1
commit into
urql-graphql:main
Choose a base branch
from
davedbase:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| --- | ||
| title: '@urql/solid-start' | ||
| order: 5 | ||
| --- | ||
|
|
||
| # SolidStart API | ||
|
|
||
| > **Note:** These API docs are deprecated as we now keep TSDocs in all published packages. | ||
| > You can view TSDocs while using these packages in your editor, as long as it supports the | ||
| > TypeScript Language Server. | ||
| > We're planning to replace these API docs with a separate web app soon. | ||
|
|
||
| The `@urql/solid-start` package integrates urql with SolidStart's native data-fetching primitives | ||
| (`query()`, `action()`, `createAsync()`, `useAction()`). Its API differs from `@urql/solid` because | ||
| queries run on both server and client and mutations are exposed as SolidStart actions. | ||
|
|
||
| > For client-side only SolidJS applications, see the [`@urql/solid` API docs](./solid.md) instead. | ||
|
|
||
| ## createQuery | ||
|
|
||
| Creates a cached query function using SolidStart's `query()` primitive. | ||
|
|
||
| | Argument | Type | Description | | ||
| | ----------- | ------------------------ | -------------------------------------------------------------------------------------------------------- | | ||
| | `query` | `string \| DocumentNode` | The query to be executed. Accepts as a plain string query or GraphQL DocumentNode. | | ||
| | `key` | `string` | A unique cache key for SolidStart's router to use for deduplication and caching. | | ||
| | `options` | `?object` | Optional configuration object (see table below). | | ||
|
|
||
| The `options` object accepts: | ||
|
|
||
| | Prop | Type | Description | | ||
| | --------------- | ------------------------ | -------------------------------------------------------------------------------------------------------- | | ||
| | `variables` | `?object` | Static variables to be used with the GraphQL request. | | ||
| | `requestPolicy` | `?RequestPolicy` | An optional [request policy](./core.md#requestpolicy) that should be used specifying the cache strategy. | | ||
| | `context` | `?object` | Holds the contextual information for the query. | | ||
|
|
||
| This function returns a query function (the result of SolidStart's `query()`) that, when called, | ||
| executes the GraphQL query and returns a `Promise` resolving to an | ||
| [`OperationResult`](./core.md#operationresult). The returned function is intended to be used with | ||
| SolidStart's `createAsync()` to get a reactive result in your component: | ||
|
|
||
| ```jsx | ||
| import { createQuery } from '@urql/solid-start'; | ||
| import { createAsync } from '@solidjs/router'; | ||
| import { gql } from '@urql/core'; | ||
|
|
||
| const TodosQuery = gql` | ||
| query { todos { id title } } | ||
| `; | ||
|
|
||
| const queryTodos = createQuery(TodosQuery, 'todos-list'); | ||
|
|
||
| export default function Todos() { | ||
| const result = createAsync(() => queryTodos()); | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| `createQuery` must be called inside a component (or a reactive context) where it has access to the | ||
| URQL context, as it reads the `Client` and SolidStart `query` function from the `Provider`. | ||
|
|
||
| [Read more about how to use the `createQuery` API on the "Queries" page.](../basics/solid-start.md#queries) | ||
|
|
||
| ## createMutation | ||
|
|
||
| Creates a GraphQL mutation action using SolidStart's `action()` primitive. | ||
|
|
||
| | Argument | Type | Description | | ||
| | ----------- | ------------------------ | ---------------------------------------------------------------------------------- | | ||
| | `mutation` | `string \| DocumentNode` | The mutation to be executed. Accepts as a plain string query or GraphQL DocumentNode. | | ||
| | `key` | `string` | A unique cache key for SolidStart's router. | | ||
|
|
||
| Returns a `CreateMutationAction`, which is a SolidStart `Action` that accepts mutation variables | ||
| and an optional [`Partial<OperationContext>`](./core.md#operationcontext), and resolves to an | ||
| [`OperationResult`](./core.md#operationresult). | ||
|
|
||
| The returned action integrates with SolidStart's `useAction()` and `useSubmission()` primitives for | ||
| reactive state tracking and progressive enhancement: | ||
|
|
||
| ```jsx | ||
| import { createMutation } from '@urql/solid-start'; | ||
| import { useAction, useSubmission } from '@solidjs/router'; | ||
| import { gql } from '@urql/core'; | ||
|
|
||
| const UpdateTodo = gql` | ||
| mutation ($id: ID!, $title: String!) { | ||
| updateTodo(id: $id, title: $title) { id title } | ||
| } | ||
| `; | ||
|
|
||
| function EditTodo() { | ||
| const updateTodoAction = createMutation(UpdateTodo, 'update-todo'); | ||
| const updateTodo = useAction(updateTodoAction); | ||
| const submission = useSubmission(updateTodoAction); | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| `createMutation` must be called inside a component where it has access to the URQL context. | ||
|
|
||
| [Read more about how to use the `createMutation` API on the "Mutations" page.](../basics/solid-start.md#mutations) | ||
|
|
||
| ## createSubscription | ||
|
|
||
| Accepts a single required options object as its first argument with the following properties: | ||
|
|
||
| | Prop | Type | Description | | ||
| | ----------- | ------------------------ | -------------------------------------------------------------------------------- | | ||
| | `query` | `string \| DocumentNode` | The subscription query to be executed. Accepts as a plain string query or GraphQL DocumentNode. | | ||
| | `variables` | `?object` | The variables to be used with the GraphQL request. | | ||
| | `pause` | `?boolean` | A boolean flag instructing execution to be paused. | | ||
| | `context` | `?object` | Holds the contextual information for the subscription. | | ||
|
|
||
| The primitive optionally accepts a second argument, a handler function with a type signature of: | ||
|
|
||
| ```js | ||
| type SubscriptionHandler<T, R> = (previousData: R | undefined, data: T) => R; | ||
| ``` | ||
|
|
||
| This function will be called with the previous data (or `undefined`) and the new data that's | ||
| incoming from a subscription event, and may be used to "reduce" the data over time, altering the | ||
| value of `result.data`. | ||
|
|
||
| This primitive returns a tuple of the shape `[result, executeSubscription]`. | ||
|
|
||
| - The `result` is a reactive store object with the shape of an [`OperationResult`](./core.md#operationresult). | ||
| - The `executeSubscription` function optionally accepts | ||
| [`Partial<OperationContext>`](./core.md#operationcontext) and restarts the current subscription when | ||
| it's called. When `pause` is set to `true` this starts the subscription, overriding the otherwise | ||
| paused state. | ||
|
|
||
| Note that GraphQL subscriptions require a subscription exchange (e.g. `subscriptionExchange`) to be | ||
| configured on the `Client`. | ||
|
|
||
| [Read more about how to use the `createSubscription` API on the "Subscriptions" | ||
| page.](../advanced/subscriptions.md) | ||
|
|
||
| ## Context API | ||
|
|
||
| In `@urql/solid-start`, the context holds the `Client` as well as SolidStart's `query` and `action` | ||
| functions. All three must be provided together via the `Provider` component: | ||
|
|
||
| ```jsx | ||
| import { Router, action, query } from '@solidjs/router'; | ||
| import { FileRoutes } from '@solidjs/start/router'; | ||
| import { Suspense } from 'solid-js'; | ||
| import { createClient, Provider, cacheExchange, fetchExchange } from '@urql/solid-start'; | ||
|
|
||
| const client = createClient({ | ||
| url: 'http://localhost:3000/graphql', | ||
| exchanges: [cacheExchange, fetchExchange], | ||
| }); | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <Router | ||
| root={props => ( | ||
| <Provider value={{ client, query, action }}> | ||
| <Suspense>{props.children}</Suspense> | ||
| </Provider> | ||
| )} | ||
| > | ||
| <FileRoutes /> | ||
| </Router> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| The `Provider` value is an object with the following shape: | ||
|
|
||
| | Prop | Type | Description | | ||
| | -------- | ----------------------- | ------------------------------------------------------------ | | ||
| | `client` | `Client` | The [`Client`](./core.md#client) instance to use. | | ||
| | `query` | `query` (from router) | SolidStart's `query` function from `@solidjs/router`. | | ||
| | `action` | `action` (from router) | SolidStart's `action` function from `@solidjs/router`. | | ||
|
|
||
| ### useClient | ||
|
|
||
| Returns the [`Client`](./core.md#client) from the current URQL context. Throws an error in | ||
| development if no `Provider` wraps the component. | ||
|
|
||
| ### useQuery | ||
|
|
||
| Returns SolidStart's `query` function from the current URQL context. Used internally by | ||
| [`createQuery`](#createquery). | ||
|
|
||
| ### useAction | ||
|
|
||
| Returns SolidStart's `action` function from the current URQL context. Used internally by | ||
| [`createMutation`](#createmutation). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| --- | ||
| title: '@urql/solid' | ||
| order: 4 | ||
| --- | ||
|
|
||
| # Solid API | ||
|
|
||
| > **Note:** These API docs are deprecated as we now keep TSDocs in all published packages. | ||
| > You can view TSDocs while using these packages in your editor, as long as it supports the | ||
| > TypeScript Language Server. | ||
| > We're planning to replace these API docs with a separate web app soon. | ||
|
|
||
| ## createQuery | ||
|
|
||
| Accepts a single required options object as an input with the following properties: | ||
|
|
||
| | Prop | Type | Description | | ||
| | --------------- | ------------------ | -------------------------------------------------------------------------------------------------------- | | ||
| | `query` | `string \| DocumentNode` | The query to be executed. Accepts as a plain string query or GraphQL DocumentNode. | | ||
| | `variables` | `?object` | The variables to be used with the GraphQL request. | | ||
| | `requestPolicy` | `?RequestPolicy` | An optional [request policy](./core.md#requestpolicy) that should be used specifying the cache strategy. | | ||
| | `pause` | `?boolean` | A boolean flag instructing [execution to be paused](../basics/solid.md#pausing-createquery). | | ||
| | `context` | `?object` | Holds the contextual information for the query. | | ||
|
|
||
| This primitive returns a tuple of the shape `[result, reExecuteQuery]`. | ||
|
|
||
| - The `result` is a reactive store object with the shape of an [`OperationResult`](./core.md#operationresult) with | ||
| an added `fetching: boolean` property, indicating whether the query is being fetched. Unlike React hooks, `result` | ||
| is a Solid store that is accessed directly (not as an accessor function). | ||
| - The `reExecuteQuery` function optionally accepts | ||
| [`Partial<OperationContext>`](./core.md#operationcontext) and re-executes the current query when | ||
| it's called. When `pause` is set to `true` this executes the query, overriding the otherwise | ||
| paused primitive. | ||
|
|
||
| The `variables`, `requestPolicy`, `pause`, and `context` options each accept either a static value | ||
| or a Solid accessor function `() => T`. When passed as an accessor, the query will automatically | ||
| re-execute whenever the accessor's value changes. | ||
|
|
||
| If the `Client` has `suspense` mode enabled, `createQuery` will integrate with Solid's Suspense | ||
| boundaries instead of updating the `fetching` flag. | ||
|
|
||
| [Read more about how to use the `createQuery` API on the "Queries" page.](../basics/solid.md#queries) | ||
|
|
||
| ## createMutation | ||
|
|
||
| Accepts a single `query` argument of type `string | DocumentNode` and returns a tuple of the shape | ||
| `[state, executeMutation]`. | ||
|
|
||
| - The `state` is a reactive store object with the shape of an [`OperationResult`](./core.md#operationresult) with | ||
| an added `fetching: boolean` property, indicating whether the mutation is being executed. The | ||
| state updates reactively as the mutation progresses. | ||
| - The `executeMutation` function accepts variables and optionally | ||
| [`Partial<OperationContext>`](./core.md#operationcontext) and starts executing the mutation when | ||
| called. It returns a `Promise` resolving to an [`OperationResult`](./core.md#operationresult). | ||
|
|
||
| Unlike `createQuery`, `createMutation` does not execute automatically. The returned execute function | ||
| must be called with the mutation variables to start the mutation. | ||
|
|
||
| The returned promise will never reject — check `result.error` for a | ||
| [`CombinedError`](./core.md#combinederror) instead. | ||
|
|
||
| [Read more about how to use the `createMutation` API on the "Mutations" | ||
| page.](../basics/solid.md#mutations) | ||
|
|
||
| ## createSubscription | ||
|
|
||
| Accepts a single required options object as its first argument with the following properties: | ||
|
|
||
| | Prop | Type | Description | | ||
| | ----------- | ------------------------ | -------------------------------------------------------------------------------------------------------- | | ||
| | `query` | `string \| DocumentNode` | The subscription query to be executed. Accepts as a plain string query or GraphQL DocumentNode. | | ||
| | `variables` | `?object` | The variables to be used with the GraphQL request. | | ||
| | `pause` | `?boolean` | A boolean flag instructing [execution to be paused](../basics/solid.md#pausing-createquery). | | ||
| | `context` | `?object` | Holds the contextual information for the subscription. | | ||
|
|
||
| The primitive optionally accepts a second argument, a handler function with a type signature of: | ||
|
|
||
| ```js | ||
| type SubscriptionHandler<T, R> = (previousData: R | undefined, data: T) => R; | ||
| ``` | ||
|
|
||
| This function will be called with the previous data (or `undefined`) and the new data that's | ||
| incoming from a subscription event, and may be used to "reduce" the data over time, altering the | ||
| value of `result.data`. | ||
|
|
||
| This primitive returns a tuple of the shape `[result, executeSubscription]`. | ||
|
|
||
| - The `result` is a reactive store object with the shape of an [`OperationResult`](./core.md#operationresult). | ||
| - The `executeSubscription` function optionally accepts | ||
| [`Partial<OperationContext>`](./core.md#operationcontext) and restarts the current subscription when | ||
| it's called. When `pause` is set to `true` this starts the subscription, overriding the otherwise | ||
| paused primitive. | ||
|
|
||
| The `fetching: boolean` property on the `result` will be set to `false` when the server proactively | ||
| ends the subscription. By default, `urql` is unable to start subscriptions since this requires | ||
| some additional setup. | ||
|
|
||
| [Read more about how to use the `createSubscription` API on the "Subscriptions" | ||
| page.](../advanced/subscriptions.md) | ||
|
|
||
| ## Context API | ||
|
|
||
| In `@urql/solid`, the [`Client`](./core.md#client) is passed to all primitives via Solid's Context | ||
| API. The `Client` must be provided using the exported `Provider` component. | ||
|
|
||
| ```jsx | ||
| import { createClient, Provider, cacheExchange, fetchExchange } from '@urql/solid'; | ||
|
|
||
| const client = createClient({ | ||
| url: 'http://localhost:3000/graphql', | ||
| exchanges: [cacheExchange, fetchExchange], | ||
| }); | ||
|
|
||
| const App = () => ( | ||
| <Provider value={client}> | ||
| <YourRoutes /> | ||
| </Provider> | ||
| ); | ||
| ``` | ||
|
|
||
| ### useClient | ||
|
|
||
| `@urql/solid` also exports a `useClient` function, which retrieves the current [`Client`](./core.md#client) | ||
| from Solid's Context. An error is thrown in development if no `Provider` wraps the component. | ||
|
|
||
| ```js | ||
| import { useClient } from '@urql/solid'; | ||
|
|
||
| const MyComponent = () => { | ||
| const client = useClient(); | ||
| // ... | ||
| }; | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is why they were missing, we've deprecated API docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhhhhhhhhh. I didn't realize!