Beforehand, hello to everyone!
I initialize Graphql in Loopback 4 like this:
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}`);
Then I have a relation Favors>User (a User can have many Favors, but a Favor has only one User). I've created this relation with lb4 relation
, and I haven't made any other change.
User Model:
@hasMany(() => Favor)
favors: Favor[];
Favor Model:
@belongsTo(() => User)
userId: string;
At the moment of the query (http://localhost:3000/graphql
) this is what happens:
{
favors {
id
userId
user {
id
}
}
}
I have no idea if it has something to do with the loopback-connector-mongodb
which does not match ObjectId correctly, but I have no clue how to solve this.
My package.json:
"@loopback/core": "^2.12.0",
"openapi-to-graphql": "^2.2.5",
"loopback-connector-mongodb": "^5.4.0",
Hello from the LoopBack team 👋
In order to allow openapi-to-graphql understand relations, the OpenAPI schema produced by a LoopBack application must describe Links between entities.
LoopBack 4 does not provide such metadata out of the box. We have been discussing this use case in GitHub issue loopback-next#2153, unfortunately we haven't found a clear solution yet.
What you can try: In your controllers, enhance your response specifications with a links section pointing to relevant API endpoints for accessing the related models.
Thanks for your response! I will try to do so and update this question with an approach.