Map Types
Protocol Buffers supports map fields (key-value pairs) which are represented as map<key_type, value_type> in .proto files. In this plugin, map types are handled using specialized GraphQL types that preserve the key-value structure while ensuring type safety.
Map Type Representation
When converting Protocol Buffer map types to GraphQL:
- Each map type is represented as an array of entry objects
- Each entry object contains a
keyand avaluefield - Separate types are generated for both input and output contexts
Map Entry Type Naming
The plugin follows specific naming conventions for map entry types based on the field type:
For scalar value types:
- Map entry object types (output types):
{ScalarTypeName}MapEntry - Map entry input types:
{ScalarTypeName}MapEntryInput
For message or enum value types:
- Map entry object types (output types):
{MapKeyType}_{DescriptorName}MapEntry - Map entry input types:
{MapKeyType}_{DescriptorName}MapEntryInput
Where:
ScalarTypeNameis the Pascal-cased GraphQL scalar type nameMapKeyTypeis the scalar type used for the map key (e.g., String, Int)DescriptorNameis determined by the following rules:- If the message/enum has a parent:
{PascalCasedParentTypeName}_{PascalCasedMessageOrEnumName} - If the message/enum has no parent:
{PascalCasedTypeName}
- If the message/enum has a parent:
Map Entry Types
Each map entry is represented as an object with:
key: The key type from the Protocol Buffer mapvalue: The value type from the Protocol Buffer map
These types are used when returning map data in queries and mutations.
Example
Given a Protocol Buffer message with a map:
message User {
string id = 1;
map<string, string> attributes = 2;
map<string, Address> addresses = 3;
}
message Address {
string street = 1;
string city = 2;
}The plugin will generate:
- A
StringMapEntryobject type for theattributesfield (since it uses string values) - A
StringMapEntryInputinput type for theattributesfield - A
String_AddressMapEntryobject type for theaddressesfield (since it uses message values with string keys) - A
String_AddressMapEntryInputinput type for theaddressesfield - The
Usertype will have the fields properly typed as arrays of these entry types
This approach allows for typed key-value operations while maintaining compatibility with GraphQL's type system.