Skip to content

formatError: put all extensions inside 'extensions' property#1284

Merged
leebyron merged 2 commits intographql:masterfrom
IvanGoncharov:errorExtensions
Apr 6, 2018
Merged

formatError: put all extensions inside 'extensions' property#1284
leebyron merged 2 commits intographql:masterfrom
IvanGoncharov:errorExtensions

Conversation

@IvanGoncharov
Copy link
Copy Markdown
Member

Mirrors graphql/graphql-spec#407

This is breaking change, however, extensions is the relatively new feature that was added in 0.12.0.
Also, it could be easily workaround by providing formatError property to a server:
https://github.com/graphql/express-graphql#options
https://github.com/apollographql/apollo-server#options

Unintended changes: I didn't want to add extensions: undefined to every error inside tests so I refactored them.

Copy link
Copy Markdown
Contributor

@leebyron leebyron left a comment

Choose a reason for hiding this comment

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

This is great - just some small suggestions inline.

We'll also need some kind of adoption strategy to help users with the breaking change

Comment thread src/error/formatError.js Outdated
message: error.message || 'An unknown error occurred.',
locations: error.locations,
path: error.path,
extensions: error.extensions,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The extensions key should only exist if there are extensions, so perhaps this should if (error.extensions) and return different shapes

Comment thread src/error/formatError.js Outdated
// Extensions
+[key: string]: mixed,
};
+extensions: { [key: string]: mixed } | void,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Based on spec description, this should be:

+extensions?: { [key: string]: mixed }

@IvanGoncharov
Copy link
Copy Markdown
Member Author

@leebyron Thanks for review 🎉

This is great - just some small suggestions inline.

Fixed in 80ca852. I tried dozens of ways how to initialize optional non-undefined readonly property and it's the simplest one I found.

We'll also need some kind of adoption strategy to help users with the breaking change

JSON.stringify(error) is not affected.
As for formatError, if we assume people read Release notes before updating to the new version than it's easy. You just need to write that in GraphQL server of your choice express-graphql, apollo-server, etc. you need to supply formatError as:

import { formatError as baseFormatError, /* ... */ } from 'graphql';

{
  // other options
  formatError(error) {
    const { extensions, ...rest } = baseFormatError(error);
    return { ...extensions, ...rest };
  },
}

@leebyron
Copy link
Copy Markdown
Contributor

leebyron commented Apr 5, 2018

Nice - Let's get #1305 landed first and I'll let you rebase over that, otherwise I really like this change.

@IvanGoncharov
Copy link
Copy Markdown
Member Author

@leebyron I rebased this PR on top of master.

@leebyron
Copy link
Copy Markdown
Contributor

leebyron commented Apr 6, 2018

Great work on this!

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