
一个 Nuxt 3/4 模块,可轻松进行 GraphQL 请求,利用 Nuxt 的可组合项来完成令人惊叹的事情。
useAsyncData 构建的可组合项TGqlPulseClientKey 类型)graphql-request (graffle 兼容)useGqlPulseClient(clientName) — 访问原始客户端useGqlPulseRequest(...) — 简单请求useGqlPulseRawRequest(...) — 低级原始响应useGqlPulseRequestWithCache(...) — sessionStorage 缓存 (SPA)useAsyncGqlPulse(...) — 对 SSR 友好的异步数据useAsyncGqlPulseWithCache(...) — 带有 sessionStorage 的 SPA 异步useGqlPulseBatchRequests(...) — 批量处理多个查询useAsyncGqlPulseBatch(...) — 带有可选 payload 缓存的异步批处理安装模块和对等依赖
npm install nuxt-gql-pulse graphql graphql-request
将其添加到您的 nuxt.config.ts:
export default defineNuxtConfig({
modules: ['nuxt-gql-pulse'],
gqlPulse: {
clients: {
rickandmortyapi: {
endpoint: 'https://rickandmortyapi.com/graphql',
},
},
},
})
就是这样!您现在可以在 Nuxt 应用程序中使用 Nuxt GQL Pulse 了 ✨
<script setup lang="ts">
const query = /* GraphQL */ `
query {
characters(page: 1) {
results {
id
name
status
species
}
}
}
`
const { data } = await useAsyncGqlPulse<{
characters: {
results: { id: string; name: string; status: string; species: string }[]
}
}>({
client: 'rickandmortyapi',
document: query,
})
</script>
<template>
<div>
<h2>Rick & Morty Characters</h2>
<ul>
<li v-for="character in data.characters.results" :key="character.id">
{{ character.name }} — {{ character.status }} ({{ character.species }})
</li>
</ul>
</div>
</template>
以下是与相关 Nuxt GraphQL 模块的实际比较(研究摘要)。这显示了功能重叠之处以及 nuxt-gql-pulse 的突出之处。
| 能力 / 模块 | nuxt-gql-pulse | nuxt-graphql-request | nuxt-graphql-client | nuxt-graphql-middleware | @nuxtjs/apollo / URQL |
|---|---|---|---|---|---|
| 多个命名客户端 | ✅ (内置) | ✅ | ✅ | ⚠️ (侧重服务器) | ✅ |
基于 useAsyncData 的可组合项 | ✅ (已就绪) | ✅ (手动使用) | ✅ (代码生成) | ✅ (服务器封装) | ✅ |
| sessionStorage 缓存 (客户端持久化) | ✅ 独特 | ❌ | ❌ | ❌ (仅限内存/payload) | ❌ (使用规范化缓存) |
| Nuxt payload / SSR 水合缓存 | ✅ | ❌ | ⚠️ | ✅ | ✅ |
与 graphql-request / graffle 集成 | ✅ | ✅ | ⚠️ (可能使用不同的客户端/代码生成) | ⚠️ (服务器请求) | ❌ (Apollo/URQL) |
支持原始响应 (rawRequest) | ✅ | ✅ | ⚠️ | ⚠️ | ✅ (客户端特定) |
| 批量请求助手 | ✅ | ⚠️ (手动) | ⚠️ | ⚠️ | ✅ (取决于客户端) |
| Nuxt 3 / 4 支持 | ✅ | ✅ | ✅ | ✅ | ✅ |
| 轻量级 / 最少运行时占用 | ✅ | ✅ | ⚠️ (可能添加代码生成) | ⚠️ | ❌ (Apollo 更大) |
nuxt.config.ts)export default defineNuxtConfig({
modules: ['nuxt-gql-pulse'],
gqlPulse: {
/**
* Define your GraphQL clients
*/
clients: {
// 'rickandmortyapi' was used in examples above instead of 'default'
default: {
/**
* The client endpoint URL
*/
endpoint: 'https://rickandmortyapi.com/graphql',
/**
* Per-client request configuration
* Docs: https://github.com/graffle-js/graffle/blob/graphql-request/examples/configuration-fetch-options.ts
*/
options: {
method?: 'GET' | 'POST';
headers?: Headers | string[][] | Record<string, string> | (() => Headers | string[][] | Record<string, string>);
cache?: 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload';
credentials?: 'include' | 'omit' | 'same-origin';
integrity?: string;
keepalive?: boolean;
mode?: 'same-origin' | 'cors' | 'navigate' | 'no-cors';
priority?: 'auto' | 'high' | 'low';
redirect?: 'error' | 'follow' | 'manual';
referrer?: string;
referrerPolicy?: '' | 'same-origin' | 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
errorPolicy?: 'none' | 'ignore' | 'all';
}
},
secondClient: {
// ... another client config
},
},
/**
* Global options applied to all clients
*/
options: {
// same structure as per-client `options`
},
/**
* Optional
* By default, all composables are auto-imported.
*
* You can exclude some if you only use one or two.
*
* Available auto-import composables:
* [
* 'useGqlPulseRequest',
* 'useGqlPulseRequestWithCache',
* 'useGqlPulseBatchRequests',
* 'useGqlPulseRawRequest',
* 'useAsyncGqlPulse',
* 'useAsyncGqlPulseWithCache',
* 'useAsyncGqlPulseBatch',
* ]
*
* ⚠️ `useGqlPulseClient` is **always** imported by default
* and cannot be excluded.
*/
excludeComposables: [],
},
})
🔑 类型声明 (index.d.ts)
如果您希望在项目中拥有类型安全的客户端名称,您可以在项目中全局声明 TGqlPulseClientKey 类型。通常这会在构建期间在 .nuxt/types/gql-pulse.d.ts 中自动生成,但您也可以在项目中手动声明以获得更好的开发体验。
declare global {
type TGqlPulseClientKey = 'rickandmortyapi'
}
export {}
🔧 API 快速参考 (签名)
🔁 缓存:SSR vs SPA
useGqlPulseRequest
// Make standard GraphQL requests.
const data = await useGqlPulseRequest({
document, // string | DocumentNode
client: string, // defaults to first client
variables: TVariables,
})
useGqlPulseRawRequest
// Low-level request returning headers, status, and errors.
const res = await useGqlPulseRawRequest({
document: string,
client: string,
variables: TVariables,
}) // { status, headers, data, extensions, errors? }
useGqlPulseClient
// Access the underlying GraphQLClient instance.
// Always auto-imported and cannot be excluded.
const client = useGqlPulseClient('default')
useAsyncGqlPulse
// Nuxt-friendly async data fetching with optional payload cache. (SSR Friendly)
const { data, pending, error } = await useAsyncGqlPulse({
key: 'characters',
document,
variables,
client: 'default',
withPayloadCache: true,
})
useAsyncGqlPulseWithCache
// SPA-only sessionStorage caching.
const data = await useAsyncGqlPulseWithCache({
key: 'characters',
document,
variables,
})
useGqlPulseBatchRequests
// Batch multiple queries into one request.
const result = await useGqlPulseBatchRequests({
documents: [
{ document: query1, variables: { id: 1 } },
{ document: query2, variables: { id: 2 } },
],
client: 'default',
})
useAsyncGqlPulseBatch
// Nuxt-friendly async data fetching for batched requests with optional payload cache. (SSR Friendly)
const { data, pending, error } = await useAsyncGqlPulseBatch({
key: 'batch-1',
documents: [
{ document: query1, variables: { id: 1 } },
{ document: query2, variables: { id: 2 } },
],
client: 'default',
withPayloadCache: true,
})
useGqlPulseRequestWithCache
// SPA-only sessionStorage caching for single requests.
const result = await useGqlPulseRequestWithCache({
key: 'character-1',
document,
variables,
})
您还可以通过 Nuxt 插件为客户端提供自定义配置
export default defineNuxtPlugin(() => {
const client = useGqlPulseClient('rickandmortyapi')
const secondClient = useGqlPulseClient('secondClient')
/**
* Request middleware
* Runs before every GraphQL request.
* You can modify headers, body, or any request config here.
*/
const requestMiddleware = async (
request: RequestExtendedInit<Variables>
) => ({
...request,
headers: {
...request.headers,
Authorization: `Bearer token`,
},
})
/**
* Response middleware
* Runs after every GraphQL response.
* Perfect for custom logging or error handling.
*/
const responseMiddleware = async (
response: GraphQLClientResponse<unknown> | Error
) => {
if (response instanceof Error) {
console.error('❌ GraphQL error:', response.message)
} else {
console.log('✅ Response received:', response)
}
}
// Apply middlewares to both clients
for (const c of [client, secondClient]) {
c.requestConfig.requestMiddleware = requestMiddleware
c.requestConfig.responseMiddleware = responseMiddleware
}
/**
* Optional: override other requestConfig options
*/
// client.requestConfig.jsonSerializer = JSON
// client.requestConfig.body = JSON.stringify({ query: "{ characters { name } }" })
// client.requestConfig.signal = AbortSignal.timeout(5000)
// client.requestConfig.window = null
})
您还可以通过不同方式提供认证头
nuxt.config.ts 中设置静态头export default defineNuxtConfig({
gqlPulse: {
clients: {
default: {
endpoint: 'https://default.com/graphql',
options: {
headers: {
authorization: 'Bearer TOKEN_HERE',
},
},
},
},
},
})
plugins/gqlPulse.ts 中使用 requestMiddleware 设置动态头const client = useGqlPulseClient('default')
const requestMiddleware = async (request: RequestExtendedInit<Variables>) => {
const token = await getAuthTokenSomehow()
return {
...request,
headers: {
...request.headers,
authorization: `Bearer ${token}`,
},
}
}
client.requestConfig.requestMiddleware = requestMiddleware
useGqlPulseRequest 选项中设置每个请求的头const data = await useGqlPulseRequest({
document,
client: 'default',
variables,
requestOptions: {
headers: {
authorization: `Bearer ${token}`,
},
},
})
useGqlPulseClient() 中的 setHeaders() 或 setHeader()const client = useGqlPulseClient('rickandmortyapi')
// Replace all existing headers
client.setHeaders({
Authorization: 'Bearer my-secret-token',
'Content-Type': 'application/json',
})
// Set one header without touching the rest
client.setHeader('x-custom-header', 'my-value')
// Point the client to a different GraphQL endpoint
client.setEndpoint('https://custom-api.example.com/graphql')
请参阅示例目录以获取更多使用示例
gqlPulse.clientsclientName.options 反映了 graffle/graphql-request 中可用的请求选项(headers、method、requestMiddleware、responseMiddleware 等)。有关高级选项和中间件,请参阅 graffle 示例。有关高级请求配置和中间件示例,请参阅 graphql-request / graffle。
本项目建立在并整合了 GraphQL 社区的杰出工作
本软件包本身根据 MIT 许可证发布。