Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement provide context on global error handlers #1681

Closed
maimike opened this issue Sep 17, 2018 · 5 comments
Closed

How to implement provide context on global error handlers #1681

maimike opened this issue Sep 17, 2018 · 5 comments

Comments

@maimike
Copy link

maimike commented Sep 17, 2018

I just check the below PR but still do not know how to implement. Any else can help me?
#1343

Thank you so much

@Whoaa512
Copy link
Contributor

Whoaa512 commented Sep 17, 2018

@theduyetmai, if you use the extensions API (which is not well documented, so I suggest reading the source). You can create a custom error tracking extension that will be passed the context to each of the lifecycle methods.

const { ApolloServer } = require('apollo-server')

import { GraphQLExtension } from 'graphql-extensions'

class MyErrorTrackingExtension extends GraphQLExtension {
    willSendResponse(o) {
        const { context, graphqlResponse } = o

        context.trackErrors(graphqlResponse.errors)

        return o
    }
    // Other lifecycle methods include
    // requestDidStart
    // parsingDidStart
    // validationDidStart
    // executionDidStart
    // willSendResponse
}

const server = new ApolloServer({
    typeDefs,
    resolvers,
    extensions: [() => new MyErrorTrackingExtension()],
    context({ req }) {
        return {
            req,
            trackErrors(errors) {
                // Track the errors
            },
        }
    },
})

server.listen().then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`)
})

@maimike
Copy link
Author

maimike commented Sep 18, 2018

Thank you so much

@maimike maimike closed this as completed Sep 18, 2018
@kosanna
Copy link

kosanna commented Oct 2, 2018

on which version of apollo-server it's supposed to work? I have 2.1.0, but still have context being undefined @Whoaa512

@Whoaa512
Copy link
Contributor

Whoaa512 commented Oct 2, 2018

@kosanna According to the release notes it should have gone out in v2.0.8

@omgrief
Copy link

omgrief commented Oct 11, 2018

Bumping this issue, the fix brought in to v2.0.8 does seem like it would be the fix to allow for the context to be accessible from within the formatError function, but it is not the case as it never gets passed to the actual formatter, and is just returned by the extension.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants