解析相关对象的惯用的、高效的方法是什么?
What is the idiomatic, performant way to resolve related objects?
如何在 GraphQL 中编写对关系数据库表现良好的查询解析器?
使用 this tutorial 中的示例架构,假设我有一个包含 users
和 stories
的简单数据库。用户可以创作多个故事,但故事只有一个用户作为作者(为简单起见)。
查询用户时,可能还希望获得该用户创作的所有故事的列表。一种可能的定义是 GraphQL 查询来处理它(从上面的链接教程中窃取):
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: User,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
},
resolve(parent, {id}, {db}) {
return db.get(`
SELECT * FROM User WHERE id = $id
`, {$id: id});
}
},
})
});
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
stories: {
type: new GraphQLList(Story),
resolve(parent, args, {db}) {
return db.all(`
SELECT * FROM Story WHERE author = $user
`, {$user: parent.id});
}
}
})
});
这将按预期工作;如果我查询一个特定的用户,如果需要的话,我也可以得到那个用户的故事。然而,这并不理想。当使用 JOIN
的单个查询就足够时,它需要两次访问数据库。如果我查询多个用户,问题会被放大——每个额外的用户都会导致额外的数据库查询。我遍历对象关系越深,问题就会呈指数级恶化。
这个问题解决了吗?有没有一种方法可以编写不会导致生成低效 SQL 查询的查询解析器?
有两种解决此类问题的方法。
Facebook 使用的一种方法是将一次发生的请求排入队列,并在发送前将它们组合在一起。这样一来,您就可以通过一个请求来检索有关多个用户的信息,而不是为每个用户发出请求。 Dan Schafer 写了一篇 good comment explaining this approach. Facebook released Dataloader,这是该技术的一个示例实现。
// Pass this to graphql-js context
const storyLoader = new DataLoader((authorIds) => {
return db.all(
`SELECT * FROM Story WHERE author IN (${authorIds.join(',')})`
).then((rows) => {
// Order rows so they match orde of authorIds
const result = {};
for (const row of rows) {
const existing = result[row.author] || [];
existing.push(row);
result[row.author] = existing;
}
const array = [];
for (const author of authorIds) {
array.push(result[author] || []);
}
return array;
});
});
// Then use dataloader in your type
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
stories: {
type: new GraphQLList(Story),
resolve(parent, args, {rootValue: {storyLoader}}) {
return storyLoader.load(parent.id);
}
}
})
});
虽然这不能解决高效 SQL,但它对于许多用例来说仍然足够好,并且会使东西 运行 更快。对于不允许 JOIN 的非关系数据库,这也是一种很好的方法。
另一种方法是在解析函数中使用有关请求字段的信息,以便在相关时使用 JOIN。解析上下文有 fieldASTs
字段,它解析了当前解析查询部分的 AST。通过查看该 AST (selectionSet) 的子项,我们可以预测是否需要连接。一个非常简单和笨拙的例子:
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
stories: {
type: new GraphQLList(Story),
resolve(parent, args, {rootValue: {storyLoader}}) {
// if stories were pre-fetched use that
if (parent.stories) {
return parent.stories;
} else {
// otherwise request them normally
return db.all(`
SELECT * FROM Story WHERE author = $user
`, {$user: parent.id});
}
}
}
})
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: User,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
},
resolve(parent, {id}, {rootValue: {db}, fieldASTs}) {
// find names of all child fields
const childFields = fieldASTs[0].selectionSet.selections.map(
(set) => set.name.value
);
if (childFields.includes('stories')) {
// use join to optimize
return db.all(`
SELECT * FROM User INNER JOIN Story ON User.id = Story.author WHERE User.id = $id
`, {$id: id}).then((rows) => {
if (rows.length > 0) {
return {
id: rows[0].author,
name: rows[0].name,
stories: rows
};
} else {
return db.get(`
SELECT * FROM User WHERE id = $id
`, {$id: id}
);
}
});
} else {
return db.get(`
SELECT * FROM User WHERE id = $id
`, {$id: id}
);
}
}
},
})
});
请注意,这可能有问题,例如片段。但是也可以处理它们,这只是更详细地检查选择集的问题。
目前在 graphql-js 存储库中有一个 PR,这将允许通过在上下文中提供 'resolve plan' 来编写更复杂的查询优化逻辑。
如何在 GraphQL 中编写对关系数据库表现良好的查询解析器?
使用 this tutorial 中的示例架构,假设我有一个包含 users
和 stories
的简单数据库。用户可以创作多个故事,但故事只有一个用户作为作者(为简单起见)。
查询用户时,可能还希望获得该用户创作的所有故事的列表。一种可能的定义是 GraphQL 查询来处理它(从上面的链接教程中窃取):
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: User,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
},
resolve(parent, {id}, {db}) {
return db.get(`
SELECT * FROM User WHERE id = $id
`, {$id: id});
}
},
})
});
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
stories: {
type: new GraphQLList(Story),
resolve(parent, args, {db}) {
return db.all(`
SELECT * FROM Story WHERE author = $user
`, {$user: parent.id});
}
}
})
});
这将按预期工作;如果我查询一个特定的用户,如果需要的话,我也可以得到那个用户的故事。然而,这并不理想。当使用 JOIN
的单个查询就足够时,它需要两次访问数据库。如果我查询多个用户,问题会被放大——每个额外的用户都会导致额外的数据库查询。我遍历对象关系越深,问题就会呈指数级恶化。
这个问题解决了吗?有没有一种方法可以编写不会导致生成低效 SQL 查询的查询解析器?
有两种解决此类问题的方法。
Facebook 使用的一种方法是将一次发生的请求排入队列,并在发送前将它们组合在一起。这样一来,您就可以通过一个请求来检索有关多个用户的信息,而不是为每个用户发出请求。 Dan Schafer 写了一篇 good comment explaining this approach. Facebook released Dataloader,这是该技术的一个示例实现。
// Pass this to graphql-js context
const storyLoader = new DataLoader((authorIds) => {
return db.all(
`SELECT * FROM Story WHERE author IN (${authorIds.join(',')})`
).then((rows) => {
// Order rows so they match orde of authorIds
const result = {};
for (const row of rows) {
const existing = result[row.author] || [];
existing.push(row);
result[row.author] = existing;
}
const array = [];
for (const author of authorIds) {
array.push(result[author] || []);
}
return array;
});
});
// Then use dataloader in your type
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
stories: {
type: new GraphQLList(Story),
resolve(parent, args, {rootValue: {storyLoader}}) {
return storyLoader.load(parent.id);
}
}
})
});
虽然这不能解决高效 SQL,但它对于许多用例来说仍然足够好,并且会使东西 运行 更快。对于不允许 JOIN 的非关系数据库,这也是一种很好的方法。
另一种方法是在解析函数中使用有关请求字段的信息,以便在相关时使用 JOIN。解析上下文有 fieldASTs
字段,它解析了当前解析查询部分的 AST。通过查看该 AST (selectionSet) 的子项,我们可以预测是否需要连接。一个非常简单和笨拙的例子:
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
stories: {
type: new GraphQLList(Story),
resolve(parent, args, {rootValue: {storyLoader}}) {
// if stories were pre-fetched use that
if (parent.stories) {
return parent.stories;
} else {
// otherwise request them normally
return db.all(`
SELECT * FROM Story WHERE author = $user
`, {$user: parent.id});
}
}
}
})
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: User,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
},
resolve(parent, {id}, {rootValue: {db}, fieldASTs}) {
// find names of all child fields
const childFields = fieldASTs[0].selectionSet.selections.map(
(set) => set.name.value
);
if (childFields.includes('stories')) {
// use join to optimize
return db.all(`
SELECT * FROM User INNER JOIN Story ON User.id = Story.author WHERE User.id = $id
`, {$id: id}).then((rows) => {
if (rows.length > 0) {
return {
id: rows[0].author,
name: rows[0].name,
stories: rows
};
} else {
return db.get(`
SELECT * FROM User WHERE id = $id
`, {$id: id}
);
}
});
} else {
return db.get(`
SELECT * FROM User WHERE id = $id
`, {$id: id}
);
}
}
},
})
});
请注意,这可能有问题,例如片段。但是也可以处理它们,这只是更详细地检查选择集的问题。
目前在 graphql-js 存储库中有一个 PR,这将允许通过在上下文中提供 'resolve plan' 来编写更复杂的查询优化逻辑。