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

restore proper display of form field errors from GraphQL requests #162

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/server/graphql/httpGraphQLHandler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import Schema from './rootSchema';
import {graphql} from 'graphql';
import {prepareClientError} from './models/utils';

export default async (req, res) => {
// Check for admin privileges
const {query, variables, ...newContext} = req.body;
const authToken = req.user || {};
const context = {authToken, context: newContext};
const result = await graphql(Schema, query, null, context, variables);
if (result.errors) {
console.log('DEBUG GraphQL Error:', result.errors);
}
res.send(result);
const context = {authToken, ...newContext};
const result = await graphql(Schema, query, null, context, variables)
const {error} = prepareClientError(result)
res.send({data: result.data, error})
Copy link
Owner

Choose a reason for hiding this comment

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

sending an error field in every payload is no good since we want it as short as possible. Rather it should rest on the client to know how to interpret the payload.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so you're saying only assign error if it's not null? makes sense

};
10 changes: 3 additions & 7 deletions src/universal/utils/fetching.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export function getJSON(route) {
// return Object.assign({}, ...(for (p of fields) {[p]: o[p]}));
// }

export const getClientError = errors => {
if (!errors) {
return;
}
const error = errors[0].message;
export const getClientError = error => {
if (!error || error.indexOf('{"_error"') === -1) {
return {_error: 'Server query error'};
}
Expand All @@ -63,6 +59,6 @@ export const fetchGraphQL = async graphParams => {
body: serializedParams
});
const resJSON = await res.json();
const {data, errors} = resJSON;
return {data, error: getClientError(errors)};
const {data, error} = resJSON;
return {data, error: getClientError(error)};
};