Access Control
To implement access control, define the following function in the designatedsrc/server-extension/check module:
- if the function returns
true, the request is processed as usual; - if the function returns
false, the server responds with'{"errors":[{"message":"not allowed"}]}'; - if the function returns an
errorString, the server responds with{"errors":[{"message":"${errorString}"}]}.
RequestCheckContext
The context type has the following interface:
httpfield contains the low level HTTP info. Information on headers is stored in aMapfrom lowercase header names to values. For example,req.http.headers.get('authorization')is the value of the authorization header.operationis the rootOperationDefinitionNodeof the tree describing the query. Useful if the authorization decision depends on the query contents.operationNameis the query name.schemais aGraphQLSchemaobject.contextholds aPoolOpenreaderContextatcontext.openreader. It can be used to access the database, though this is highly discouraged: the interfaces involved are considered to be internal and are subject to change without notice.modelis an Openreader dataModel.
Sending user data to resolvers
Authentication data such as user name can be passed fromrequestCheck() to a custom resolver through Openreader context:
Examples
A simple strategy that authorizes anyone with a12345 token to perform any query can be implemented with:
src/server-extension/check.ts
requestCheck() for authorization can be spotted in the wild in the code of a squid used by Reef.
