graphql-to-openapi icon indicating copy to clipboard operation
graphql-to-openapi copied to clipboard

add support for graphql mutation

Open Ali-iotechsys opened this issue 2 years ago • 7 comments

Hello, thank you for providing this tool. I have tried to generate api from graphql query and mutation, the example is available: link. I have found the tool currently generates api from graphql mutation rendered as GET endpoints, not as POST (or PATCH) as expected. I understand the tool currently can only support geaphql query. It would be nice feature to have mutations supported too. Thanks again for your contribution.

Ali-iotechsys avatar Nov 10 '22 11:11 Ali-iotechsys

Do you have an example schema, mutation and expected openapi output?

schwer avatar Nov 14 '22 01:11 schwer

Thanks for getting back to me. Yes I have example schema and query file (included few mutations): https://github.com/Ali-iotechsys/graphql-to-openapi/tree/dockerise-branch/example

Also, I have added an expected api file in the same directory.

Ali-iotechsys avatar Nov 14 '22 09:11 Ali-iotechsys

Good news: this the only issue in the queue. Bad news: I cannot make any guarantee around completion time estimate 😄 .

schwer avatar Nov 17 '22 01:11 schwer

No worries at all, thanks :)

Ali-iotechsys avatar Nov 17 '22 09:11 Ali-iotechsys

In fact, it almost works :)

The schema, with a Query and a Mutation:

type Query {
  getUser(id: ID!): User!
}

type Mutation {
  saveUser(user: UserInput!): ID
}

type User {
  id: ID!
  name: String!
}

input UserInput {
  name: String!
}

And the queries:

query getUser ($id: ID!) {
  user: getUser(id: $id) { id }
}

mutation saveUser ($user: UserInput!) {
  userId: saveUser(user: $user)
}

Produce the following OpenAPI spec:

#...
paths:
  # query getUser(...)
  /getUser:
    get:
      parameters:
        - name: id  # arguments: ($id: ID!)
          in: query
          required: true
          schema:
            type: string
      responses:
        "200":
          description: response
          content:
            application/json:
              schema:
                type: object
                properties:
                  user:  # Response field. Renamed.
                    nullable: false
                    properties:
                      id:  # Response field
                        type: string
                        nullable: false
                    type: object
  # mutation: saveUser
  /saveUser:
    get:   # GET method. Should be POST
      parameters:  # arguments. In query. Should be in POST body
        - name: user
          in: query
          required: true
          schema:
            type: object
            properties:
              name:
                type: string
                nullable: false

      responses:
        "200":
          description: response
          content:
            application/json:
              schema:
                type: object
                properties:
                  userId:  # response field, renamed
                    type: string
                    nullable: true

It works. With two small issues:

  • the mutation should be a POST :)
  • the parameters should go into the POST body

kolypto avatar Mar 29 '23 13:03 kolypto

Mutations in GraphQL can be add/remove/update operations, the issue (I think) is how to guide the code generation to generate the correct REST API verbs (POST/DELETE/UPDATE or PATCH)?

Ali-iotechsys avatar Mar 29 '23 14:03 Ali-iotechsys

So it's not always obvious whether a mutation should be a POST, PUT or DELETE. One option is to allow the parser to use annotations/directives in the graphql schema (prefixed by@ I think), to indicate the desired method name. Or perhaps a convention in the naming of the operations could be used.

schwer avatar Apr 02 '23 02:04 schwer