Skip to content

feat: support graphql subscriptions #239

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
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ on:
paths:
- "docs/**"
- ".github/workflows/docs.yml"
pull_request:
branches:
- main
paths:
- "docs/**"
- ".github/workflows/docs.yml"
workflow_dispatch:

jobs:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- 🚀 [Nuxt 3](https://v3.nuxtjs.org) Support
- Full Typescript Support
- HMR (Hot Module Reload) for GraphQL documents
- Minimal [GraphQL Client](https://github.com/prisma-labs/graphql-request#graphql-request) + [Code Generation](https://www.graphql-code-generator.com/)
- Minimal GraphQL Client + [Code Generation](https://www.graphql-code-generator.com/)

## Preview

Expand Down
3 changes: 1 addition & 2 deletions docs/content/1.getting-started/3.composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ export default defineNuxtPlugin(() => {
for (const gqlError of err.gqlErrors) {
console.error('[nuxt-graphql-client] [GraphQL error]', {
client: err.client,
operation: err.operation,
statusCode: err.statusCode,
operationType: err.operationType,
operationName: err.operationName,
gqlError
})
}
Expand Down
2 changes: 1 addition & 1 deletion docs/content/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ snippet: yarn add nuxt-graphql-client
Nuxt [GraphQL Client]{ .text-primary }

#description
Minimal [GraphQL Client](https://github.com/prisma-labs/graphql-request#graphql-request) + [Code Generation](https://www.graphql-code-generator.com/) for [Nuxt](https://v3.nuxtjs.org) ⚡️
Minimal GraphQL Client + [Code Generation](https://www.graphql-code-generator.com/) for [Nuxt](https://v3.nuxtjs.org) ⚡️

#extra
::list
Expand Down
2 changes: 1 addition & 1 deletion docs/content/4.community/1.credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Special thanks to [@danielroe](https://github.com/danielroe) for helping navigat
<br>

Under the hood:
- [GraphQL Request](https://github.com/prisma-labs/graphql-request#graphql-request)
- [ofetch](https://github.com/unjs/ofetch)
- [GraphQL Code Generator](https://www.graphql-code-generator.com/)
64 changes: 64 additions & 0 deletions examples/subscription/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<template>
<div>
<NuxtExampleLayout repo="diizzayy/nuxt-graphql-client" example="subscription" class="text-start">
<template #name>
<NuxtLink to="https://github.com/Diizzayy/nuxt-graphql-client/tree/refactor/ohmygql/examples/subscription">
subscription
</NuxtLink>
</template>

<div grid grid-cols-2>
<div>
<div>
<p>WS Status: {{ wsStatus }}</p>
</div>

<div v-if="wsStatus === 'connected'">
<p id="connected">
🟩
</p>
</div>

<div>
<input id="todoInput" v-model="todoInput" type="text">
</div>

<NButton @click="createTodo()">
Create Todo
</NButton>
</div>

<div id="todoResponse">
{{ data }}
</div>
</div>
</NuxtExampleLayout>
</div>
</template>

<script lang="ts" setup>
import type { TodoAddedSubscription } from '#gql'

const wsStatus = ref<string>('')
const todoInput = ref(`Random Text ${Math.random().toString()}`)
const data = ref<TodoAddedSubscription['todoAdded'] | null>(null)

onMounted(() => {
const { subscribe, onResult, onError, unsubscribe } = GqlTodoAdded(null, {
on: {
opened: () => (wsStatus.value = 'opened'),
connected: () => (wsStatus.value = 'connected'),
connecting: () => (wsStatus.value = 'connecting'),
closed: () => (wsStatus.value = 'closed')
}
})

onResult((result) => {
data.value = result.data?.todoAdded
})

subscribe()
})

const createTodo = () => GqlCreateTodo({ todo: { text: todoInput.value } })
</script>
9 changes: 9 additions & 0 deletions examples/subscription/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default defineNuxtConfig({
modules: ['@nuxt/ui', 'nuxt-graphql-client'],

runtimeConfig: {
public: {
GQL_HOST: 'https://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query'
}
}
})
16 changes: 16 additions & 0 deletions examples/subscription/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"private": true,
"name": "example-subscription",
"scripts": {
"dev": "nuxt dev",
"build": "nuxt build",
"preview": "nuxt preview",
"generate": "nuxt generate",
"postinstall": "nuxt prepare"
},
"devDependencies": {
"nuxt": "latest",
"@nuxt/ui": "latest",
"nuxt-graphql-client": "latest"
}
}
7 changes: 7 additions & 0 deletions examples/subscription/queries/todos.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query getTodos { todos { id text } }

query getTodo($id: Int!) { todo (id: $id) { id text } }

mutation createTodo($todo: TodoInput!) { createTodo(todo: $todo) { id text } }

subscription todoAdded { todoAdded { id text } }
3 changes: 3 additions & 0 deletions examples/subscription/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"gql",
"graphql",
"graphql-client",
"graphql-request",
"ofetch",
"codegen",
"graphql-code-generator"
],
Expand Down Expand Up @@ -47,13 +47,12 @@
"dependencies": {
"@graphql-codegen/cli": "npm:graphql-codegen-cli-nuxt@latest",
"@graphql-codegen/typescript": "^2.8.3",
"@graphql-codegen/typescript-graphql-request": "^4.5.8",
"@graphql-codegen/typescript-operations": "^2.5.8",
"@nuxt/kit": "latest",
"defu": "^6.1.1",
"graphql": "^16.6.0",
"graphql-request": "^5.0.0",
"knitwork": "^1.0.0",
"ogql": "^0.0.1",
"ohash": "^1.0.0",
"scule": "^1.0.0"
},
Expand All @@ -64,6 +63,7 @@
"@vitest/coverage-c8": "^0.25.3",
"eslint": "^8.29.0",
"nuxt": "latest",
"playwright": "^1.28.1",
"vitest": "^0.25.3"
},
"packageManager": "pnpm@7.18.0"
Expand Down
3 changes: 1 addition & 2 deletions playground/plugins/onError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export default defineNuxtPlugin(() => {
for (const gqlError of err.gqlErrors) {
console.error('[nuxt-graphql-client] [GraphQL error]', {
client: err.client,
operation: err.operation,
statusCode: err.statusCode,
operationType: err.operationType,
operationName: err.operationName,
gqlError
})
}
Expand Down
Loading