gRPC resolvers
Define schema in a gRPC ApiDoc
, define gRPC resolvers in your Gloo environment to resolve schema fields, and map schema to resolvers.
Create gRPC resources
Create the ApiDoc
, GraphQLResolverMap
, and GraphQLSchema
Gloo CRs.
Step 1: gRPC schema
Start by defining your schema in an ApiDoc
Gloo custom resource (CR).
Define the protobuf descriptors that represent the gRPC services provided by your API, encoded in base64. For more information, see the protobuf reference for FileDescriptorSet
.
Step 2: gRPC resolvers
Define gRPC resolver servers for each query type in a GraphQLResolverMap
Gloo CR.
Optional: Define variables for use in resolvers
You can define variables that you want to use in the variables
section of a resolver in the resolver map. For example, you might define variables for the following uses:
- Schema arguments: Define variables to retrieve arguments defined in your schema.
- Authorization: Define common authorization variables, such as
requestHeader: "Authorization"
. jq
filters: Define variables in this section that you want to use injq
filters for the resolver request or result. For more information, see Applyingjq
filters.
Note that some variables have defined names and guidelines for values. For more information about each type of variable, see the API documentation for resolver variables.
For example, you might define the following variables for the Reviews
service:
...
Product:
fields:
reviews:
variables:
parentVar:
graphqlParent: {}
authorizationVar:
requestHeader: "Authorization"
In the same resolver map resource, you can then call these variables in the resolver request headers or body, such as the following headers. Note that you can also specify individual values that are not declared in variables.
...
resolvers:
- restResolver:
destinations:
...
request:
headers:
Authorization:
variable: authorizationVar
:path:
variable: parentVar
value: "[1,2,3]"
Optional: Apply jq filters
To transform the JSON content of a GraphQL resolver request or result, you can provide jq filters in your GraphQL resolver map.
You can define variables that you want to use in the jq transformation filter in the variables
section of a resolver in the resolver map. For example, a variable named “userIdHeader” can be used in a jq filter as .userIdHeader
. Then, you can apply jq filters to resolver requests (resolver.restResolver.request
) or results (resolver.restResolver.resolverResultTransform
) that call these variables.
For more information, see the API documentation for jq
transformation. For information about jq syntax and how to construct your jq filter, see the jq development manual.
For example, you might define the following variables for the Reviews
service:
...
Product:
fields:
reviews:
variables:
parentVar:
graphqlParent: {}
authorizationVar:
requestHeader: "Authorization"
You can then call these variables in the resolver request headers or body:
...
resolvers:
- restResolver:
destinations:
...
request:
headers:
:path:
jq: '"/reviews/" + .parentVar.id'
customAuth:
jq: '"Bearer " + .authorizationVar'
You can also use variables in jq filters for resolver results. For example, if the data that the resolver fetched from your upstream API is not formatted in a way that the client can understand according to your schemas, you can apply a jq filter to the result data.
The fetched data from your API might be formatted like the following:
{ "userIdHeader": "john_doe123", "resolverResultVar": { "data": {"name": "John Doe"} } }
First, you define the following variables.
...
variables:
userIdHeader:
requestHeader: x-user-id
resolverResultVar:
resolverResult: {}
You then call those variables in a resolver.resolverResultTransform
jq filter. Note that the resolverResultVar
variable can only be used in the resolverResultTransform
field, and not in a request field.
...
resolvers:
- restResolver:
destinations:
...
request:
...
resolverResultTransform:
jq: '"User: " + .userIdHeader + ", Name: " + .resolverResultVar.data.name'
The result of the jq transformation is as follows:
"User: john_doe123, Name: John Doe"
jq filters must only result in one value. For example, for the input [1,2,3]
, the jq filter .[]
is not a valid jq filter as it results in multiple jq results. However, the jq filter '. | join(",")'
is valid as it results in only one result: “1,2,3”. Refer to the jq manual for jq syntax and tips.
Step 3: Map schema to resolvers
Map the types and fields from your schema definition (ApiDoc
) to the resolver servers (GraphQLResolverMap
) in a GraphQLSchema
Gloo CR. The GraphQLSchema
CR ensures that the GraphQL resolver services can access the field information for each type.
Reference
For more information, see the Gloo Mesh Gateway API reference for the following CRs: