GraphQL LogoGraphQL 中文网

构造类型

对于许多应用,你可以在应用启动时定义固定结构,并使用 GraphQL 结构语言来定义它。在某些情况下,以编程方式构建结构很有用。你可以使用 GraphQLSchema 构造函数来完成此操作。

英:For many apps, you can define a fixed schema when the application starts, and define it using GraphQL schema language. In some cases, it's useful to construct a schema programmatically. You can do this using the GraphQLSchema constructor.

当你使用 GraphQLSchema 构造函数创建结构时,你可以将它们创建为单独的对象类型,而不是仅使用结构语言定义 QueryMutation 类型。

英:When you are using the GraphQLSchema constructor to create a schema, instead of defining Query and Mutation types solely using schema language, you create them as separate object types.

例如,假设我们正在构建一个简单的 API,可让你根据 ID 获取一些硬编码用户的用户数据。使用 buildSchema 我们可以编写一个服务器:

英:For example, let's say we are building a simple API that lets you fetch user data for a few hardcoded users based on an id. Using buildSchema we could write a server with:

var express = require("express")
var { createHandler } = require("graphql-http/lib/use/express")
var { buildSchema } = require("graphql")
var schema = buildSchema(`
type User {
id: String
name: String
}
type Query {
user(id: String): User
}
`)
// Maps id to User object
var fakeDatabase = {
a: {
id: "a",
name: "alice",
},
b: {
id: "b",
name: "bob",
},
}
var root = {
user: ({ id }) => {
return fakeDatabase[id]
},
}
var app = express()
app.all(
"/graphql",
createHandler({
schema: schema,
rootValue: root,
})
)
app.listen(4000)
console.log("Running a GraphQL API server at localhost:4000/graphql")

我们可以在不使用 GraphQL 结构语言的情况下实现相同的 API:

英:We can implement this same API without using GraphQL schema language:

var express = require("express")
var { createHandler } = require("graphql-http/lib/use/express")
var graphql = require("graphql")
// Maps id to User object
var fakeDatabase = {
a: {
id: "a",
name: "alice",
},
b: {
id: "b",
name: "bob",
},
}
// Define the User type
var userType = new graphql.GraphQLObjectType({
name: "User",
fields: {
id: { type: graphql.GraphQLString },
name: { type: graphql.GraphQLString },
},
})
// Define the Query type
var queryType = new graphql.GraphQLObjectType({
name: "Query",
fields: {
user: {
type: userType,
// `args` describes the arguments that the `user` query accepts
args: {
id: { type: graphql.GraphQLString },
},
resolve: (_, { id }) => {
return fakeDatabase[id]
},
},
},
})
var schema = new graphql.GraphQLSchema({ query: queryType })
var app = express()
app.all(
"/graphql",
createHandler({
schema: schema,
})
)
app.listen(4000)
console.log("Running a GraphQL API server at localhost:4000/graphql")

当我们使用这种创建 API 的方法时,根级别解析器是在 QueryMutation 类型上实现的,而不是在 root 对象上实现的。

英:When we use this method of creating the API, the root level resolvers are implemented on the Query and Mutation types rather than on a root object.

如果你想从其他内容(例如数据库结构)自动创建 GraphQL 结构,这尤其有用。你可能具有用于创建和更新数据库记录之类的通用格式。这对于实现联合类型等不能完全映射到 ES6 类和结构语言的功能也很有用。

英:This is particularly useful if you want to create a GraphQL schema automatically from something else, like a database schema. You might have a common format for something like creating and updating database records. This is also useful for implementing features like union types which don't map cleanly to ES6 classes and schema language.

继续阅读 →graphql-http
GraphQL 中文网 - 粤ICP备13048890号