【发布时间】:2021-06-24 03:06:17
【问题描述】:
在网上搜索了一个具体示例后,我认输并寻求帮助。
我正在使用 Apollo 服务器、GraphQL 和 Sequelize,并且我正在调用存储过程,该过程返回从两个不同表创建的记录集。我正在取回数据,但我不知道如何将结果放入 GraphQL 模式响应中。
这是我的解析器中的代码:
async functionName(_, {input}, {user = null}) {
if (!user) {
throw new AuthenticationError('You must login to use this function');
}
const {record_id} = input;
const result = await DBService.query(
'Call sp_JoinTwoTables_Select(:id)',
{
model: FooModel,
mapToModel: true,
raw: true,
replacements: {id: record_id},
type: QueryTypes.SELECT
}
);
console.log('functionName.result');
console.log(result); // Getting results
return result;
}
这是我的架构中的代码:
const {gql} = require('apollo-server-express');
module.exports = gql`
type Foo {
id: Int!
foo_name: String!
date_created: String!
date_modified: String!
}
extend type Mutation {
functionName(input: fooInput!): fooResponse!
}
input fooInput {
id: Int!
}
type fooResponse {
tree: [fooSchemaForBothTables!]
}
type fooSchemaForBothTables {
id: Int!
foo_name: String!
column_from_second_table: Int!
}
`;
由于数据库中没有表,我创建了一个简单的对象。当失败时,我尝试了一个续集模型对象,但这也失败了。这是这段代码:
module.exports = {FooModel: {
id: 0,
fooName: '',
column_from_second_table: 0
}};
我得到的输出是(不是我想的二维数组):
Executing (default): Call sp_CommunityHierarchy_Select(9)
selectHierarchyTree.result
[
{
'0': {
community_id: 1,
community_name: 'Cars',
level_from_apex: null,
parent_id: null
},
'1': {
community_id: 8,
community_name: 'Chevy',
level_from_apex: 2,
parent_id: 1
},
'2': {
community_id: 9,
community_name: 'Suburban',
level_from_apex: 3,
parent_id: 8
},
meta: [ [ColumnDef], [ColumnDef], [ColumnDef], [ColumnDef] ]
},
{ affectedRows: 6, insertId: 0, warningStatus: 0 }
]
【问题讨论】:
-
从数据库返回什么(形状)? ...如何(查看 grqphql 类型)[你认为]响应(形状)应该是什么样子?
-
形状是一个基本记录集,二维数组,其中每条记录与我给出的示例中的模式 fooSchemaForBothTables 匹配。
-
console.log(result)? -
我将结果的输出添加到我原来的问题中。
标签: stored-procedures graphql sequelize.js apollo