IBM / openapi-to-graphql

Translate APIs described by OpenAPI Specifications (OAS) into GraphQL
https://developer.ibm.com/open/projects/openapi-to-graphql/
MIT License
1.61k stars 210 forks source link

Invalid URI (missing protocol) #32

Open ind1go opened 6 years ago

ind1go commented 6 years ago

I'm using the oasgraph CLI, with a Swagger doc generated from Liberty's built-in API explorer - i.e. I go to /ibm/api/docs and download the JSON it gives me.

When I run a GraphQL request, I get the following response:

{
  "errors": [
    {
      "message": "Invalid URI \"//host:27634/path/to/api\"",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "sysplex"
      ]
    }
  ],
  "data": {
    "sysplex": null
  }
}

It appears that the protocol is not being added to the URI.

With trace on I see:

 http Call GET //host:27634/path/to/api? headers:{"content-type":"application/json","accept":"application/json"} +0ms
  http Error: Invalid URI "//host:27634/path/to/api"
  http     at Request.init (/path/oasgraph/node_modules/request/request.js:273:31)
  http     at new Request (/path/oasgraph/node_modules/request/request.js:127:8)
  http     at request (/path/oasgraph/node_modules/request/index.js:53:10)
  http     at Promise (/path/oasgraph/lib/resolver_builder.js:161:13)
  http     at new Promise (<anonymous>)
  http     at /path/oasgraph/lib/resolver_builder.js:160:16
  http     at resolveFieldValueOrError (/path/oasgraph/node_modules/graphql/execution/execute.js:479:18)
  http     at resolveField (/path/oasgraph/node_modules/graphql/execution/execute.js:446:16)
  http     at executeFields (/path/oasgraph/node_modules/graphql/execution/execute.js:293:18)
  http     at executeOperation (/path/oasgraph/node_modules/graphql/execution/execute.js:237:122) +3ms
ErikWittern commented 6 years ago

I take it you are using a Swagger, and not an OpenAPI Specification 3, correct? If so, what's defined in the schemes property at the root of that Swagger? In general, OASGraph will chose the single entry present. If both, https and http are defined, OASGraph will chose https over http.

ind1go commented 6 years ago

That's right, using Swagger 2 rather than OAS 3 (and, furthermore, auto-generated Swagger from JAX-RS). The schemes property is not set for most of the endpoints. In the spec I see that schemes:

The transfer protocol of the API. Values MUST be from the list: "http", "https", "ws", "wss". If the schemes is not included, the default scheme to be used is the one used to access the Swagger definition itself.

Which, I guess, is undefined at the point of running the oasgraph CLI, but would be good to provide as an option to the CLI, perhaps? Unless you assume a default of https which doesn't seem right.

Alan-Cha commented 6 years ago

OASGraph is built for OAS 3, not Swagger 2, so I don't think it makes sense to add a Swagger-specific option to the CLI. In OASGraph, we immediately convert the Swagger to an OAS using an external library. I think the best we can do is a validation check on the servers property in the OAS and perhaps add a protocol in unstrict mode.

Another thing to keep in mind is that the CLI isn't the intended usage of OASGraph, at least in this iteration. It's just a small script for quick testing. It doesn't provide nearly the flexibility and functionality of createGraphQlSchema(spec, options).

In any case, did you make the necessary changes and get the GraphQL server running?

ind1go commented 6 years ago

I've not tried making changes to the Swagger doc (really, want to be able to pipe it straight from Liberty's discoverable API catalog to oasgraph) but will have a go and see where I get.

ErikWittern commented 5 years ago

We should create a warning if there is no scheme defined and OASGraph selects a fall-back.

KolbySisk commented 3 years ago

I'm running into this problem as well. I'm also using a generated Swagger 2.0.

I tried appending the property manually with a swaggerObject.schemes = 'https'; but that didn't seem to help. Any advice on fixing this?

KolbySisk commented 3 years ago

I was able to make it work by adding: swaggerObject.schemes = ['https'];