You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Updated middleware execution to provide new functionality (#91)
* Updated middleware execution to provide new functionality
* Add missing headers to the changeset
* Remove context modification from result middleware
* Update from review
* Added new README sections and changelog message
* Spelling
* Prettier
You can now opt to return a Lambda result object directly from the middleware. This will cancel the middleware chain, bypass GraphQL request processing, and immediately return the Lambda result.
As with all Apollo Server 4 integrations, the context resolution is done in the integration. For the Lambda integration, it will look like the following:
51
+
52
+
```ts
53
+
import { ApolloServer } from'@apollo/server';
54
+
import {
55
+
startServerAndCreateLambdaHandler,
56
+
handlers,
57
+
} from'@as-integrations/aws-lambda';
58
+
59
+
typeContextValue= {
60
+
isAuthenticated:boolean;
61
+
};
62
+
63
+
// The GraphQL schema
64
+
const typeDefs =`#graphql
65
+
type Query {
66
+
hello: String!
67
+
isAuthenticated: Boolean!
68
+
}
69
+
`;
70
+
71
+
// Set up Apollo Server
72
+
const server =newApolloServer<ContextValue>({
73
+
typeDefs,
74
+
resolvers: {
75
+
Query: {
76
+
hello: () =>'world',
77
+
isAuthenticated: (root, args, context) => {
78
+
// For context typing to be valid one of the following must be implemented
79
+
// 1. `resolvers` defined inline in the server config (not particularly scalable, but works)
80
+
// 2. Add the type in the resolver function. ex. `(root, args, context: ContextValue)`
81
+
// 3. Propagate the type from an outside definition like GraphQL Codegen
If you want to define strictly typed middleware outside of the middleware array, the easiest way would be to extract your request handler into a variable and utilize the `typeof` keyword from Typescript. You could also manually use the `RequestHandler` type and fill in the event and result values yourself.
In some situations, a middleware function might require the execution end before reaching Apollo Server. This might be a global auth guard or session token lookup.
194
+
195
+
To achieve this, the request middleware function accepts `ResultType` or `Promise<ResultType>` as a return type. Should middleware resolve to such a value, that result is returned and no further execution occurs.
Each of the provided request handler factories has a generic for you to pass a manually extended event type if you have custom authorizers, or if the event type you need has a generic you must pass yourself. For example, here is a request that allows access to the lambda authorizer:
0 commit comments