Client ID Enforcement
Require client details and operation names to help monitor schema usage
As part of GraphOS Studio metrics reporting, servers can tag reported operations with the requesting client's name and version. This helps graph maintainers understand which clients are using which fields in the schema.
Clients can (and should) also name their GraphQL operations, which provides more context around how and where data is being used.
Together, these pieces of information help teams monitor their graph and make changes to it safely. We strongly encourage that your GraphQL gateway require client details and operation names from all requesting clients.
Enforcing in GraphOS Router
The GraphOS Router supports client awareness by default if the client sets the apollographql-client-name
and apollographql-client-id
in their requests. These values can be overridden using the router configuration file directly.
Client headers can also be enforced using a Rhai script on every incoming request.
1fn supergraph_service(service) {
2 const request_callback = Fn("process_request");
3 service.map_request(request_callback);
4 }
5
6fn process_request(request) {
7 log_info("processing request");
8 let valid_clients = ["1", "2"];
9 let valid_client_names = ["apollo-client"];
10
11 if ("apollographql-client-version" in request.headers && "apollographql-client-name" in request.headers) {
12 let client_header = request.headers["apollographql-client-version"];
13 let name_header = request.headers["apollographql-client-name"];
14
15 if !valid_clients.contains(client_header) {
16 log_error("Invalid client ID provided");
17 throw #{
18 status: 401,
19 message: "Invalid client ID provided"
20 };
21 }
22 if !valid_client_names.contains(name_header) {
23 log_error("Invalid client name provided");
24 throw #{
25 status: 401,
26 message: "Invalid client name provided"
27 };
28 }
29 }
30 else {
31 log_error("No client headers set");
32 throw #{
33 status: 401,
34 message: "No client headers set"
35 };
36 }
37}
See a runnable example Rhai script in the Apollo Solutions repository.
Enforcing in Apollo Server
If you're using Apollo Server for your gateway, you can require client metadata in every incoming request with a custom plugin:
1function clientEnforcementPlugin(): ApolloServerPlugin<BaseContext> {
2 return {
3 async requestDidStart() {
4 return {
5 async didResolveOperation(requestContext) {
6 const clientName = requestContext.request.http.headers.get("apollographql-client-name");
7 const clientVersion = requestContext.request.http.headers.get("apollographql-client-version");
8
9 if (!clientName) {
10 const logString = `Execution Denied: Operation has no identified client`;
11 requestContext.logger.debug(logString);
12 throw new GraphQLError(logString);
13 }
14
15 if (!clientVersion) {
16 const logString = `Execution Denied: Client ${clientName} has no identified version`;
17 requestContext.logger.debug(logString);
18 throw new GraphQLError(logString);
19 }
20
21 if (!requestContext.operationName) {
22 const logString = `Unnamed Operation: ${requestContext.queryHash}. All operations must be named`;
23 requestContext.logger.debug(logString);
24
25 throw new GraphQLError(logString);
26 }
27 },
28 };
29 },
30 };
31}
32const server = new ApolloServer({
33 typeDefs,
34 resolvers,
35 plugins: [clientEnforcementPlugin()],
36});
Adding enforcement for existing clients
If clients are already consuming your graph and are not providing client metadata, adding universal enforcement will break those clients. To resolve this you should take the following steps:
Use other headers
If you have other existing headers in your HTTP requests that can be parsed to extract some client info, you can extract the info from there.
GraphOS Router
Client awareness headers should be overridden using the router configuration file to use the appropriate header names.
Apollo Server
If you do change the identifying headers, also update the Usage Reporting Plugin to use the new headers so that the proper client info is also sent to Studio.
Ask clients to update their requests
The long-term fix will require that clients start sending the required headers needed to extract client info. While clients are working on updating their requests you can add the plugin code to your gateway, but instead of throwing an error you can log a warning so that the gateway team can track when all requests have been updated.