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

apollo: demo

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