Skip to content

Account for query's offset in file for errors #949

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 6 commits into from
Jul 14, 2017

Conversation

dwwoelfel
Copy link
Contributor

@dwwoelfel dwwoelfel commented Jul 14, 2017

Adds support for providing the location in a file that the graphQL source was located. If the location is provided as part of the source, the line numbers in the formatted errors will be updated to account for the offset.

For example, in file Foo.js:

1  const x = 1
2 
3  graphql`
4  query {
5   ?
6  }
7  `

The lexer will throw an error with message:

Parse error: GraphQLError: Syntax Error Foo.js (5:2) Cannot parse the unexpected character "?".
4: query {
5:  ?
    ^
6: }

It's up to the caller to provide the correct offset in the source.

Not sure if location is the best name for the field in the Source object. Very open to suggestions!

' ?\n' +
'\n';
const source = new Source(str, 'foo.js', { line: 11, column: 0 });
return createLexer(source).advance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

this.body = body;
this.name = name || 'GraphQL request';
this.location = location || { line: 1, column: 0 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should column be 1-indexed here as well?

Copy link
Contributor Author

@dwwoelfel dwwoelfel Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason lines are 1-indexed and columns are 0-indexed. I just looked at what Emacs prints in the modeline when I wrote this, but confirmed with the Babylon docs just now: https://github.com/babel/babylon/blob/master/ast/spec.md#node-objects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. That really makes no sense at all to me. In Vim, Atom, VSCode, TextMate, both line and column are 1-indexed. gcc also reports errors using 1-indexed lines and columns, and I suspect many other source-processing tools will as well.

@@ -22,9 +22,11 @@ export function syntaxError(
description: string
): GraphQLError {
const location = getLocation(source, position);
const line = location.line + source.location.line - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we don't want to name this source.location, to avoid confusion with the existing SourceLocation type and getLocation function. Maybe we could call it something like base or offset or something else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also handle the column offset too, but only on the first line. eg. if my source looks like this (ie. starting line 2, starting column 24):

  12345678901234567890123456789
1:
2:     graphql` ... X ...
3:  Y ...
4:`

If there is an error at "X", we should report that as being on line 2, column 29, and if there is an error at "Y" then we should report it as being on line 3, column 3. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about locationOffset?

Oh right! I meant to mention that in the pull request body. I wrote code to adjust for the column offset, but deleted it because it didn't seem worth the hassle for a pretty rare (as far as I can tell) special case. I can add the code again if you think it's important.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locationOffset sounds pretty good.

'\n' +
' ?\n' +
'\n';
expect(() => lexOne(str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth mentioning in the commit message why you made this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just put it back :)

@wincent wincent merged commit e319bdf into graphql:master Jul 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants