-
Notifications
You must be signed in to change notification settings - Fork 210
Description
Describe the bug
We're trying to expose a UK gov API with the following spec:
https://api.publish-teacher-training-courses.service.gov.uk/api-docs/public_v1/api_spec.json
Unfortunately, they decided to implement it using the content type application/vnd.api+json.
Currently, the resolver code in this project is only ever treating application/json as a first-class citizen; after changing the spec to have a content type of application/vnd.api+json, the number of entries that appear in the query section reduce signficantly. Please start treating application/vnd.api+json and application/json as synonyms.
To Reproduce
Steps to reproduce the behavior:
- Download this file
- Globally Replace 'float' with number, and save it as file spec.json
- create the following index.js app:
const express = require('express')
const {graphqlHTTP} = require('express-graphql')
const {createGraphQLSchema} = require('openapi-to-graphql')
const oas = require('./spec.json');
async function main(oas) {
// generate schema:
const {schema, report} = await createGraphQLSchema(oas, {
strict: false
})
// server schema:
const app = express()
app.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true,
})
)
app.listen(3001)
}
main(oas)
- create the following package.json file:
{
"name": "gql",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"express-graphql": "^0.12.0",
"graphql": "^15.8.0",
"openapi-to-graphql": "^2.6.3"
}
}
- Run the following:
npm install
node index.js
- Point a browser to http://localhost:3001/graphql
- Run the following Query:
query{
courseLocationListResponse (courseCode:"2N22",providerCode:"B20", year:"2020"){
jsonapi{
version
}
}
}
The response we get is the following:
{
"errors": [
{
"message": "Operation GET /recruitment_cycles/{year}/providers/{provider_code}/courses/{course_code}/locations should have a content-type 'application/json' but has 'application/vnd.api+json' instead",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"courseLocationListResponse"
]
}
],
"data": {
"courseLocationListResponse": null
}
}
Expected behavior
I'd expect the app to understand that application/json and application/vnd.api+json are compatible, and return the correct results.
Screenshots
N/A.
Additional context
Changing the spec.json file to use ''application/vnd.api+json" instead of "application/json" causes erratic behaviour, as the API does not understand this mime type.
The issue is likely to be resolved by loosening the if statement here to also treat ''application/vnd.api+json" in the same way:
https://github.com/IBM/openapi-to-graphql/blob/master/packages/openapi-to-graphql/src/resolver_builder.ts#L809