Commit e8dd0641 authored by 王曜嵚 Wang Yaoqin's avatar 王曜嵚 Wang Yaoqin

apollo demo

parent 49140e33
......@@ -35,6 +35,10 @@
<svg t="1714466881817" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="16209" width="200" height="200"><path d="M966.208 246.752L534.144 21.408a47.968 47.968 0 0 0-44.128-0.128L58.08 243.136A47.968 47.968 0 0 0 32 285.824V744.64c0 18.208 10.304 34.848 26.592 42.976l432 215.36a48 48 0 0 0 42.816 0l432-215.36A48 48 0 0 0 992 744.672V289.344c0-17.92-9.952-34.304-25.792-42.592zM508.384 463.68l-162.176-79.808 367.36-196.704 158.4 82.624-363.584 193.888z m3.488-381.696l132.992 69.376-369.312 197.76-144.896-71.328 381.216-195.808zM96 332.096l153.216 75.392v168.256a32 32 0 0 0 64 0v-136.736L480 521.024v405.184L96 734.752V332.096z m448 594.112V517.184l384-204.736v422.304l-384 191.456z" p-id="16210"></path></svg>
打包微信压缩文件
</div>
<div class="extra-operation" @click="handleTest">
<svg t="1714466881817" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="16209" width="200" height="200"><path d="M966.208 246.752L534.144 21.408a47.968 47.968 0 0 0-44.128-0.128L58.08 243.136A47.968 47.968 0 0 0 32 285.824V744.64c0 18.208 10.304 34.848 26.592 42.976l432 215.36a48 48 0 0 0 42.816 0l432-215.36A48 48 0 0 0 992 744.672V289.344c0-17.92-9.952-34.304-25.792-42.592zM508.384 463.68l-162.176-79.808 367.36-196.704 158.4 82.624-363.584 193.888z m3.488-381.696l132.992 69.376-369.312 197.76-144.896-71.328 381.216-195.808zM96 332.096l153.216 75.392v168.256a32 32 0 0 0 64 0v-136.736L480 521.024v405.184L96 734.752V332.096z m448 594.112V517.184l384-204.736v422.304l-384 191.456z" p-id="16210"></path></svg>
测试
</div>
</div>
</div>
</div>
......@@ -139,6 +143,9 @@ async function handleInstall() {
await $fetch('/api/devops/v2/install', { method: 'post' })
execute()
}
async function handleTest () {
await $fetch('/graphql')
}
function getTip (status: string) {
const HOST = useRuntimeConfig().public.dockerHost
......
This diff is collapsed.
......@@ -10,9 +10,11 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"@apollo/server": "^4.10.4",
"@pinia/nuxt": "^0.5.1",
"dockerode": "^3.3.5",
"engine.io": "^6.5.4",
"graphql": "^16.9.0",
"lodash": "^4.17.21",
"nuxt": "^3.11.1",
"pg": "^8.11.5",
......
export default {
Query: {
user: async (parent: any, args: Record<string, any>, contextValue: Record<string, any>, info: Record<string, any>) => {
return await getPgTableData('user', args.username)
},
v2: async (parent: any, args: Record<string, any>, contextValue: Record<string, any>, info: Record<string, any>) => {
return await getPgTableData('v2', args.username)
},
v1: async (parent: any, args: Record<string, any>, contextValue: Record<string, any>, info: Record<string, any>) => {
return await getPgTableData('v1', args.username)
}
}
}
\ No newline at end of file
type Query {
user(username: String): User
v2(username: String): V2
v1(username: String): V1
}
\ No newline at end of file
type User {
username: String
port: Int
}
\ No newline at end of file
type V1 {
username: String
status: String
serverProperties: [ServerProperty]!
}
\ No newline at end of file
type V2 {
username: String
tenants: Tenant
debug: Debug
has_debugged: Boolean
status: String
serverProperties: [ServerProperty]!
}
type Tenant {
id: String
host: String
databaseschema: String
primarynamespace: String
}
type Debug {
host: String
}
type ServerProperty {
id: Float
key: String
value: String
}
\ No newline at end of file
import type { NitroApp } from "nitropack";
import { ApolloServer, HeaderMap } from '@apollo/server'
import { startStandaloneServer } from "@apollo/server/standalone";
import Resolvers from '../../public/apollo/resolvers/query'
import fs from 'fs'
function readGraphqlString (filename: string) {
const path = './public/apollo/typeDefs/' + filename + '.graphql'
return fs.readFileSync(path, { encoding: 'utf-8' })
}
export default defineNitroPlugin((nitroApp: NitroApp) => {
const resolvers = Resolvers
const query = readGraphqlString('query')
const user = readGraphqlString('user')
const v2 = readGraphqlString('v2')
const v1 = readGraphqlString('v1')
const typeDefs = `
${query}
${user}
${v2}
${v1}
`
const server = new ApolloServer({
typeDefs,
resolvers
})
startStandaloneServer(server).then(() => {
console.log('apollo started')
})
nitroApp.router.use('/graphql', defineEventHandler({
handler (event) {
console.log('in graphql')
const headers = new HeaderMap();
for (const [key, value] of Object.entries(event.headers)) {
if (value !== undefined) {
headers.set(key, Array.isArray(value) ? value.join(', ') : value);
}
}
return server.executeHTTPGraphQLRequest({
httpGraphQLRequest: {
method: 'get',
headers,
search: `query{
user
}`,
body: {
username: 'wyq'
}
},
context: () => {
return Promise.resolve({})
}
}).then((value) => {
console.log('success', value)
return value
}).catch(err => {
console.log('err', err)
})
}
}))
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment