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

Add ApolloError in error body when handling errors #6956

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 51 additions & 0 deletions packages/ra-data-graphql/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ApolloClient, ApolloError } from '@apollo/client';
import { GraphQLError } from 'graphql';
import gql from 'graphql-tag';

import buildDataProvider, { BuildQueryFactory } from './index';

describe('GraphQL data provider', () => {
describe('mutate', () => {
describe('with error', () => {
it('sets ApolloError in body', async () => {
const mockClient = {
mutate: async () => {
throw new ApolloError({
graphQLErrors: [new GraphQLError('some error')],
});
},
};
const mockBuildQueryFactory = () => {
return () => ({
query: gql`
mutation {
updateMyResource {
result
}
}
`,
parseResponse: () => ({}),
});
};
const dataProvider = await buildDataProvider({
client: (mockClient as unknown) as ApolloClient<unknown>,
introspection: false,
buildQuery: (mockBuildQueryFactory as unknown) as BuildQueryFactory,
});
try {
await dataProvider.update('myResource', {
id: 1,
previousData: { id: 1 },
data: {},
});
} catch (error) {
expect(error.body).not.toBeNull();
expect(error.body.graphQLErrors).toBeDefined();
expect(error.body.graphQLErrors).toHaveLength(1);
return;
}
fail('expected data provider to throw an error');
});
});
});
});
3 changes: 1 addition & 2 deletions packages/ra-data-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,14 @@ export default async (options: Options): Promise<DataProvider> => {
};

const handleError = (error: ApolloError) => {
console.error({ error });
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 think this console.error should not be here. Since handleError re-throws, it should be logged / catched at higher levels.

if (error?.networkError as ServerError) {
throw new HttpError(
(error?.networkError as ServerError)?.message,
(error?.networkError as ServerError)?.statusCode
);
}

throw new HttpError(error.message, 200);
throw new HttpError(error.message, 200, error);
};

const getQueryOperation = query => {
Expand Down