Skip to content

Commit

Permalink
chore: add test of authenticated client
Browse files Browse the repository at this point in the history
  • Loading branch information
Diizzayy committed Nov 13, 2024
1 parent 2617424 commit deac564
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ jobs:

- name: Run Tests
run: pnpm test -- --coverage
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: codecov/codecov-action@v3

Expand Down
5 changes: 3 additions & 2 deletions examples/multi-client/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default defineNuxtConfig({
compatibilityDate: '2024-11-05',

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

runtimeConfig: {
public: {
Expand All @@ -18,7 +18,8 @@ export default defineNuxtConfig({
},
rmorty: 'https://rickandmortyapi.com/graphql',
countries: 'https://countries.trevorblades.com/graphql',
todos: 'https://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query'
todos: 'https://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query',
github: 'https://api.github.com/graphql'
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions examples/multi-client/pages/github/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<template>
<div class="flex flex-col gap-4">
<UCard class="p-4">
<div>
Github Example
</div>

<div class="flex flex-wrap gap-3 items-center">
<UInput
v-model="githubToken"
icon="carbon-logo-github"
placeholder="Your Github Token"
/>

<UButton
:disabled="!githubToken"
@click="setToken"
>
Set Token
</UButton>

<UButton @click="clearToken">
Clear Token
</UButton>
</div>

<div class="mt-4 flex flex-wrap gap-3 items-center">
<UButton
:disabled="!githubToken"
@click="refresh"
>
Load @me
</UButton>
</div>
</UCard>

<UCard class="p-4">
<p v-if="data">
Logged in as {{ data?.viewer?.login }}
</p>
<p v-else>
Not logged in
</p>
</UCard>
</div>
</template>

<script lang="ts" setup>
const githubToken = useState<string | null | undefined>()
const { data, refresh } = await useAsyncGql({
operation: 'viewer',
client: 'github'
})
const setToken = () => useGqlToken(githubToken.value, { client: 'github' })
const clearToken = () => useGqlToken(null, { client: 'github' })
</script>
5 changes: 5 additions & 0 deletions examples/multi-client/queries/github/viewer.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query viewer {
viewer {
login
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
},
"pnpm": {
"overrides": {
"node-fetch": "npm:node-fetch-native@latest"
"node-fetch": "npm:node-fetch-native@latest",
"nuxt-graphql-client": "workspace:*"
}
},
"packageManager": "[email protected]"
Expand Down
59 changes: 9 additions & 50 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/multi-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ describe('test multiple clients', () => {
expect(result).toContain('<p>First Episode: Pilot</p>')
expect(result).toContain('<p>Name: Morty Smith</p>')
})

it('retrieve github user details', async () => {
const token = process.env.GH_TOKEN
const result = await $fetch('/github', {
headers: { Cookie: token ? `gql:github=${token}` : '' }
})

expect(result).toContain('Logged in as github-actions[bot]')
})
})

0 comments on commit deac564

Please sign in to comment.