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 211 forks source link

Cannot use 'in' operator to search for '#' in undefined #323

Closed mcoenen1994 closed 4 years ago

mcoenen1994 commented 4 years ago

Describe the bug After creating a new project with the loopback 4 cli, I try to create a graphql schema from the Openapi json file. However, it fails with following error:

openapi-to-graphql http://localhost:3000/openapi.json
OpenAPI-to-GraphQL creation event error: Cannot use 'in' operator to search for '#' in undefined

To Reproduce Steps to reproduce the behavior:

  1. Create new project with tslint (lb4 app)
  2. Create new model with id, name, image, subphase (lb4 controller)
  3. Create new in-memory datasource (lb4 datasource)
  4. Create new repository with DefaultCrudRepository (lb4 repository)
  5. Create new controller (lb4 controller)
  6. Start the project (npm start)
  7. Use openapi-to-graphql to get new schema (openapi-to-graphql http://localhost:3000/openapi.json)
  8. See above error

Expected behavior Expect a Graphql schema of this because I did not change any code of the model, repository or controller. It should at least work with a generated project to start developing.

Code

export class PartnerController {
  constructor(
    @repository(PartnerRepository)
    public partnerRepository : PartnerRepository,
  ) {}

  @post('/partners', {
    responses: {
      '200': {
        description: 'Partner model instance',
        content: {'application/json': {schema: getModelSchemaRef(Partner)}},
      },
    },
  })
  async create(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Partner, {
            title: 'NewPartner',

          }),
        },
      },
    })
    partner: Partner,
  ): Promise<Partner> {
    return this.partnerRepository.create(partner);
  }

  @get('/partners/count', {
    responses: {
      '200': {
        description: 'Partner model count',
        content: {'application/json': {schema: CountSchema}},
      },
    },
  })
  async count(
    @param.where(Partner) where?: Where<Partner>,
  ): Promise<Count> {
    return this.partnerRepository.count(where);
  }

  @get('/partners', {
    responses: {
      '200': {
        description: 'Array of Partner model instances',
        content: {
          'application/json': {
            schema: {
              type: 'array',
              items: getModelSchemaRef(Partner, {includeRelations: true}),
            },
          },
        },
      },
    },
  })
  async find(
    @param.filter(Partner) filter?: Filter<Partner>,
  ): Promise<Partner[]> {
    return this.partnerRepository.find(filter);
  }

  @patch('/partners', {
    responses: {
      '200': {
        description: 'Partner PATCH success count',
        content: {'application/json': {schema: CountSchema}},
      },
    },
  })
  async updateAll(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Partner, {partial: true}),
        },
      },
    })
    partner: Partner,
    @param.where(Partner) where?: Where<Partner>,
  ): Promise<Count> {
    return this.partnerRepository.updateAll(partner, where);
  }

  @get('/partners/{id}', {
    responses: {
      '200': {
        description: 'Partner model instance',
        content: {
          'application/json': {
            schema: getModelSchemaRef(Partner, {includeRelations: true}),
          },
        },
      },
    },
  })
  async findById(
    @param.path.number('id') id: number,
    @param.filter(Partner, {exclude: 'where'}) filter?: FilterExcludingWhere<Partner>
  ): Promise<Partner> {
    return this.partnerRepository.findById(id, filter);
  }

  @patch('/partners/{id}', {
    responses: {
      '204': {
        description: 'Partner PATCH success',
      },
    },
  })
  async updateById(
    @param.path.number('id') id: number,
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Partner, {partial: true}),
        },
      },
    })
    partner: Partner,
  ): Promise<void> {
    await this.partnerRepository.updateById(id, partner);
  }

  @put('/partners/{id}', {
    responses: {
      '204': {
        description: 'Partner PUT success',
      },
    },
  })
  async replaceById(
    @param.path.number('id') id: number,
    @requestBody() partner: Partner,
  ): Promise<void> {
    await this.partnerRepository.replaceById(id, partner);
  }

  @del('/partners/{id}', {
    responses: {
      '204': {
        description: 'Partner DELETE success',
      },
    },
  })
  async deleteById(@param.path.number('id') id: number): Promise<void> {
    await this.partnerRepository.deleteById(id);
  }
}

If this issue is missing some information, please let me know!

Thanks, Maikel

Alan-Cha commented 4 years ago

@mcoenen1994 Hey! Thank you for reporting this issue! Would it be possible for you to create a repository with the code in place so we can more easily extract the OAS (i.e. run npm i and npm start and go directly to http://localhost:3000/openapi.json)? Or equally good, would it be possible for you to get the OAS and put it in a GitHub gist for us? That way, we can get the exact version of the OAS that is causing problems.

It would also be nice to include the exact version of LoopBack and OtG that you are using.

mcoenen1994 commented 4 years ago

Hi @Alan-Cha,

Thanks for the fast response. I created a repo.

Here are also the versions of lb4 and OtG:

@loopback/cli version: 2.6.0

@loopback/* dependencies:
  - @loopback/authentication: ^4.2.3
  - @loopback/boot: ^2.2.0
  - @loopback/build: ^5.3.1
  - @loopback/context: ^3.7.0
  - @loopback/core: ^2.5.0
  - @loopback/metadata: ^2.1.3
  - @loopback/openapi-spec-builder: ^2.1.3
  - @loopback/openapi-v3: ^3.3.1
  - @loopback/repository-json-schema: ^2.4.0
  - @loopback/repository: ^2.4.0
  - @loopback/rest: ^4.0.0
  - @loopback/testlab: ^3.1.3
  - @loopback/docs: ^3.7.0
  - @loopback/example-hello-world: ^2.0.8
  - @loopback/example-log-extension: ^2.0.8
  - @loopback/example-rpc-server: ^2.0.8
  - @loopback/example-todo: ^3.3.0
  - @loopback/example-soap-calculator: ^2.1.1
  - @loopback/service-proxy: ^2.2.0
  - @loopback/http-caching-proxy: ^2.1.3
  - @loopback/http-server: ^2.1.3
  - @loopback/example-todo-list: ^3.1.1
  - @loopback/dist-util: ^0.4.0
  - @loopback/rest-explorer: ^2.2.0
  - @loopback/eslint-config: ^6.0.6
  - @loopback/example-express-composition: ^2.1.1
  - @loopback/example-greeter-extension: ^2.0.8
  - @loopback/booter-lb3app: ^2.1.3
  - @loopback/example-lb3-application: ^2.0.8
  - @loopback/example-greeting-app: ^2.0.8
  - @loopback/example-context: ^2.0.8
  - @loopback/repository-tests: ^0.12.3
  - @loopback/extension-health: ^0.4.3
  - @loopback/authorization: ^0.5.8
  - @loopback/rest-crud: ^0.8.3
  - @loopback/security: ^0.2.8
  - @loopback/authentication-passport: ^2.1.3
  - @loopback/example-metrics-prometheus: ^0.2.8
  - @loopback/extension-metrics: ^0.3.3
  - @loopback/model-api-builder: ^2.1.3
  - @loopback/extension-logging: ^0.3.3
  - @loopback/example-access-control-migration: ^1.2.1
  - @loopback/example-file-transfer: ^1.2.5
  - @loopback/example-rest-crud: ^1.2.1
  - @loopback/apiconnect: ^0.3.1
  - @loopback/example-validation-app: ^1.3.1
  - @loopback/cron: ^0.2.3
  - @loopback/example-multi-tenancy: ^0.2.1
  - @loopback/example-passport-login: ^1.3.0
  - @loopback/authentication-jwt: ^0.2.1
  - @loopback/context-explorer: ^0.1.3
  - @loopback/express: ^1.1.0
$ openapi-to-graphql -V
2.1.0
yassinebenameur commented 4 years ago

Hello, i have the exact same problem.

Geril commented 4 years ago

the same here 😞

Alan-Cha commented 4 years ago

@mcoenen1994 @yassinebenameur @Geril Sorry for the long wait. I couldn't find any time at all to work on OtG in the last for weeks. I believe I have finally resolved this issue however. Please update to v2.1.1 and let me know if this problem still persists.

@mcoenen1994 Your repo was instrumental to debugging this problem. Thank you very much for creating it!

mcoenen1994 commented 4 years ago

Fully working now. Thanks for the fix and keep up the good work!

Geril commented 4 years ago

Works for me as well! Thanks!