我使用了graphql和koa2。我不想将graphql应用于所有接口。我想尝试Restful API + graphql
const Koa = require('koa')
const json = require('koa-json')
const bodyparser = require('koa-bodyparser')
const config = require('./config')
const port = config.URL.port
const cors = require('@koa/cors')
const UserRoute = require('./routes/user.router')
const { ApolloServer, gql } = require('apollo-server-koa')
const app = new Koa()
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`
const resolvers = {
Query: {
books: () => books,
},
}
app.use(cors({
origin: '*',
credentials: true,
methods: ['PUT', 'POST', 'GET', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Content-Length', 'Authorization', 'Accept', 'X-Requested-With', 'x-access-token']
}))
app.use(UserRoute.routes(), UserRoute.allowedMethods())
app.use(bodyparser())
app.use(json())
const server = new ApolloServer({ typeDefs, resolvers })
server.applyMiddleware({
app,
path: config.URL.graphql,
})
module.exports = app.listen(port)
我只想在某些接口上使用graphql。
我使用邮递员进行调试。有什么好的建议吗?
谢谢