OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.36k stars 6.46k forks source link

[GraphQL] Graphql support #570

Closed eddiecooro closed 5 years ago

eddiecooro commented 6 years ago
Description

Adding support for graphql

Do you have any plan for a graphql generator or something similar? It would be nice if we can generate both rest and graphql endpoints from one specification.

jmini commented 6 years ago

This was discussed, but for the moment the templates and the model are really close to OpenAPI. Supporting multiple input formats requires a lot of refactoring... This is really long term vision.

cc: @jimschubert

eddiecooro commented 6 years ago

Hi @jmini thankyou for reply

What do you mean by

Supporting multiple "input" formats

wing328 commented 6 years ago

@Eddie-CooRo I think @jmini is referring the input of OpenAPI Generator, which currently supports OpenAPI spec at the moment.

wing328 commented 6 years ago

@Eddie-CooRo as discussed, here is the high-level project plan:

I can probably start creating something tomorrow for you to try it out. Stay tuned.

wing328 commented 6 years ago

I've cc'd @yarax who is the owner of https://github.com/yarax/swagger-to-graphql to see if we can collaborate on this.

eddiecooro commented 6 years ago

Nice I am going to implement a sample graphql server in nodejs for given endpoints. So which library you think we should use? pure graphql-js or graphql-yoga(maintained by Prisma). I think graphql-yoga is a better choice because we can make use of

  1. file uploads (pure graphql doesn't support it but there is a "file" type in the oas)
  2. TypeScript typings
  3. Schema directives
  4. and with the help of graphql-tools we can have schema completely similar to Graphql schema language

@wing328 @yarax

wing328 commented 6 years ago

I've filed https://github.com/OpenAPITools/openapi-generator/pull/609 to create the 1st version of the GraphQL client generator. The Petstore samples can be found in https://github.com/OpenAPITools/openapi-generator/tree/af38f807f05cc6042187f4a50fe7e64597b75320/samples/client/petstore/graphql-client

Please test and comment on the code on what needs to be fixed.

wing328 commented 6 years ago

I think graphql-yoga is a better choice because we can make use of

My suggestion is to start with whatever you feel comfortable. My view is that it's just a matter of time someone asks for graphql-js later

spawn-guy commented 6 years ago

@wing328 or VulcanJS :)

renepardon commented 5 years ago

Hi!

I'd like to start a new project with documentation of type OAS3.0.0 first and then generate the GraphQL Server and possibly client out of it. I think I will work with next.js and apollo.

So what's the current state here? How can I help to get this finished? @wing328

wing328 commented 5 years ago

@renepardon thanks for offering help to move this forward. Did you have a chance to review the output in https://github.com/OpenAPITools/openapi-generator/tree/graphql_client/samples/config/petstore/graphql? Does it look good to you?

renepardon commented 5 years ago

Hi @wing328

not yet - can't get the current master building the whole stuff... Maybe it fails because of my JAVA-version? I would also start with a GraphQL server (based on express for simplicity)

$ java --version
java 10.0.2 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
renepardon commented 5 years ago

At first glance the output looks good.

wing328 commented 5 years ago

Let me merge the master into this branch so that you can build it with JDK10. Stay tuned.

renepardon commented 5 years ago

It looks like the branch is already up to date but my problem was that I'm also not able to build the master branch.

wing328 commented 5 years ago

Can you ping me via https://gitter.im (ID:wing328) so that I can help you resolve the build issue?

renepardon commented 5 years ago

I would create a separate generator as „server generator“ and name it graphql-express-server which will output the types, queries, mutations and also the resolvers.

For the resolvers we may also adjust/use a custom field within the path definitions of the OAS config because my intention is to add a „microservice URI“ for each path so the resolver knows where to fetch the data from. We could use the "summary" or "description" field if no custom field is possible or even "tags".

Example path:

paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - http://pets.example.com
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"

cc @jmini @Eddie-CooRo

renepardon commented 5 years ago

The current state is available here: https://github.com/renepardon/openapi-generator/tree/graphql_client/samples/server/petstore/graphql-server/openapi3graphql-server

But there is a problem with the types so the server won't start. A discussion about this has been started here: https://spectrum.chat/graphql/general?thread=9ee3d211-566d-4250-849c-2f8e4b42268c

renepardon commented 5 years ago

I've adjusted the existing GraphQLSchemaGenerator class and added an AbstractGraphQLCodegen class as well.

Feedback is welcome @jmini @Eddie-CooRo @yarax

renepardon commented 5 years ago

https://github.com/OpenAPITools/openapi-generator/pull/1451

wing328 commented 5 years ago

We've added the GraphQL generators via https://github.com/OpenAPITools/openapi-generator/pull/1603. Please pull the latest to give it a try.