Commit 132bcf2b authored by 王曜嵚 Wang Yaoqin's avatar 王曜嵚 Wang Yaoqin

apollo: demo

parent e8dd0641
...@@ -144,7 +144,19 @@ async function handleInstall() { ...@@ -144,7 +144,19 @@ async function handleInstall() {
execute() execute()
} }
async function handleTest () { async function handleTest () {
await $fetch('/graphql') await $fetch('/graphql', {
method: 'post',
body: {
query: `{
v2(username: "wyq"){
username,
tenants {
id
}
}
}`,
}
})
} }
function getTip (status: string) { function getTip (status: string) {
......
import type { NitroApp } from "nitropack"; import type { NitroApp } from "nitropack";
import { ApolloServer, HeaderMap } from '@apollo/server' import { ApolloServer, ContextFunction, HeaderMap } from '@apollo/server'
import { startStandaloneServer } from "@apollo/server/standalone"; import { startStandaloneServer } from "@apollo/server/standalone";
import Resolvers from '../../public/apollo/resolvers/query' import Resolvers from '../../public/apollo/resolvers/query'
import fs from 'fs' import fs from 'fs'
...@@ -23,13 +23,14 @@ export default defineNitroPlugin((nitroApp: NitroApp) => { ...@@ -23,13 +23,14 @@ export default defineNitroPlugin((nitroApp: NitroApp) => {
` `
const server = new ApolloServer({ const server = new ApolloServer({
typeDefs, typeDefs,
resolvers resolvers,
csrfPrevention: false
}) })
startStandaloneServer(server).then(() => { startStandaloneServer(server).then(() => {
console.log('apollo started') console.log('apollo started')
}) })
nitroApp.router.use('/graphql', defineEventHandler({ nitroApp.router.use('/graphql', defineEventHandler({
handler (event) { async handler (event) {
console.log('in graphql') console.log('in graphql')
const headers = new HeaderMap(); const headers = new HeaderMap();
for (const [key, value] of Object.entries(event.headers)) { for (const [key, value] of Object.entries(event.headers)) {
...@@ -37,23 +38,21 @@ export default defineNitroPlugin((nitroApp: NitroApp) => { ...@@ -37,23 +38,21 @@ export default defineNitroPlugin((nitroApp: NitroApp) => {
headers.set(key, Array.isArray(value) ? value.join(', ') : value); headers.set(key, Array.isArray(value) ? value.join(', ') : value);
} }
} }
const body = await readBody(event)
return server.executeHTTPGraphQLRequest({ return server.executeHTTPGraphQLRequest({
httpGraphQLRequest: { httpGraphQLRequest: {
method: 'get', method: event.method.toUpperCase(),
headers, headers,
search: `query{ search: '',
user body
}`,
body: {
username: 'wyq'
}
}, },
context: () => { context: () => {
return Promise.resolve({}) return Promise.resolve({})
} }
}).then((value) => { }).then((value) => {
console.log('success', value) setResponseHeader(event, 'Content-Type', 'application/json')
return value if (value.body.kind === 'complete') return value.body.string
return value.body
}).catch(err => { }).catch(err => {
console.log('err', err) console.log('err', err)
}) })
......
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