Skip to main content

Extensions

Extensions implement additional functionalities to the server's parsing, validation and execution. For example, extensions for tracing (GraphQLTracingExtension), logging (GraphQLLoggingExtension), error handling or caching (GraphQLPersistedQueries and GraphQLCacheExtension). All extension implementations can be found in the extensions folder in package:leto. The main API with all the methods that can be overridden is found in this file.

Persisted Queries

Save network bandwidth by storing GraphQL documents on the server and not requiring the Client to send the full document String on each request.

More information: https://www.apollographql.com/docs/apollo-server/performance/apq/

Source code

Apollo Tracing

Trace the parsing, validation and execution of your GraphQL server to monitor execution times of all GraphQL requests.

More information: https://github.com/apollographql/apollo-tracing

Source code

Response Cache

Utility for caching responses in your GraphQL server and client.

Client GQL Link implementation in: // TODO: 2E

  • Hash: Similar to HTTP If-None-Match and Etag headers. Computes a hash of the payload (sha1 by default) and returns it to the Client when requested. If the Client makes a request with a hash (computed locally or saved from a previous server response), the extension compares the hash and only returns the full body when the hash do not match. If the hash match, the client already has the last version of the payload.

  • MaxAge: If passed a Cache object, it will save the responses and compare the saved date with the current date, if the maxAge para is greater than the difference, it returns the cached value without executing the field's resolver.

  • UpdatedAt: Similar to HTTP If-Modified-Since and Last-Modified headers.

// TODO: 2E retrieve hash, updatedAt and maxAge in resolvers.

Source code

Logging Extension

The logging extension allows you monitor requests and responses executed by your server.

Provides some utilities for printing and retrieving information from execution, logging errors and provides a default GraphQLLog class that contains aggregated information about the request.

Source code

Map Error Extension

Simple extension for mapping an error catched on resolver execution.

With a function that receives the thrown error and some context as parameter and returns a GraphQLException?, this extension will override the error and pass it to the executor, which will eventually return it to the user as an error in the response's errors list.

Source code

Custom Extensions

To create a custom extension you can extend GraphQLExtension and override the necessary functions, all of which are executed throughout a request's parsing, validation and execution.

To save state scoped to a single request you can use the ScopedMap.setScoped(key, value) and retrieve the state in a different method with final value = ScopedMap.get(key);. Where the ScopedMap can be accessed with ctx.globals.

All extensions are implemented in this way, so you can look at the source code for some examples.