Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
feat: error in field resolvers gets caught
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schmidt authored and DanielMSchmidt committed Jan 4, 2019
1 parent a1ef250 commit 1cdbcae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
21 changes: 21 additions & 0 deletions src/__tests__/graphqlObservable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const fieldResolverSchema = makeExecutableSchema({
plain: Plain!
item: Item!
nested: Nested!
throwingResolver: String
}
`,
resolvers: {
Expand Down Expand Up @@ -164,6 +165,9 @@ const fieldResolverSchema = makeExecutableSchema({

nested() {
return Observable.of({});
},
throwingResolver() {
throw new Error("my personal error");
}
}
}
Expand Down Expand Up @@ -525,6 +529,23 @@ describe("graphqlObservable", function() {
m.expect(result.take(1)).toBeObservable(expected);
});
});

itMarbles("nested resolvers pass down the context and parent", function(m) {
const query = gql`
query {
throwingResolver
}
`;
const expected = m.cold(
"#",
{},
new Error(
"graphqlObservable error: resolver 'throwingResolver' throws this error: 'Error: my personal error'"
)
);
const result = graphqlObservable(query, fieldResolverSchema, {});
m.expect(result.take(1)).toBeObservable(expected);
});
});

describe("Mutation", function() {
Expand Down
36 changes: 21 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,28 @@ function resolveField(
}

const args = buildResolveArgs(definition, context);
const resolvedValue = field.resolve(
parent,
args,
context,
// @ts-ignore
null // that would be the info
);
try {
const resolvedValue = field.resolve(
parent,
args,
context,
// @ts-ignore
null // that would be the info
);

if (resolvedValue instanceof Observable) {
return resolvedValue;
}
if (resolvedValue instanceof Observable) {
return resolvedValue;
}

if (resolvedValue instanceof Promise) {
return Observable.fromPromise(resolvedValue);
}
if (resolvedValue instanceof Promise) {
return Observable.fromPromise(resolvedValue);
}

// It seems like a plain value
return Observable.of(resolvedValue);
// It seems like a plain value
return Observable.of(resolvedValue);
} catch (err) {
return throwObservable(
`resolver '${field.name}' throws this error: '${err}'`
);
}
}

0 comments on commit 1cdbcae

Please sign in to comment.