openapi-to-graphql icon indicating copy to clipboard operation
openapi-to-graphql copied to clipboard

Loopback 4 Relations not working as expected

Open FMGordillo opened this issue 5 years ago • 2 comments

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 avatar Nov 29 '20 14:11 FMGordillo

@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.

Alan-Cha avatar Dec 03 '20 17:12 Alan-Cha

Thank you for your response @Alan-Cha ! Here you go: https://gist.github.com/FMGordillo/66ac90908048e6686295bb2e60cd0174

FMGordillo avatar Dec 05 '20 18:12 FMGordillo

Closing because of no relevance anymore

FMGordillo avatar Jan 10 '24 20:01 FMGordillo