OpenAPI to GraphQL Converter
A Go library that converts OpenAPI 3.0 schema definitions to GraphQL (gql) Schema Definition Language (SDL).

Features
- ✅ Query operation detection with configurable heuristics
- ✅ Comprehensive type mapping (primitives, objects, arrays, enums)
- ✅ Custom GraphQL scalars (DateTime, UUID, Int64)
- ✅ Name sanitization for GraphQL identifier compatibility
- ✅ OpenAPI extension support for fine-grained control
- ✅ Detailed error messages with actionable suggestions
- ✅ DUH-RPC compliance validation
Installation
go get github.com/duh-rpc/openapi-gql.go
Quick Start
package main
import (
"fmt"
"log"
"os"
conv "github.com/duh-rpc/openapi-gql.go"
)
func main() {
// Load OpenAPI specification
data, err := os.ReadFile("openapi.yaml")
if err != nil {
log.Fatal(err)
}
// Convert to GraphQL SDL using default options
sdl, err := conv.Convert(data, conv.ConvertOptions{})
if err != nil {
log.Fatal(err)
}
fmt.Println(sdl)
}
Custom Heuristic Patterns
By default, the converter detects query operations using these method name patterns:
get, list, search, find, fetch, query, retrieve
You can customize this behavior:
opts := conv.ConvertOptions{
HeuristicPatterns: []string{"get", "list", "find"},
}
sdl, err := conv.Convert(data, opts)
if err != nil {
log.Fatal(err)
}
OpenAPI Extensions
The converter supports OpenAPI extensions for fine-grained control:
x-graphql-query (boolean)
Force include or exclude an operation from GraphQL query generation:
paths:
/v1/users.delete:
post:
x-graphql-query: true # Include delete operation in Query type
# ...
/v1/users.get:
post:
x-graphql-query: false # Exclude get operation from Query type
# ...
x-graphql-field-name (string)
Override the generated GraphQL field name:
paths:
/v1/users.get:
post:
x-graphql-field-name: "user" # Use "user" instead of "usersGet"
# ...
Type Mapping
The library maps OpenAPI types to GraphQL types according to the following rules:
| OpenAPI Type |
OpenAPI Format |
GraphQL Type |
string |
- |
String |
string |
date-time |
DateTime (custom scalar) |
string |
uuid |
UUID (custom scalar) |
integer |
- |
Int |
integer |
int64 |
Int64 (custom scalar) |
number |
- |
Float |
boolean |
- |
Boolean |
object |
- |
GraphQL type/input |
array |
- |
[Type!]! |
enum |
- |
GraphQL enum (SCREAMING_SNAKE_CASE) |
Custom Scalars
The following custom scalars are automatically declared when used:
scalar DateTime # RFC3339 format
scalar UUID # RFC4122 format
scalar Int64 # 64-bit integer
Requirements
- Go 1.24+
- DUH-RPC compliant OpenAPI 3.0 specification
DUH-RPC Compliance
This library requires OpenAPI specifications that follow the DUH-RPC conventions:
- All operations must use POST method
- Paths follow
/v{version}/{subject}.{method} pattern
- Request/response bodies use
application/json content type
- Schema references use
#/components/schemas/ format
Run duh lint on your OpenAPI specification to ensure compliance before conversion.
Known Limitations
The following OpenAPI constructs are not supported in v0.1.0:
oneOf, anyOf, allOf (use simple object types instead)
- Mutation and subscription operations (query-only)
- Non-DUH-RPC OpenAPI specifications
- Cursor-based pagination
These limitations are documented in error messages when encountered.
Error Handling
The library provides detailed error messages with context and suggestions:
Error in validation stage: Invalid x-graphql-field-name "user name"
Path: /v1/users.get
Reason: Contains invalid character (space)
Suggestion: Use valid GraphQL identifier (e.g., "userName")
- duh-cli: Command-line tool that uses this library for GraphQL generation
- openapi-proto.go: Sister library for Protocol Buffers generation