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
- type CallKind
- type DataSource
- type DataSourceConfig
- type DataType
- type DependencyGraph
- func (g *DependencyGraph) Fetch(index int) (FetchItem, error)
- func (g *DependencyGraph) FetchDependencies(fetch *FetchItem) ([]FetchItem, error)
- func (g *DependencyGraph) SetFetchData(index int, serviceCall *ServiceCall)
- func (g *DependencyGraph) TopologicalSortResolve(resolver func(nodes []FetchItem) error) error
- type Document
- type EntityRPCConfig
- type Enum
- type EnumValue
- type EnumValueMapping
- type FetchItem
- type Field
- type FieldArgumentMap
- type FieldMap
- type FieldMapData
- type GRPCConfiguration
- type GRPCMapping
- func (g *GRPCMapping) FindEntityRPCConfig(typeName, key string) (RPCConfig, bool)
- func (g *GRPCMapping) FindEnumValueMapping(enumName, enumValue string) (string, bool)
- func (g *GRPCMapping) FindFieldArgumentMapping(typeName, fieldName, argumentName string) (string, bool)
- func (g *GRPCMapping) FindFieldMapping(typeName string, fieldName string) (string, bool)
- func (g *GRPCMapping) FindResolveTypeFieldMapping(typeName, fieldName string) *ResolveRPCTypeField
- type LevelInfo
- type ListMetadata
- type Message
- type Method
- type NodeKind
- type OneOfType
- type PlanVisitor
- type ProtoConfig
- type RPCCall
- type RPCCompiler
- type RPCConfig
- type RPCConfigMap
- type RPCExecutionPlan
- type RPCField
- type RPCFieldSelectionSet
- type RPCFields
- type RPCMapping
- type RPCMessage
- type ResolveRPCMapping
- type ResolveRPCTypeField
- type Service
- type ServiceCall
Constants ¶
const (
// InvalidRef is a constant used to indicate that a reference is invalid.
InvalidRef = -1
)
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 ¶
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
FederationConfigs plan.FederationFieldConfigurations
Disabled bool
}
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.
type DependencyGraph ¶
type DependencyGraph struct {
// contains filtered or unexported fields
}
DependencyGraph is a graph of the calls in the execution plan. It is used to determine the order in which to execute the calls.
func NewDependencyGraph ¶
func NewDependencyGraph(executionPlan *RPCExecutionPlan) *DependencyGraph
func (*DependencyGraph) Fetch ¶
func (g *DependencyGraph) Fetch(index int) (FetchItem, error)
Fetch returns the fetch item for a given index.
func (*DependencyGraph) FetchDependencies ¶
func (g *DependencyGraph) FetchDependencies(fetch *FetchItem) ([]FetchItem, error)
FetchDependencies returns the dependencies for a given fetch item.
func (*DependencyGraph) SetFetchData ¶
func (g *DependencyGraph) SetFetchData(index int, serviceCall *ServiceCall)
SetFetchData sets the service call for a given index.
func (*DependencyGraph) TopologicalSortResolve ¶
func (g *DependencyGraph) TopologicalSortResolve(resolver func(nodes []FetchItem) error) error
TopologicalSortResolve sorts the calls in the execution plan in a topological order. In order to perform calls in the correct order, we need to determine the dependencies between the calls. We are using a depth-first search to determine the dependencies between the calls by building an index map of the call hierarchy. Each index in the index map corresponds to a call index in the execution plan. The map contains the level of the call in the hierarchy. The root call has a level of 0.
type Document ¶
type Document struct {
Package string // The package name of the protobuf document
Imports []string // The imports 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
// contains filtered or unexported fields
}
Document represents a compiled protobuf document with all its services, messages, and methods.
func (*Document) EnumByName ¶
EnumByName returns an Enum by its name. Returns false if the enum does not exist.
func (*Document) MessageByName ¶
MessageByName returns a Message by its name. Returns an empty Message if no message with the given name exists. We only expect this function to return false if either the message name was provided incorrectly, or the schema and mapping was not properly configured.
func (*Document) MessageByRef ¶
MessageByRef returns a Message by its reference index.
func (*Document) MessageRefByName ¶
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) ServiceByName ¶
ServiceByName returns a Service by its name. Returns an empty Service if no service with the given name exists.
type EntityRPCConfig ¶
type EntityRPCConfig struct {
// RPCConfig is the embedded configuration for the RPC operation
RPCConfig
// Key is a list of field names that uniquely identify the entity
Key string
}
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 // The GraphQL enum value
TargetValue string // The gRPC enum value
}
EnumValueMapping defines the mapping between a GraphQL enum value and a gRPC enum value
type FetchItem ¶
type FetchItem struct {
ID int
Plan *RPCCall
ServiceCall *ServiceCall
DependentFetches []int
}
FetchItem is a single fetch item in the execution plan. It contains the call information, the service call, and a list of references to the dependent fetches.
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) ResolveUnderlyingMessage ¶
type FieldArgumentMap ¶
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 The key is the field name in the GraphQL type. The value is the FieldMapData for that field which contains the target name and the argument mappings.
type FieldMapData ¶
type FieldMapData struct {
TargetName string // The name of the gRPC field
ArgumentMappings FieldArgumentMap // The mapping between GraphQL field arguments and gRPC request arguments
}
FieldMapData defines the mapping between a GraphQL field and a gRPC field
type GRPCConfiguration ¶
type GRPCConfiguration struct {
Disabled bool // Whether the RPC is disabled
Mapping *GRPCMapping // The mapping between GraphQL types and gRPC messages
Compiler *RPCCompiler // The compiler for the RPC
}
GRPCConfiguration defines the configuration for a gRPC datasource
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[RPCConfig]
// MutationRPCs maps GraphQL mutation fields to the corresponding gRPC RPC configurations
MutationRPCs RPCConfigMap[RPCConfig]
// SubscriptionRPCs maps GraphQL subscription fields to the corresponding gRPC RPC configurations
SubscriptionRPCs RPCConfigMap[RPCConfig]
// ResolveRPCs maps GraphQL resolve fields to the corresponding gRPC RPC configurations
ResolveRPCs RPCConfigMap[ResolveRPCMapping]
// EntityRPCs defines how GraphQL types are resolved as entities using specific RPCs
// The key is the type name and the value is a list of EntityRPCConfig for that type
EntityRPCs map[string][]EntityRPCConfig
// Fields defines the field mappings between GraphQL types and gRPC messages
// The key is the type name and the value is a map of field name to FieldMapData for that type
Fields map[string]FieldMap
// EnumValues defines the enum values for each enum type
// The key is the enum type name and the value is a list of EnumValueMapping for that enum type
EnumValues map[string][]EnumValueMapping
}
func (*GRPCMapping) FindEntityRPCConfig ¶
func (g *GRPCMapping) FindEntityRPCConfig(typeName, key string) (RPCConfig, bool)
FindEntityRPCConfig finds the gRPC RPC config for a given GraphQL type and key
func (*GRPCMapping) FindEnumValueMapping ¶
func (g *GRPCMapping) FindEnumValueMapping(enumName, enumValue string) (string, bool)
FindEnumValueMapping finds the gRPC enum value for a given GraphQL enum value
func (*GRPCMapping) FindFieldArgumentMapping ¶
func (g *GRPCMapping) FindFieldArgumentMapping(typeName, fieldName, argumentName string) (string, bool)
FindFieldArgumentMapping finds the gRPC field name for a given GraphQL field name and argument name
func (*GRPCMapping) FindFieldMapping ¶
func (g *GRPCMapping) FindFieldMapping(typeName string, fieldName string) (string, bool)
FindFieldMapping finds the gRPC field name for a given GraphQL field name and type
func (*GRPCMapping) FindResolveTypeFieldMapping ¶
func (g *GRPCMapping) FindResolveTypeFieldMapping(typeName, fieldName string) *ResolveRPCTypeField
FindResolveTypeFieldMapping finds the gRPC field name for a given GraphQL field name and type
type LevelInfo ¶
type LevelInfo struct {
// Optional indicates if the field is optional
Optional bool
}
LevelInfo contains the metadata for the list type
type ListMetadata ¶
type ListMetadata struct {
// NestingLevel is the nesting level of the list type
NestingLevel int
// LevelInfo contains the metadata for each nesting level of the list
LevelInfo []LevelInfo
}
ListMetadata contains the metadata for the list type
type Message ¶
type Message struct {
Fields map[uint64]Field
Name string // The name of the message
Desc protoref.MessageDescriptor // The protobuf descriptor for the message
}
Message represents a protobuf message type with its fields.
func (*Message) AllocFields ¶
AllocFields allocates a new map of fields with the given count.
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 OneOfType ¶
type OneOfType uint8
OneOfType represents the type of a oneof field in a protobuf message. It can be either an interface or a union type.
const ( // OneOfTypeNone represents no oneof type (default/zero value) OneOfTypeNone OneOfType = iota // OneOfTypeInterface represents an interface type oneof field OneOfTypeInterface // OneOfTypeUnion represents a union type oneof field OneOfTypeUnion )
OneOfType constants define the different types of oneof fields.
type PlanVisitor ¶
type PlanVisitor interface {
PlanOperation(operation, definition *ast.Document) (*RPCExecutionPlan, error)
}
func NewPlanner ¶
func NewPlanner(subgraphName string, mapping *GRPCMapping, federationConfigs plan.FederationFieldConfigurations) (PlanVisitor, error)
NewPlanner returns a new PlanVisitor 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.
type ProtoConfig ¶
type ProtoConfig struct {
Schema string
}
type RPCCall ¶
type RPCCall struct {
// Kind of call, used to decide how to execute the call
// This is used to identify the call type and execution behaviour.
Kind CallKind
// 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
// ResponsePath is the path to the response in the JSON response
ResponsePath ast.Path
}
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) CompileFetches ¶
func (p *RPCCompiler) CompileFetches(graph *DependencyGraph, fetches []FetchItem, inputData gjson.Result) ([]ServiceCall, error)
func (*RPCCompiler) CompileNode ¶
func (p *RPCCompiler) CompileNode(graph *DependencyGraph, fetch FetchItem, inputData gjson.Result) (ServiceCall, error)
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[T RPCMapping] map[string]T
RPCConfigMap is a map of RPC names to RPC configurations The key is the field name in the GraphQL operation type (query, mutation, subscription). The value is the RPC configuration for that field.
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.
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
// ProtoTypeName is the name of the type of the field in the protobuf definition
ProtoTypeName DataType
// JSONPath either holds the path to the variable definition for the request message,
// or defines the name of the response field in the message.
JSONPath string
// ResolvePath is used to resolve values from another message.
ResolvePath ast.Path
// 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
// Optional indicates if the field is optional
Optional bool
// IsListType indicates if the field is a list wrapper type
IsListType bool
// ListMetadata contains the metadata for the list type
ListMetadata *ListMetadata
// Message represents the nested message type definition for complex fields.
// This enables recursive construction of nested protobuf message structures.
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.
func (*RPCField) AliasOrPath ¶
AliasOrPath returns the alias of the field if it exists, otherwise it returns the JSONPath.
func (*RPCField) IsOptionalScalar ¶
IsOptionalScalar checks if the field is an optional scalar value.
func (*RPCField) ToOptionalTypeMessage ¶
func (r *RPCField) ToOptionalTypeMessage(protoName string) *RPCMessage
ToOptionalTypeMessage returns a message that wraps the scalar value in a message as protobuf scalar types are not nullable.
type RPCFieldSelectionSet ¶
RPCFieldSelectionSet is a map of field selections based on inline fragments
func (RPCFieldSelectionSet) Add ¶
func (r RPCFieldSelectionSet) Add(fragmentName string, field RPCField)
Add adds a field selection set to the map
func (RPCFieldSelectionSet) SelectFieldsForTypes ¶
func (r RPCFieldSelectionSet) SelectFieldsForTypes(validTypes []string) RPCFields
SelectFieldsForTypes returns the fields for the given valid types. It also makes sure to deduplicate the fields.
type RPCFields ¶
type RPCFields []RPCField
RPCFields is a list of RPCFields that provides helper methods for working with collections of fields.
type RPCMapping ¶
type RPCMapping interface {
RPCConfig | ResolveRPCMapping
}
LookupFieldMapDefinition defines the mapping between a GraphQL field and a gRPC field
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
// FieldSelectionSet are field selections based on inline fragments
FieldSelectionSet RPCFieldSelectionSet
// OneOfType indicates the type of the oneof field
OneOfType OneOfType
// MemberTypes provides the names of the types that are implemented by the Interface or Union
MemberTypes []string
// Message represents the nested message type definition for complex fields.
// This enables recursive construction of nested protobuf message structures.
Message *RPCMessage
}
RPCMessage represents a gRPC message structure for requests and responses. It defines the structure of the message including all its fields.
func (*RPCMessage) AppendTypeNameField ¶
func (r *RPCMessage) AppendTypeNameField(typeName string)
AppendTypeNameField appends a typename field to the message.
func (*RPCMessage) IsOneOf ¶
func (r *RPCMessage) IsOneOf() bool
IsOneOf checks if the message is a oneof field.
func (*RPCMessage) SelectValidTypes ¶
func (r *RPCMessage) SelectValidTypes(typeName string) []string
SelectValidTypes returns the valid types for a given type name.
type ResolveRPCMapping ¶
type ResolveRPCMapping map[string]ResolveRPCTypeField
ResolveRPCMapping defines the mapping between a GraphQL field resolver and a gRPC RPC configuration
type ResolveRPCTypeField ¶
type ResolveRPCTypeField struct {
FieldMappingData FieldMapData // The mapping between GraphQL field arguments and gRPC request arguments
RPC string // The name of the RPC method to call
Request string // The name of the request message type
Response string // The name of the response message type
}
ResolveRPCTypeField defines the mapping between a GraphQL resolve field and a gRPC RPC configuration
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.
type ServiceCall ¶
type ServiceCall struct {
// 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
// Input is the input message for the gRPC call
Input protoref.Message
// Output is the output message for the gRPC call
Output protoref.Message
// RPC is the call that was made to the gRPC service
RPC *RPCCall
}
ServiceCall represents a single gRPC service call with its input and output messages.
func (*ServiceCall) MethodFullName ¶
func (s *ServiceCall) MethodFullName() string