Skip to content

Commit 303a811

Browse files
committed
Add fallback, due unexpected behavior of defaultValue for enum field
graphql/graphql-js#435 (comment)
1 parent 72b51e2 commit 303a811

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/resolvers/connectionResolver.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function prepareConnectionResolver(
7878
...additionalArgs,
7979
sort: {
8080
type: sortEnumType,
81-
defaultValue: sortEnumType.getValues()[0].value, // first enum used by default
81+
defaultValue: sortEnumType.getValues()[0].name, // first enum used by default
8282
description: 'Sort argument for data ordering',
8383
},
8484
},
@@ -90,12 +90,29 @@ export function prepareConnectionResolver(
9090
resolveParams,
9191
{ args: {} } // clear this params in copy
9292
);
93-
const sortOptions: connectionSortOpts = args.sort;
93+
let sortOptions: connectionSortOpts;
94+
if (typeof args.sort === 'string') {
95+
const sortValue = sortEnumType.parseValue(args.sort);
96+
if (sortValue) {
97+
sortOptions = sortValue;
98+
} else {
99+
sortOptions = {
100+
sortValue: {},
101+
uniqueFields: [],
102+
// $FlowFixMe
103+
directionFilter: filter => filter,
104+
};
105+
}
106+
} else {
107+
sortOptions = args.sort;
108+
}
94109

95110
findManyParams.args.filter = prepareFilter(args);
96111
findManyParams.args.sort = sortOptions.sortValue;
97112

98-
findManyParams.projection = projection && projection.edges && projection.edges.node;
113+
if (projection && projection.edges) {
114+
findManyParams.projection = projection.edges.node || {};
115+
}
99116
sortOptions.uniqueFields.forEach(fieldName => {
100117
findManyParams.projection[fieldName] = true;
101118
});

0 commit comments

Comments
 (0)