GraphQL有自己的语言规范,有一定的上手成本,通过type-graphql,可以使我们无需学习GraphQL语法去编写Schema,使用TypeScript就可以编写Schema!
参考express-typescript-starter 初始化Express+TypeScript环境
npm i apollo-server-express graphql type-graphql class-validator -S
# or
yarn add apollo-server-express graphql type-graphql class-validator -S更多关于Reflect Metadata的介绍,详见https://jkchao.github.io/typescript-book-chinese/tips/metadata.html#%E5%9F%BA%E7%A1%80https://jkchao.github.io/typescript-book-chinese/tips/metadata.html#%E5%9F%BA%E7%A1%80
- 先安装reflect-metadata
npm i reflect-metadata -S- 在入口文件最顶层引入reflect-metadata(在引入type-graphql及resolver之前)
// app.ts
import 'reflect-metadata'// tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"lib": ["es2018", "esnext.asynciterable"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}更多更复杂的Schema定义方法,参考type-graphql官方文档
// app.ts
import { ObjectType, Field } from 'type-graphql'
@ObjectType()
class User {
@Field()
name: string
@Field()
email: string
}此处使用固定数据模拟,真实开发中,数据库、rpc接口、http接口等任何数据源均可作为graphQL的数据提供者
interface User {
name: string
email: string
}
const users: User[] = [
{ name: 'Logan', email: 'im.lizhh@gmail.com' },
{ name: 'Emma', email: 'ouwenxia1994@foxmail.com' }
]参数传递、上下文传递等更多用法,参考type-graphql官方文档
import { Resolver, Query } from 'type-graphql'
@Resolver()
class UserResolver {
@Query(_ => [User])
async getUsers() {
return users
}
}import express from 'express'
import { buildSchemaSync } from 'type-graphql'
const PORT = 3000
const app = express()
const apolloServer = new ApolloServer({
schema: buildSchemaSync({
resolvers: [ UserResolver ],
}),
playground: true,
})
const apolloGraphQLPath = '/graphql'
apolloServer.applyMiddleware({
app,
path: apolloGraphQLPath,
})
app.listen(PORT, () => {
console.log(`server started at http://localhost:${PORT}`)
})运行npm run dev启动服务后,就可以通过http://localhost:3000/graphql访问GraphQL PlayGround
在左侧描述需要请求的数据类型后,点击中间按钮,右侧就会展示服务端返回数据。
也可以点击页面右侧DOCS,查看GraphQL根据Schema自动生成的接口文档
