diff --git a/graphql_schema/lib/src/enum.dart b/graphql_schema/lib/src/enum.dart index 3d68cc2c..294ef73f 100644 --- a/graphql_schema/lib/src/enum.dart +++ b/graphql_schema/lib/src/enum.dart @@ -24,6 +24,7 @@ class GraphQLEnumType extends GraphQLScalarType @override String serialize(Value value) { + if (value == null) return null; return values.firstWhere((v) => v.value == value).name; } diff --git a/graphql_server/test/mirrors_test.dart b/graphql_server/test/mirrors_test.dart index f6cfeee5..28c81aba 100644 --- a/graphql_server/test/mirrors_test.dart +++ b/graphql_server/test/mirrors_test.dart @@ -28,6 +28,10 @@ void main() { expect(asEnumType.serialize(RomanceLanguage.FRANCE), 'FRANCE'); }); + test('can serialize null', () { + expect(asEnumType.serialize(null), null); + }); + test('fails to serialize invalid value', () { expect(() => asEnumType.serialize(34), throwsStateError); });