graphql使用主体解析器中间件?

时间:2019-04-21 15:38:27

标签: graph apollo-server

我使用了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。

image

image

我使用邮递员进行调试。有什么好的建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)