Skip to content

Commit deac564

Browse files
committed
chore: add test of authenticated client
1 parent 2617424 commit deac564

File tree

7 files changed

+89
-53
lines changed

7 files changed

+89
-53
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ jobs:
105105

106106
- name: Run Tests
107107
run: pnpm test -- --coverage
108+
env:
109+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
108110

109111
- uses: codecov/codecov-action@v3
110112

examples/multi-client/nuxt.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default defineNuxtConfig({
22
compatibilityDate: '2024-11-05',
33

4-
modules: ['nuxt-graphql-client'],
4+
modules: ['@nuxt/ui', 'nuxt-graphql-client'],
55

66
runtimeConfig: {
77
public: {
@@ -18,7 +18,8 @@ export default defineNuxtConfig({
1818
},
1919
rmorty: 'https://rickandmortyapi.com/graphql',
2020
countries: 'https://countries.trevorblades.com/graphql',
21-
todos: 'https://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query'
21+
todos: 'https://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query',
22+
github: 'https://api.github.com/graphql'
2223
}
2324
}
2425
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<template>
2+
<div class="flex flex-col gap-4">
3+
<UCard class="p-4">
4+
<div>
5+
Github Example
6+
</div>
7+
8+
<div class="flex flex-wrap gap-3 items-center">
9+
<UInput
10+
v-model="githubToken"
11+
icon="carbon-logo-github"
12+
placeholder="Your Github Token"
13+
/>
14+
15+
<UButton
16+
:disabled="!githubToken"
17+
@click="setToken"
18+
>
19+
Set Token
20+
</UButton>
21+
22+
<UButton @click="clearToken">
23+
Clear Token
24+
</UButton>
25+
</div>
26+
27+
<div class="mt-4 flex flex-wrap gap-3 items-center">
28+
<UButton
29+
:disabled="!githubToken"
30+
@click="refresh"
31+
>
32+
Load @me
33+
</UButton>
34+
</div>
35+
</UCard>
36+
37+
<UCard class="p-4">
38+
<p v-if="data">
39+
Logged in as {{ data?.viewer?.login }}
40+
</p>
41+
<p v-else>
42+
Not logged in
43+
</p>
44+
</UCard>
45+
</div>
46+
</template>
47+
48+
<script lang="ts" setup>
49+
const githubToken = useState<string | null | undefined>()
50+
51+
const { data, refresh } = await useAsyncGql({
52+
operation: 'viewer',
53+
client: 'github'
54+
})
55+
56+
const setToken = () => useGqlToken(githubToken.value, { client: 'github' })
57+
58+
const clearToken = () => useGqlToken(null, { client: 'github' })
59+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query viewer {
2+
viewer {
3+
login
4+
}
5+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
},
7878
"pnpm": {
7979
"overrides": {
80-
"node-fetch": "npm:node-fetch-native@latest"
80+
"node-fetch": "npm:node-fetch-native@latest",
81+
"nuxt-graphql-client": "workspace:*"
8182
}
8283
},
8384
"packageManager": "[email protected]"

pnpm-lock.yaml

Lines changed: 9 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/multi-client.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ describe('test multiple clients', () => {
3333
expect(result).toContain('<p>First Episode: Pilot</p>')
3434
expect(result).toContain('<p>Name: Morty Smith</p>')
3535
})
36+
37+
it('retrieve github user details', async () => {
38+
const token = process.env.GH_TOKEN
39+
const result = await $fetch('/github', {
40+
headers: { Cookie: token ? `gql:github=${token}` : '' }
41+
})
42+
43+
expect(result).toContain('Logged in as github-actions[bot]')
44+
})
3645
})

0 commit comments

Comments
 (0)