Skip to content

Re-implemented graphqlerrors package to be more idiomatic Go code. #1

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

Merged
merged 2 commits into from
Sep 10, 2015

Conversation

sogko
Copy link
Member

@sogko sogko commented Sep 9, 2015

Hi @chris-ramon

Really appreciate the work you've put into so far in making a golang graphql library thats closely based on graphql-js JS library released recently. (There are other golang-based graphql libraries but most of them were pre-graphql-js). Would love to see where this go.

I would like to make a suggestion for this library to be more Go-idiomatic, specifically with error-handling.
It'll make it easier to use it with other Go packages.

Comments appreciated.

Details

  • Previous implementation requires package user to handle errors in non-idiomatic Go, e.g.:
    document, err := Parse(params)
    if err.Error != nil {
        // handle error
    }

Generally, most package users would expect a consistent error handling.

    // Parse() returns `error` interface
    document, err := Parse(params)
    if err != nil {
        // handle error
    }

This might seem trivial, but has an intangible benefit of not introducing more friction for other devs to adopt the packages.

Reference: https://github.com/golang/go/wiki/Errors

  • Implemented other error types based on graphql-js
    • Added functions to create different types of GraphQLError (LocatedError, SynxtaxError)

Tested

Does not break existing tests.

$ go build
$ go test github.com/chris-ramon/graphql-go/language/parser github.com/chris-ramon/graphql-go/language/lexer
ok      github.com/chris-ramon/graphql-go/language/parser   0.005s
ok      github.com/chris-ramon/graphql-go/language/lexer    0.008s

### Details

- Previous implementation requires package user to handle errors in non-idiomatic Go, e.g.:
```golang
	document, err := Parse(params)
	if err.Error != nil {
		// handle error
	}
```

Generally, most package users would expect a consistent error handling.
```golang
	document, err := Parse(params)
	if err != nil {
		// handle error
	}
```

This might seem trivial, but has an intangible benefit of not introducing more friction for other devs to adopt the packages.

Refs:
- https://github.com/golang/go/wiki/Errors

- Implemented other error types based on `graphql-js`
Added functions to create different types of GraphQLError (`LocatedError`, `SynxtaxError`)

### Tested

Does not break existing tests.

```bash
$ go build
$ go test github.com/chris-ramon/graphql-go/language/parser github.com/chris-ramon/graphql-go/language/lexer
ok  	github.com/chris-ramon/graphql-go/language/parser 0.005s
ok  	github.com/chris-ramon/graphql-go/language/lexer 0.008s
```
@chris-ramon
Copy link
Member

Wow! 🌟 thanks for your contribution @sogko !

This is definitely something need to be improved! and I totally agree with what you had done. 👍

What do you think about having shorter file naming for files within pkg errors, so instead of having formatted_error.go we just have formatted.go and so on.

- for e.g: `formatted_error.go` to `formatted.go`
@sogko
Copy link
Member Author

sogko commented Sep 10, 2015

Hi @chris-ramon,

Thanks for taking time to look at this PR, appreciate the effort 👍🏻

Yeah I agree with you, it could be less verbose lol. I've updated the PR with those changes.

Btw, I've been working on the parser on another branch with the goal of passing graphql-js parser tests.
I'll make a separate PR when its ready.

chris-ramon added a commit that referenced this pull request Sep 10, 2015
Re-implemented `graphqlerrors` package to be more idiomatic Go code.
@chris-ramon chris-ramon merged commit db5c778 into graphql-go:master Sep 10, 2015
@sogko sogko deleted the fork-master branch September 14, 2015 01:46
sogko added a commit that referenced this pull request Sep 25, 2015
Merge latest changes from HEAD
sogko pushed a commit that referenced this pull request Nov 3, 2015
Fixed failed tests and restored `language/parser` and `testutil` packages
@ajackson-cpi ajackson-cpi mentioned this pull request Nov 15, 2016
gburt pushed a commit to gburt/graphql that referenced this pull request Mar 24, 2018
fix a race condition, make things faster, profit
didrikmunther referenced this pull request in didrikmunther/graphql Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants