【发布时间】:2021-03-12 01:04:00
【问题描述】:
先向大家问好!
我在 Loopback 4 中这样初始化 Graphql:
const graphqlPath = '/graphql';
// @ts-ignore
const oas: Oas3 = await (<Oas3>app.restServer.getApiSpec());
console.log(graphqlHTTP);
const {schema} = await createGraphQLSchema(oas, {
strict: false,
viewer: true,
baseUrl: url,
});
//@ts-ignore
const handler: graphqlHTTP.Middleware = graphqlHTTP({
schema,
graphiql: true,
});
app.mountExpressRouter(graphqlPath, handler);
console.log(`Graphql: ${url}${graphqlPath}`);
然后我有一个 Favors>User 关系(一个用户可以有很多 Favor,但一个 Favor 只有一个用户)。我已经与lb4 relation 建立了这种关系,并且我没有进行任何其他更改。
用户模型:
@hasMany(() => Favor)
favors: Favor[];
喜欢的模特:
@belongsTo(() => User)
userId: string;
在查询 (http://localhost:3000/graphql) 时会发生以下情况:
{
favors {
id
userId
user {
id
}
}
}
我不知道它是否与不正确匹配 ObjectId 的 loopback-connector-mongodb 有关,但我不知道如何解决这个问题。
我的 package.json:
"@loopback/core": "^2.12.0",
"openapi-to-graphql": "^2.2.5",
"loopback-connector-mongodb": "^5.4.0",
【问题讨论】: