grpcdatasource

package
v2.0.0-rc.181 Latest Latest
Warning

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

Go to latest
Published: May 23, 2025 License: MIT Imports: 17 Imported by: 1

Documentation

Overview

Package grpcdatasource provides a GraphQL datasource implementation for gRPC services. It allows GraphQL servers to connect to gRPC backends and execute RPC calls as part of GraphQL query resolution.

The package includes tools for parsing Protobuf definitions, building execution plans, and converting GraphQL queries into gRPC requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataSource

type DataSource struct {
	// contains filtered or unexported fields
}

DataSource implements the resolve.DataSource interface for gRPC services. It handles the conversion of GraphQL queries to gRPC requests and transforms the responses back to GraphQL format.

func NewDataSource

func NewDataSource(client grpc.ClientConnInterface, config DataSourceConfig) (*DataSource, error)

NewDataSource creates a new gRPC datasource

func (*DataSource) Load

func (d *DataSource) Load(ctx context.Context, input []byte, out *bytes.Buffer) (err error)

Load implements resolve.DataSource interface. It processes the input JSON data to make gRPC calls and writes the response to the output buffer.

The input is expected to contain the necessary information to make a gRPC call, including service name, method name, and request data.

func (*DataSource) LoadWithFiles

func (d *DataSource) LoadWithFiles(ctx context.Context, input []byte, files []*httpclient.FileUpload, out *bytes.Buffer) (err error)

LoadWithFiles implements resolve.DataSource interface. Similar to Load, but handles file uploads if needed.

Note: File uploads are typically not part of gRPC, so this method might not be applicable for most gRPC use cases.

Currently unimplemented.

type DataSourceConfig

type DataSourceConfig struct {
	Operation    *ast.Document
	Definition   *ast.Document
	Compiler     *RPCCompiler
	SubgraphName string
	Mapping      *GRPCMapping
}

type DataType

type DataType string

DataType represents the different types of data that can be stored in a protobuf field.

const (
	DataTypeString  DataType = "string"    // String type
	DataTypeInt32   DataType = "int32"     // 32-bit integer type
	DataTypeInt64   DataType = "int64"     // 64-bit integer type
	DataTypeUint32  DataType = "uint32"    // 32-bit unsigned integer type
	DataTypeUint64  DataType = "uint64"    // 64-bit unsigned integer type
	DataTypeFloat   DataType = "float"     // 32-bit floating point type
	DataTypeDouble  DataType = "double"    // 64-bit floating point type
	DataTypeBool    DataType = "bool"      // Boolean type
	DataTypeEnum    DataType = "enum"      // Enumeration type
	DataTypeMessage DataType = "message"   // Nested message type
	DataTypeUnknown DataType = "<unknown>" // Represents an unknown or unsupported type
	DataTypeBytes   DataType = "bytes"     // Bytes type
)

Protobuf data types supported by the compiler.

func (DataType) IsValid

func (d DataType) IsValid() bool

IsValid checks if the DataType is a valid protobuf type.

func (DataType) String

func (d DataType) String() string

String returns the string representation of the DataType.

type Document

type Document struct {
	Package  string    // The package name of the protobuf document
	Services []Service // All services defined in the document
	Enums    []Enum    // All enums defined in the document
	Messages []Message // All messages defined in the document
	Methods  []Method  // All methods from all services in the document
}

Document represents a compiled protobuf document with all its services, messages, and methods.

func (*Document) EnumByName

func (d *Document) EnumByName(name string) (Enum, bool)

func (*Document) MessageByName

func (d *Document) MessageByName(name string) Message

MessageByName returns a Message by its name. Returns an empty Message if no message with the given name exists.

func (*Document) MessageByRef

func (d *Document) MessageByRef(ref int) Message

MessageByRef returns a Message by its reference index.

func (*Document) MessageRefByName

func (d *Document) MessageRefByName(name string) int

MessageRefByName returns the index of a Message in the Messages slice by its name. Returns -1 if no message with the given name exists.

func (*Document) MethodByName

func (d *Document) MethodByName(name string) Method

MethodByName returns a Method by its name. Returns an empty Method if no method with the given name exists.

func (*Document) MethodByRef

func (d *Document) MethodByRef(ref int) Method

MethodByRef returns a Method by its reference index.

func (*Document) MethodRefByName

func (d *Document) MethodRefByName(name string) int

MethodRefByName returns the index of a Method in the Methods slice by its name. Returns -1 if no method with the given name exists.

func (*Document) ServiceByName

func (d *Document) ServiceByName(name string) Service

ServiceByName returns a Service by its name. Returns an empty Service if no service with the given name exists.

func (*Document) ServiceByRef

func (d *Document) ServiceByRef(ref int) Service

ServiceByRef returns a Service by its reference index.

type EntityRPCConfig

type EntityRPCConfig struct {
	// Key is a list of field names that uniquely identify the entity
	Key string
	// RPCConfig is the embedded configuration for the RPC operation
	RPCConfig
}

EntityRPCConfig defines the configuration for entity lookups

type Enum

type Enum struct {
	Name   string      // The name of the enum
	Values []EnumValue // The values in the enum
}

Enum represents a protobuf enum type with its values.

type EnumValue

type EnumValue struct {
	Name         string // The name of the enum value
	GraphqlValue string // The target value of the enum value
	Number       int32  // The numeric value of the enum value
}

EnumValue represents a single value in a protobuf enum.

type EnumValueMapping

type EnumValueMapping struct {
	Value       string
	TargetValue string
}

type Field

type Field struct {
	Name       string   // The name of the field
	Type       DataType // The data type of the field
	Number     int32    // The field number in the protobuf message
	Ref        int      // Reference to the field (used for complex types)
	Repeated   bool     // Whether the field is a repeated field (array/list)
	Optional   bool     // Whether the field is optional
	MessageRef int      // If the field is a message type, this points to the message definition
}

Field represents a field in a protobuf message.

func (*Field) IsMessage

func (f *Field) IsMessage() bool

func (*Field) ResolveUnderlyingMessage

func (f *Field) ResolveUnderlyingMessage(doc *Document) *Message

type FieldArgumentMap

type FieldArgumentMap map[string]string

FieldArgumentMap defines the mapping between a GraphQL field argument and a gRPC field

type FieldMap

type FieldMap map[string]FieldMapData

FieldMap defines the mapping between a GraphQL field and a gRPC field

type FieldMapData

type FieldMapData struct {
	TargetName       string
	ArgumentMappings FieldArgumentMap
}

type GRPCConfiguration

type GRPCConfiguration struct {
	Mapping  *GRPCMapping
	Compiler *RPCCompiler
}

type GRPCMapping

type GRPCMapping struct {
	// Service is the name of the gRPC service to use
	Service string
	// QueryRPCs maps GraphQL query fields to the corresponding gRPC RPC configurations
	QueryRPCs RPCConfigMap
	// MutationRPCs maps GraphQL mutation fields to the corresponding gRPC RPC configurations
	MutationRPCs RPCConfigMap
	// SubscriptionRPCs maps GraphQL subscription fields to the corresponding gRPC RPC configurations
	SubscriptionRPCs RPCConfigMap
	// EntityRPCs defines how GraphQL types are resolved as entities using specific RPCs
	EntityRPCs map[string]EntityRPCConfig
	// Fields defines the field mappings between GraphQL types and gRPC messages
	Fields map[string]FieldMap
	// EnumValues defines the enum values for each enum type
	EnumValues map[string][]EnumValueMapping
}

func (*GRPCMapping) ResolveEnumValue

func (g *GRPCMapping) ResolveEnumValue(enumName, enumValue string) (string, bool)

func (*GRPCMapping) ResolveFieldArgumentMapping

func (g *GRPCMapping) ResolveFieldArgumentMapping(typeName, fieldName, argumentName string) (string, bool)

func (*GRPCMapping) ResolveFieldMapping

func (g *GRPCMapping) ResolveFieldMapping(typeName string, fieldName string) (string, bool)

ResolveFieldMapping resolves the gRPC field name for a given GraphQL field name and type

type Invocation

type Invocation struct {
	ServiceName string
	MethodName  string
	Input       *dynamicpb.Message
	Output      *dynamicpb.Message
	Call        *RPCCall
}

Invocation represents a single gRPC invocation with its input and output messages.

type Message

type Message struct {
	Name   string                     // The name of the message
	Fields []Field                    // The fields in the message
	Desc   protoref.MessageDescriptor // The protobuf descriptor for the message
}

Message represents a protobuf message type with its fields.

type Method

type Method struct {
	Name       string // The name of the method
	InputName  string // The name of the input message type
	InputRef   int    // Reference to the input message in the Document.Messages slice
	OutputName string // The name of the output message type
	OutputRef  int    // Reference to the output message in the Document.Messages slice
}

Method represents a gRPC method with input and output message types.

type Planner

type Planner struct {
	// contains filtered or unexported fields
}

func NewPlanner

func NewPlanner(subgraphName string, mapping *GRPCMapping) *Planner

NewPlanner returns a new Planner instance.

The planner is responsible for creating an RPCExecutionPlan from a given GraphQL operation. It is used by the engine to execute operations against gRPC services.

func (*Planner) PlanOperation

func (p *Planner) PlanOperation(operation, definition *ast.Document) (*RPCExecutionPlan, error)

type ProtoConfig

type ProtoConfig struct {
	Schema string
}

type RPCCall

type RPCCall struct {
	// CallID is the unique identifier for the call
	CallID int
	// DependentCalls is a list of calls that must be executed before this call
	DependentCalls []int
	// ServiceName is the name of the gRPC service to call
	ServiceName string
	// MethodName is the name of the method on the service to call
	MethodName string
	// Request contains the message structure for the gRPC request
	Request RPCMessage
	// Response contains the message structure for the gRPC response
	Response RPCMessage
}

RPCCall represents a single call to a gRPC service method. It contains all the information needed to make the call and process the response.

type RPCCompiler

type RPCCompiler struct {
	Ancestor []Message
	// contains filtered or unexported fields
}

RPCCompiler compiles protobuf schema strings into a Document and can build protobuf messages from JSON data based on the schema.

func NewProtoCompiler

func NewProtoCompiler(schema string, mapping *GRPCMapping) (*RPCCompiler, error)

NewProtoCompiler compiles the protobuf schema into a Document structure. It extracts information about services, methods, messages, and enums from the protobuf schema.

func (*RPCCompiler) Compile

func (p *RPCCompiler) Compile(executionPlan *RPCExecutionPlan, inputData gjson.Result) ([]Invocation, error)

Compile processes an RPCExecutionPlan and builds protobuf messages from JSON data based on the compiled schema.

func (*RPCCompiler) ConstructExecutionPlan

func (p *RPCCompiler) ConstructExecutionPlan(operation, schema *ast.Document) (*RPCExecutionPlan, error)

ConstructExecutionPlan constructs an RPCExecutionPlan from a parsed GraphQL operation and schema. It will return an error if the operation does not match the protobuf definition provided to the compiler.

type RPCConfig

type RPCConfig struct {
	// RPC is the name of the RPC method to call
	RPC string
	// Request is the name of the request message type
	Request string
	// Response is the name of the response message type
	Response string
}

RPCConfig defines the configuration for a specific RPC operation

type RPCConfigMap

type RPCConfigMap map[string]RPCConfig

RPCConfigMap is a map of RPC names to RPC configurations

type RPCExecutionPlan

type RPCExecutionPlan struct {
	// Calls is a list of gRPC calls that are executed in the same group
	Calls []RPCCall
}

RPCExecutionPlan represents a plan for executing one or more RPC calls to gRPC services. It defines the sequence of calls and their dependencies.

func (*RPCExecutionPlan) String

func (r *RPCExecutionPlan) String() string

type RPCField

type RPCField struct {
	// Alias can be used to rename the field in the request message
	// This is needed to make sure that during the json composition,
	// the field names match the GraphQL request naming.
	// TODO implement alias handling
	Alias string
	// Repeated indicates if the field is a repeated field (array/list)
	Repeated bool
	// Name is the name of the field as defined in the protobuf message
	Name string
	// TypeName is the name of the type of the field in the protobuf definition
	TypeName string
	// JSONPath defines the path within the variables to provide the value for the field
	// This is used to extract data from the GraphQL variables
	JSONPath string
	// EnumName is the name of the enum if the field is an enum type
	EnumName string
	// StaticValue is the static value of the field
	StaticValue string

	// Message is the message type if the field is a nested message type
	// This allows for recursive construction of complex message types
	Message *RPCMessage
}

RPCField represents a single field in a gRPC message. It contains all information required to extract data from GraphQL variables and construct the appropriate protobuf field.

type RPCFields

type RPCFields []RPCField

RPCFields is a list of RPCFields that provides helper methods for working with collections of fields.

func (RPCFields) ByName

func (r RPCFields) ByName(name string) *RPCField

ByName returns a field by its name from the collection of fields. Returns nil if no field with the given name exists.

func (RPCFields) Exists

func (r RPCFields) Exists(name string) bool

type RPCMessage

type RPCMessage struct {
	// Name is the name of the message type in the protobuf definition
	Name string
	// Fields is a list of fields in the message
	Fields RPCFields
	// OneOf indicates if the message is an interface
	OneOf bool
}

RPCMessage represents a gRPC message structure for requests and responses. It defines the structure of the message including all its fields.

type Service

type Service struct {
	Name        string // The name of the service
	FullName    string // The full name of the service
	MethodsRefs []int  // References to methods in the Document.Methods slice
}

Service represents a gRPC service with methods.

Jump to

Keyboard shortcuts

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