xetra11
xetra11

Reputation: 8895

Google API Gateway configuration for a Cloud Run Server that serves GraphQL at endpoint /api

I have a running backend server that serves GraphQL on endpoint /api. Works like a charme but I still have it externally open and access is possible without authentication.

enter image description here

So I started trying the API Gateway beta Google just released. All that was pretty straight forward expcept the part where I have to configure the OpenAPI 2.0 configuration YAML.

swagger: "2.0"
info:
  title: myapp-backend-graphql-api
  description: "API for GraphQL queries and mutations"
  version: "1.0.0"
schemes:
  - "https"
paths:
  "/api":
    post:
      description: "GraphQL Endpoint"
      operationId: "graphqlEndpoint"
      x-google-backend:
        address: https://myapp-backend-43Jasfasd-ew.a.run.app
      parameters:
        - in: body
          name: GraphQl
          schema:
            type: object
      responses:
        200:
          description: "Success."
          schema:
            type: object


So this is my openapi-config.yml. I somewhat struggled a b


it defining the "REST" endpoint here since I have a GraphQL endpoint. But it's just a POST request, right? So nothing usual for OpenAPI 2.0 I thought.

Anyways the API Gateway was created successfully according to GCP enter image description here

However - calling the API now via the Gateway URL https://mystuff-api-9kytelt5.ew.gateway.dev and the endpoint /api fails.

xetra11@pop-os:~$ curl https://mystuff-api-9kytelt5.ew.gateway.dev/api/schema.json 
{"message":"Path does not match any requirement URI template.","code":404}

I am somewhat lost with that error message and don't know where I have to adjust something.

Upvotes: 2

Views: 1311

Answers (2)

MaryM
MaryM

Reputation: 172

In your openapi-config.yml file, like the example here, you need to add the host just above the schemes entry. The host is the hostname portion of the URL that Cloud Run creates. Also your path and scheme have double quotes which it doesn't need to be. I made some modification to your example, see if that helps.

Example:

swagger: "2.0"
info:
  title: myapp-backend-graphql-api
  description: "API for GraphQL queries and mutations"
  version: "1.0.0"
host: myapp-backend-43Jasfasd-ew.a.run.app
schemes:
  - https
paths:
  /api:
    post:
      description: "GraphQL Endpoint"
      operationId: "graphqlEndpoint"
      x-google-backend:
        address: https://myapp-backend-43Jasfasd-ew.a.run.app
      parameters:
        - in: body
          name: GraphQl
          schema:
            type: object
      responses:
        '200':
          description: Success
          schema:
            type: object

Upvotes: 1

guillaume blaquiere
guillaume blaquiere

Reputation: 76073

You only defined the POST verb in your openAPI spec. add a -X POST to your curl command to change the default GET verb to POST

Upvotes: 0

Related Questions