graphql-tools
Apollo によって開発が進められている、GraphQL.js をサポートするライブラリ群。基本的には、GraphQL.js の上に便利な API をいくつか生やすレイヤーの役割を担っていると思ってしまって良さそう。
GraphQL.js における GraphQLSchema インスタンスを作成する各種 API
GraphQL API のモッキング
複数の GraphQL API の Schema Stitching
https://github.com/apollographql/graphql-tools
GraphQL.js の大きな欠点は、SDL から直接 GraphQLSchema オブジェクトを生成することが簡単にできず、スキーマファーストな開発をしづらくしている点、という意見があり、graphql-tools はそのギャップをうめるものとされている。
The biggest shortcoming of GraphQL.js is that it doesn’t allow you to write a schema in the SDL and then easily generate an executable version of a GraphQLSchema.
https://www.prisma.io/blog/graphql-server-basics-the-schema-ac5e2950214e
例えば、makeExecutableSchema は、SDL の文字列とリゾルバー定義を別々に定義しておき、その2つを引数に渡すだけで GraphQLSchema オブジェクトを生成してくれる。この時、フィールドとリゾルバーの紐づけは勝手に行ってくれる。
code:js
const { makeExecutableSchema } = require('graphql-tools')
const typeDefs = `
type Query {
item(id: ID!): Item
}
type Item {
id: ID!
name: String
}
`
const resolvers = {
Query: {
item: (root, args, context, info) => {
return fetchItemById(args.id)
}
},
}
const schema = makeExecutableSchema({
typeDefs,
resolvers
})
また、これ以外にも、スキーマ同士を集約してくれる機能等、便利な機能がある。