OpenAPI

The OpenAPI connector allows you to load remote OpenAPI (2/3.0/3.1) and Swagger schemas that are added to the Edge Gateway inside of the provided namespace.

The OpenAPI connector accepts the following config:

  • name (required) — The unique name for the API
  • url (optional) — The URL used to execute requests*
  • schema — The OpenAPI (2/3) and Swagger schema
  • headers (optional) — The static or forwarded headers sent with requests
  • transforms — Any schema transformations to be applied
* url is optional if the schema contains the server URL.
import { config, connector, g } from '@grafbase/sdk'

const stripe = connector.OpenAPI('Stripe', {
  schema:
    'https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.json',
  headers: headers => {
    headers.set('Authorization', `Bearer ${g.env('STRIPE_API_KEY')}`)
  },
})

g.datasource(stripe)

The schema must follow the OpenAPI Specification (OAS) to connect successfully.

Query Naming

The OpenAPI connector names the generated query fields after their underlying resources. But some OpenAPI schemas are structured around requests & responses rather than resources.

In these cases the generated query field names can be improved using the optional operationId value which is a unique value used to identify an operation.

To enable this you must pass OPERATION_ID.

import { config, connector, g } from '@grafbase/sdk'

const mux = connector.OpenAPI('Mux', {
  schema: 'https://docs.mux.com/api-spec.json',
  headers: headers => {
    headers.set('Authorization', `Basic ${g.env('MUX_BASE64')}`)
  },
  transforms: schema => {
    schema.queryNaming('OPERATION_ID')
  },
})

g.datasource(mux)
Was this page helpful?