Skip to content

Commit

Permalink
feat: graphql请求封装
Browse files Browse the repository at this point in the history
  • Loading branch information
Azhicc committed Jan 4, 2025
1 parent 907456d commit e077416
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 7 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@
"@dcloudio/uni-mp-xhs": "3.0.0-4020920240930001",
"@dcloudio/uni-quickapp-webview": "3.0.0-4020920240930001",
"dayjs": "1.11.10",
"graphql": "^16.10.0",
"graphql-tag": "^2.12.6",
"pinia": "2.0.36",
"pinia-plugin-persistedstate": "3.2.1",
"qs": "6.5.3",
"villus": "^3.5.1",
"vue": "3.4.21",
"wot-design-uni": "^1.4.0",
"z-paging": "^2.8.4"
Expand Down Expand Up @@ -166,4 +169,4 @@
"vite-plugin-restart": "^0.4.2",
"vue-tsc": "^1.8.27"
}
}
}
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

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

23 changes: 23 additions & 0 deletions src/graphql/graphql-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// graphql-user.ts

import gql from 'graphql-tag'

export const refreshToken = gql`
mutation refreshToken($token: JWT!) {
refreshToken(token: $token) {
accessToken
refreshToken
}
}
`

export const Login = gql`
query Login($input: JsCodeReq!) {
Login(input: $input) {
code
jwt
}
}
`

// 更多...
10 changes: 10 additions & 0 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as user from './graphql-user'
// ...

/* how to use
import GQL from "@/graphql"
const curGQL = GQL.home.listAsOwner
**/
export default {
user,
}
35 changes: 35 additions & 0 deletions src/graphql/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createClient, fetch } from 'villus'

type Methods = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'

// 此处重写fetch,请求采用UniAPP提供的uni.request
const fetchPlugin = fetch({
fetch(url, options) {
return new Promise((resolve, reject) => {
uni.request({
url: url.toString(),
method: options?.method as Methods,
data: options?.body as any,
header: options?.headers,

success(res) {
resolve({
ok: true,
status: res.statusCode,
headers: res.header,
text: async () => JSON.stringify(res.data),
json: async () => res.data,
} as Response)
},
fail(e) {
reject(e)
},
})
})
},
})

export const apolloClient = createClient({
url: 'http://it9i.com:10007/gql',
use: [fetchPlugin],
})
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import store from './store'
import { routeInterceptor, requestInterceptor, prototypeInterceptor } from './interceptors'
import 'virtual:uno.css'
import '@/style/index.scss'
import { apolloClient } from './graphql/setup'

export function createApp() {
const app = createSSRApp(App)
app.use(store)
app.use(apolloClient)
app.use(routeInterceptor)
app.use(requestInterceptor)
app.use(prototypeInterceptor)
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "unibest",
"name": "crm-capp",
"appid": "H57F2ACE4",
"description": "",
"versionName": "1.0.0",
Expand Down Expand Up @@ -108,4 +108,4 @@
"base": "/unibest/"
}
}
}
}
10 changes: 9 additions & 1 deletion src/pages/index/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@
</view>
</template>

<script lang="ts" setup>
<script lang="js" setup>
import PLATFORM from '@/utils/platform'
import { computed } from 'vue'
import { useQuery } from 'villus'
import GQL from '@/graphql'

const { data, execute } = useQuery({ query: GQL.user.Login })
console.log('data', data)
console.log('execute', execute)
const count = computed(() => data.value?.me.countAsFriend ?? '--')
console.log('count', count)
defineOptions({
name: 'Home',
})
Expand Down
6 changes: 3 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ export const getEnvBaseUrl = () => {

switch (envVersion) {
case 'develop':
baseUrl = 'https://ukw0y1.laf.run'
baseUrl = 'http://it9i.com:10007'
break
case 'trial':
baseUrl = 'https://ukw0y1.laf.run'
baseUrl = 'http://it9i.com:10007'
break
case 'release':
baseUrl = 'https://ukw0y1.laf.run'
baseUrl = 'http://it9i.com:10007'
break
}
}
Expand Down

0 comments on commit e077416

Please sign in to comment.