Loopback 4 Relations not working as expected
Describe the bug
Working right now with Loopback 4 + MongoDB. I have two schemas:
- Favor (think about it like Post)
- Comment
Favor has a relation with Comment like this:
@hasMany(() => Comment)
comments: Comment[];
And both are working as expected from Loopback with /favors/{id}/comments. But this is where things get weird, and non-compliant:
query UglyGetComments {
favorComments(id:"5fc3a41230196977b823389a") {
text
}
}
query IdealGetComments {
favors {
comments {
text
}
}
}
Response from UglyGetComments:
{
"data": {
"favorComments": []
}
}
Response from IdealGetComments:
{
"data": {
"favors": [
{
"comments": null
}
]
}
}
Expected behavior Same result on both ends
Additional context
Using:
"express-graphql": "^0.12.0"
"openapi-to-graphql": "^2.2.5"
And this is how I mount graphql:
const url = app.restServer.url;
console.log(`Server is running at ${url}`);
console.log(`Try ${url}/ping`);
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}`);
@FMGordillo Thank you for filling this issue. Is it possible to provide a sample project and/or the OAS that Loopback produces? It will be easier to understand the problem that way.
Thank you for your response @Alan-Cha ! Here you go: https://gist.github.com/FMGordillo/66ac90908048e6686295bb2e60cd0174
Closing because of no relevance anymore