openapi-react-query-codegen icon indicating copy to clipboard operation
openapi-react-query-codegen copied to clipboard

How to set cookie/headers when running query/mutation?

Open vinaybedre opened this issue 1 year ago • 4 comments

I really like codegens, which make the life of developers easy.

How ever with this codegen, I was unable to find a way to set cookie/header per query/mutation level. I couldnt find anything in documentation either.

Did someone achieved this?

vinaybedre avatar Apr 17 '24 07:04 vinaybedre

@vinaybedre, one way to achieve that with this package as of now would be to add an interceptor on the OpenAPI object that is exported from requests/core/OpenAPI.ts

In the interceptor, you could check the URL and method and add the header. I haven't tried that though.

It might be nice if we exposed an ability to pass service overrides per hook but that will involve code changes.

seriouslag avatar Apr 17 '24 16:04 seriouslag

I have opened an issue in @hey-api/openapi-ts#465 to add the functionality to override the OpenAPI config per request.

seriouslag avatar Apr 22 '24 03:04 seriouslag

I was able to set headers by specifying them in the swagger definitions:

  /brokers:
    get:
      summary: retrieve the broker mbean
      description: >
        **Get the broker mbean**
          The return value is a one-element array that contains
          the broker's mbean object name. 
          **Example:**
          
          **Request url:** https://localhost:9443/api/v1/brokers
          **Response:**
          ```json
          [
            "org.apache.activemq.artemis:broker=\"amq-broker\""
          ]
          ```
      tags:
        - jolokia
      operationId: getBrokers
      parameters:
        - in: header
          name: jolokia-session-id
          schema:
            type: string
          required: true
        - in: header
          name: jolokia-target-pod-session-id
          schema:
            type: integer
          required: true
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BrokersResponse'

lavocatt avatar Apr 29 '24 16:04 lavocatt

I was able to set headers by specifying them in the swagger definitions:

  /brokers:
    get:
      summary: retrieve the broker mbean
      description: >
        **Get the broker mbean**
          The return value is a one-element array that contains
          the broker's mbean object name. 
          **Example:**
          
          **Request url:** https://localhost:9443/api/v1/brokers
          **Response:**
          ```json
          [
            "org.apache.activemq.artemis:broker=\"amq-broker\""
          ]
          ```
      tags:
        - jolokia
      operationId: getBrokers
      parameters:
        - in: header
          name: jolokia-session-id
          schema:
            type: string
          required: true
        - in: header
          name: jolokia-target-pod-session-id
          schema:
            type: integer
          required: true
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BrokersResponse'

This is the correct way to document the need for the headers!

Thanks for sharing :)

seriouslag avatar Apr 30 '24 23:04 seriouslag