loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.96k stars 1.07k forks source link

Exposing GraphQL APIs with nested queries enabled using OASGraph. #2153

Open esoodev opened 5 years ago

esoodev commented 5 years ago

Hello, I am exposing GraphQL API for my current project using OASGraph, and am having issues querying for nested models when tested on GraphiQL.

What I Did

I currently have three models: User, GameProfile, and Game. Their relationships are as follows:

It is my understanding that simply typing into the console npx oasgraph http://localhost:3000/openapi.json provided by the tutorial won't expose nested relationships, since OASGraph's default option for addSubOperation is false.

Therefore, after creating a GET route for /users/{id}/game-profiles for retrieving GameProfile model instances belong to User, and testing that it works as expected, I created my own OASGraph server following the guideline, creating the necessary schema as follows :

const oas = JSON.parse(
    await request.get('http://localhost:3000/openapi.json')
);
const { schema, report } = await createGraphQlSchema(oas, {
    addSubOperations: true
});

Current Behavior

Starting the server and navigating to GraphiQL I ran into two issues :

  1. The query won't recognize "users" as valid, and recommends that I query for "user" "users2" or "userCount."
  2. The output for the query is null.
    query{
    users2 {
    gameProfiles {
      gameProfileId
      gameProfileNickname
      statusId
      createdAt
      userId
      gameId
    }
    }
    }
    {
    "data": {
    "users2": [
      {
        "gameProfiles": null
      }
    ]
    }
    }

Comment

Would anyone have any idea as to why this might be happening? Your help is appreciated!

bajtos commented 5 years ago

@esoodev please create a small application we can use to reproduce the issue locally. See https://loopback.io/doc/en/contrib/Reporting-issues.html#loopback-4x-bugs. You can use examples/todo-list as the app to start from, it comes with a has-many relation between TodoList and Todo models.

esoodev commented 5 years ago

@bajtos If it's alright with you, I am attaching a link to the project, since it's quite small and can be used to observe the problem mentioned above. npm install && npm start then navigate to GraphiQL with example query

bajtos commented 5 years ago

Thank you for the link. I was able to run your app and reproduce the issue. Kind of. I am confused about where users2 is coming from and also how is it possible that gameProfile is including a nested user data inside?

Anyhow. I think to make the relations work, we need to include "links" in the description of REST API methods.

Quoting from https://github.com/strongloop/oasgraph#characteristics:

Nested data Links defined in the OAS are used to compose data definitions. Furthermore, hierarchical path structures can be used to nest data via the addSubOperations option.

Link to OpenAPI spec:

What to try: in your controllers, enhance your response specifications with a links section pointing to relevant endpoints. Once we figure out the right way how to make this work, it would be great to update our todo-list example accordingly and describe this solution in our docs too.

esoodev commented 5 years ago

@bajtos Thank you for the link to the OpenAPI spec. I edited my response specifications accordingly as you advised. I can query individual GameProfile with its User successfully. Regarding my attempt to query a list of GameProfiles with their respective Users, it doesn't seem as simple as I thought. I still need to figure out how.

On the other hand, I still do not understand why using oasgraph as a module within the loopback project would prompt me to query for "users2" and "gameProfiles2" instead of "users" and "gameProfiles." For the time being, I am using the CLI method of calling npx oasgraph http://localhost:3000/openapi.json as workaround since that doesn't cause the issue.

dougal83 commented 5 years ago

query for "users2" and "gameProfiles2" instead of "users" and "gameProfiles."

I think that is down to the oasgraph implementation. Probably prudent to raise the issue here: https://github.com/strongloop/oasgraph/issues

stale[bot] commented 4 years ago

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] commented 4 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.