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

Better handle errors report by reactive-graphql #4

Merged
merged 2 commits into from
Jan 23, 2019
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
59 changes: 37 additions & 22 deletions src/__tests__/reactiveSchemaLink.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { execute } from 'apollo-link';
import { execute, Observable } from 'apollo-link';
import { from } from "rxjs";
import { makeExecutableSchema } from 'graphql-tools';
import gql from 'graphql-tag';

import { ReactiveSchemaLink } from '../reactiveSchemaLink';

/**
* Take an Observable and return a Promise that is resolved with and on the
* first value emited.
*/
function toPromise<T>(obs: Observable<T>): Promise<T> {
return new Promise((resolve, reject) => {
obs.subscribe({
next: resolve,
error: reject,
})
})
}


const sampleQuery = gql`
query SampleQuery {
sampleQuery {
Expand Down Expand Up @@ -98,27 +112,6 @@ describe('ReactiveSchemaLink', () => {
});
});

it('calls error when fetch fails', done => {
const badTypeDefs = 'type Query {}';
const badSchema = makeExecutableSchema({ typeDefs });

const link = new ReactiveSchemaLink({ schema: badSchema });
const observable = execute(link, {
query: sampleQuery,
});
observable.subscribe(
result => expect(false),
error => {
expect(error).toBeDefined();
done();
},
() => {
expect(false);
done();
},
);
});

it('pass down variables', done => {
const next = jest.fn();
const sampleQueryWithArgsResolver = jest.fn()
Expand Down Expand Up @@ -182,4 +175,26 @@ describe('ReactiveSchemaLink', () => {
},
});
});


it('adds an error in erros when a resolver fail', async () => {
const resolvers = {
Query: {
sampleQuery: (root, args, context) => {
throw new Error('Bip bip');
}
}
}
const schema = makeExecutableSchema({
typeDefs,
resolvers
});

const link = new ReactiveSchemaLink({ schema });
const results = await toPromise(execute(link, {
query: sampleQuery,
}));

expect(results.errors).toHaveLength(1);
})
});
30 changes: 25 additions & 5 deletions src/reactiveSchemaLink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link';
import { GraphQLSchema } from 'graphql';
import { GraphQLSchema, GraphQLError } from 'graphql';
import { graphql } from 'reactive-graphql';
import { of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

export namespace ReactiveSchemaLink {
export type ResolverContextFunction<ContextType> = (
Expand All @@ -20,6 +22,23 @@ export namespace ReactiveSchemaLink {
}
}

// will transform the errors of the execution results provided by
// reactive-graphql in proper GraphQLErrors
function normalizeErrorsField(results: {data?: object, errors?: string[]}): FetchResult<object> {
const _results: FetchResult<object> = {};
_results.data = results.data;
if (results.errors) {
_results.errors = results.errors.map(message => {
// @ts-ignore just to be sure, message could be an instance of error
if (typeof message.message === 'string') {
// @ts-ignore
return new GraphQLError(message.message);
}
return new GraphQLError(message);
});
}
return _results;
}
export class ReactiveSchemaLink<ContextType> extends ApolloLink {
public schema: GraphQLSchema;
public context: ReactiveSchemaLink.ResolverContextFunction<ContextType> | any;
Expand All @@ -42,11 +61,12 @@ export class ReactiveSchemaLink<ContextType> extends ApolloLink {
null,
context,
operation.variables,
// @ts-ignore issue with the typage of the `errors` key in reactive-graphql
// https://github.com/mesosphere/reactive-graphql/pull/11/files#r249788225
).subscribe(observer);
)
.pipe(catchError((error: Error) => of({errors: [new GraphQLError(error.message)] })))
.pipe(map(normalizeErrorsField))
.subscribe(observer);
} catch(e) {
observer.error(e);
observer.next({ errors: [new GraphQLError(e.mesage)]});
}
});
}
Expand Down