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

Custom error In Graphql Query Response #807

Closed
gauravmakkar opened this issue Feb 6, 2018 · 2 comments
Closed

Custom error In Graphql Query Response #807

gauravmakkar opened this issue Feb 6, 2018 · 2 comments

Comments

@gauravmakkar
Copy link

gauravmakkar commented Feb 6, 2018

How can I respond with custom Errors while querying:

app.use('/graphql', graphqlExpress((req) => {

    return new Promise((resolve,reject) => {
        

        let user;
        let loader = Loader()
        if(!req.headers['authentication-token'] || true){
            throw new ErrorResponse([{key: 'authentication', message: 'Authentication Token is not 
            provided' }]);

        }



        req.session.reload(function (err) {
            let base_context = {
                orders: orders,
                users: users,
                ppsList:ppsList,
                auth_token: req.headers['authentication-token']
            }
            if (err) {
                loader.CurrentUserLoader(req.headers['authentication-token']).load(req.headers['authentication-token']).then(function (res) {
                    if(!res){
                        throw new ErrorResponse([{key: 'authentication', message: 'Authentication failure' }]);
                    }
                    req.session.current_user = res
                    logger.info("Session Created for user: " + res.name)
                    Object.assign(base_context, {current_user: req.session.current_user});
                    resolve({
                        schema,
                        tracing: true,
                        cacheControl: true,
                        context: base_context,
                        
                    })
                });

            } else {
                logger.info("Session Stored for the user: " + req.session.current_user.name);
                Object.assign(base_context, {current_user: req.session.current_user});
                resolve({
                    schema,
                    tracing: true,
                    cacheControl: true,
                    context: base_context,
                })

            }

        })


    })
}));
@bjagadeesan
Copy link

You can use this repo to throw custom errors.
https://www.npmjs.com/package/apollo-errors
It works well with Apollo graphql.

I see that you are doing authentication. As far as I know, there are two methods to do authorization,
1)manually checking for authentication in each resolver.
2)using directives as middleware as explained here.
ardatan/graphql-tools#212
which is pushed to graphql-tools but it is limiting.

Please Note:
The method 2 using directive is limiting as it DOES NOT work on QUERIES, but rather only work on FIELD_DESCRIPTIONS.

@abernix
Copy link
Member

abernix commented Apr 26, 2019

👋 I'll close this since it's quite old and also doesn't appear to be a bug with Apollo Server, but rather a question about how to use it or one of its components.

For more on error handling, check out the documentation for errors.

Rather than asking it here in GitHub Issues — where efforts are focused on fixing bugs and adding new features — I'd ask that you take this question to the Apollo Server channel within the Apollo community on Spectrum.chat where there are community members who might be able to relate to a similar problem, or might be able to help you out more interactively. Thanks for your understanding!

@abernix abernix closed this as completed Apr 26, 2019
@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

3 participants