conv

package module
v0.0.0-...-dcc0d81 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

OpenAPI to GraphQL Converter

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

Go Version CI Status Go Report Card

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(spec []byte, opts ConvertOptions) (string, error)

Convert transforms a DUH-RPC OpenAPI spec to GraphQL SDL. IMPORTANT: spec MUST pass DUH-RPC validation before calling Convert. If spec is invalid, Convert will return error.

Types

type ConvertOptions

type ConvertOptions struct {
	// HeuristicPatterns are method name patterns that indicate query operations.
	// Default: ["get", "list", "search", "find", "fetch", "query", "retrieve"]
	HeuristicPatterns []string
}

ConvertOptions configures the conversion behavior.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址