@dedrick
To extend the context object of a GraphQL call, you can do the following:
1 2 3 4 |
interface CustomContext extends Context { // Add custom properties or methods here user: User; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const server = new ApolloServer({ typeDefs, resolvers, context: ({ req }): CustomContext => { // Access the default context object const defaultContext = createContext(req); // Extend the default context with custom properties or methods const customContext: CustomContext = { ...defaultContext, user: getUserFromRequest(req) }; return customContext; } }); |
1 2 3 4 5 6 7 |
const resolvers = { Query: { currentUser: (_, __, { user }: CustomContext) => { return user; }, }, }; |
By following these steps, you can extend the context object of a GraphQL call with custom properties or methods to provide additional functionality or data to your resolvers.