Skip to content

Commit

Permalink
fix: error thrown for nullable field resolving null
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
alexstrat authored and DanielMSchmidt committed Mar 6, 2019
1 parent d4cca0b commit af83c91
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/__tests__/graphqlObservable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const fieldResolverSchema = makeExecutableSchema({
type Item {
nodeFieldResolver: ObjectValue!
nullableNodeFieldResolver: ObjectValue
giveMeTheParentFieldResolver: ObjectValue!
giveMeTheArgsFieldResolver(arg: String!): ObjectValue!
giveMeTheContextFieldResolver: ObjectValue!
Expand Down Expand Up @@ -139,6 +140,9 @@ const fieldResolverSchema = makeExecutableSchema({
nodeFieldResolver() {
return of({ value: "I am a node field resolver" });
},
nullableNodeFieldResolver() {
return null;
},
giveMeTheParentFieldResolver(parent) {
return of({ value: JSON.stringify(parent) });
},
Expand Down Expand Up @@ -488,6 +492,29 @@ describe("graphqlObservable", function() {
m.expect(result.pipe(take(1))).toBeObservable(expected);
});

itMarbles("if nullable field resolver returns null, it resolves null", function(m) {
const query = gql`
query {
item {
nullableNodeFieldResolver {
value
}
}
}
`;
const expected = m.cold("(a|)", {
a: {
data: {
item: {
nullableNodeFieldResolver: null
}
}
}
});
const result = graphql(fieldResolverSchema, query, null, {});
m.expect(result.pipe(take(1))).toBeObservable(expected);
});

itMarbles("the field resolvers 1st argument is parent", function(m) {
const query = gql`
query {
Expand Down
8 changes: 6 additions & 2 deletions src/reactive-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
isScalarType,
isEnumType,
isObjectType,
parse
isNonNullType,
parse,
} from "graphql";

// WARNING: This is NOT a spec complete graphql implementation
Expand Down Expand Up @@ -145,7 +146,10 @@ export default function graphql<T = object>(
return resolvedObservable.pipe(
concatMap(emitted => {
if (!emitted) {
return throwObservable("resolver emitted empty value");
if (isNonNullType(type)) {
return throwObservable(`resolver for ${field.name} emitted empty value`);
}
return of(null);
}

if (emitted instanceof Array) {
Expand Down

0 comments on commit af83c91

Please sign in to comment.