gbicou / nuxt-urql Goto Github PK
View Code? Open in Web Editor NEWNuxt URQL module
Home Page: https://nuxt-urql-docs.vercel.app
License: MIT License
Nuxt URQL module
Home Page: https://nuxt-urql-docs.vercel.app
License: MIT License
I have a use case where I want to abstract out some of my GraphQL logic and put them into separate Typescript files. In these files, I don't have access to setup
or any of the Vue's lifecycle hook. If possible, I want a way to access the client that was created in the nuxt.config.ts
file and use it outside of the Nuxt components.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates are pending. To force PRs open, click the checkbox below.
@nuxt/kit
, @nuxt/schema
, nuxt
)These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/ci.yml
actions/checkout v4
pnpm/action-setup v2
actions/setup-node v4
actions/checkout v4
pnpm/action-setup v2
actions/setup-node v4
github/codeql-action v2
actions/checkout v4
pnpm/action-setup v2
actions/setup-node v4
docs/package.json
@nuxt-themes/docus ^1.15.0
nuxt ^3.8.1
vue ^3.3.8
vue-router ^4.2.5
package.json
@nuxt/kit ^3.8.1
@urql/core ^4.2.0
@urql/vue ^1.1.2
@bicou/countries-server-schema ^1.4.0
@bicou/semantic-release ^1.0.1
@graphql-eslint/eslint-plugin ^3.20.1
@nuxt/eslint-config ^0.2.0
@nuxt/module-builder ^0.5.4
@nuxt/schema ^3.8.1
@nuxt/test-utils ^3.8.1
@types/node ^20.9.2
@urql/exchange-execute ^2.2.2
defu ^6.1.3
eslint ^8.54.0
eslint-config-prettier ^9.0.0
eslint-plugin-jsdoc ^46.9.0
eslint-plugin-prettier ^5.0.1
graphql ^16.8.1
nuxt ^3.8.1
prettier ^3.1.0
semantic-release ^22.0.8
vitest ^0.34.6
defu ^6.1.2
graphql ^16.6.0
vue ^3.2.47
pnpm 8.12.0
playground/package.json
@urql/exchange-graphcache ^6.3.3
graphql ^16.8.1
@graphql-codegen/cli ^5.0.0
@graphql-codegen/near-operation-file-preset ^3.0.0
@graphql-codegen/schema-ast ^4.0.0
@graphql-codegen/typed-document-node ^5.0.1
@graphql-codegen/typescript ^4.0.1
@graphql-codegen/typescript-operations ^4.0.1
@graphql-codegen/typescript-urql ^4.0.0
@graphql-codegen/typescript-urql-graphcache ^3.0.0
@graphql-codegen/urql-introspection ^3.0.0
@graphql-typed-document-node/core ^3.2.0
@picocss/pico ^1.5.10
@types/node ^20.9.2
sass ^1.69.5
nuxt ^3.8.1
ts-node ^10.9.1
typescript ^5.3.2
vue ^3.3.8
vue-router ^4.2.5
.nvmrc
node 20.10.0
How can I access the client directly? I want to do something like this:
client
.query('{ test }', {
/* vars */
})
.toPromise()
.then(result => {
console.log(result); // OperationResult
});
In regular Urql, I would create a client, then export the client to be used outside of Vue components. How can I do this using Nuxt Urql? From my understanding is that Nuxt Urql already creates the client in the Nuxt config file, but is there a way to access that client somehow without defining a new one?
without providing a urql.config.ts, only a url and defaults exchanges
Hi I have sucessfully setup urql in nuxt3 with a customized urql.config.ts like so:
import { fetchExchange, subscriptionExchange } from '@urql/core'
import { cacheExchange } from '@urql/exchange-graphcache'
import { defineUrqlClient } from '#urql/client'
import { createClient as createWSClient } from 'graphql-ws'
export default defineUrqlClient((ssrExchange) => {
const setUpServer = () => {
return [ssrExchange, fetchExchange]
}
const setUpClient = () => {
const wsClient = createWSClient({
url: `wss://${location.hostname}/api/graphql`
})
return [
cacheExchange(),
ssrExchange,
fetchExchange,
subscriptionExchange({
forwardSubscription(request) {
const input = { ...request, query: request.query || '' }
return {
subscribe(sink) {
const unsubscribe = wsClient.subscribe(input, sink)
return { unsubscribe }
}
}
}
})
]
}
const exchanges = process.server ? setUpServer() : setUpClient()
const headers = useRequestHeaders(['cookie', 'authorization'])
return {
exchanges,
fetchOptions: () => ({ headers })
}
})
subscriptions ar working fine and I am change local state of users = ref([])
onDeleteAccount like so:
await useSubscription(
{
query: gql`
subscription onDeleteAccount {
user: accountDeleted {
id
}
}
`
},
(_, { user }) => {
users.value = users.value.filter((u) => u.id !== user.id)
}
)
now the record still exists in cache and reappears on client side navigation. So I added a handler directly in cacheExchange() like:
const cache = cacheExchange({
updates: {
Subscription: {
accountDeleted: (result, args, cache, info) => {
const { accountDeleted } = result;
if (accountDeleted) {
cache.invalidate({ __typename: 'Account', id: accountDeleted.id });
}
}
}
}
});
which perfectly removes that cache entry but now user
in the local subscription handler is undefined.
Any hints or thoughts on that?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.