Skip to content

Commit d2c407e

Browse files
committed
Model lists as Lists of NonNull elements
1 parent 79e989e commit d2c407e

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/backref-types.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

33
const _get = require('lodash.get');
4-
const {GraphQLString, GraphQLInt, GraphQLObjectType, GraphQLList} = require('graphql');
4+
const {
5+
GraphQLNonNull,
6+
GraphQLObjectType,
7+
GraphQLInt,
8+
GraphQLList,
9+
GraphQLString,
10+
} = require('graphql');
511

612
module.exports = createBackrefsType;
713

@@ -24,7 +30,7 @@ function prepareBackrefsFields (ct, ctIdToType) {
2430

2531
function createBackrefFieldConfig (backref, Type) {
2632
return {
27-
type: new GraphQLList(Type),
33+
type: new GraphQLList(new GraphQLNonNull(Type)),
2834
args: {
2935
q: {type: GraphQLString},
3036
skip: {type: GraphQLInt},

src/field-config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const _get = require('lodash.get');
44

55
const {
6+
GraphQLNonNull,
67
GraphQLString,
78
GraphQLInt,
89
GraphQLFloat,
@@ -49,15 +50,15 @@ function createObjectFieldConfig (field) {
4950
}
5051

5152
function createArrayOfStringsFieldConfig (field) {
52-
return createFieldConfig(new GraphQLList(GraphQLString), field);
53+
return createFieldConfig(new GraphQLList(new GraphQLNonNull(GraphQLString)), field);
5354
}
5455

5556
function createAssetFieldConfig (field) {
5657
return createFieldConfig(AssetType, field, getAsset);
5758
}
5859

5960
function createArrayOfAssetsFieldConfig (field) {
60-
return createFieldConfig(new GraphQLList(AssetType), field, (links, ctx) => {
61+
return createFieldConfig(new GraphQLList(new GraphQLNonNull(AssetType)), field, (links, ctx) => {
6162
if (Array.isArray(links)) {
6263
return links.map(link => getAsset(link, ctx)).filter(isObject);
6364
}
@@ -81,7 +82,7 @@ function createEntryFieldConfig (field, ctIdToType) {
8182
}
8283

8384
function createArrayOfEntriesFieldConfig (field, ctIdToType) {
84-
const Type = new GraphQLList(typeFor(field, ctIdToType));
85+
const Type = new GraphQLList(new GraphQLNonNull(typeFor(field, ctIdToType)));
8586

8687
return createFieldConfig(Type, field, (links, ctx) => {
8788
if (Array.isArray(links)) {

src/schema.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const _get = require('lodash.get');
44

55
const {
66
GraphQLSchema,
7+
GraphQLNonNull,
78
GraphQLObjectType,
89
GraphQLList,
910
GraphQLString,
@@ -68,7 +69,7 @@ function createQueryFields (spaceGraph) {
6869
};
6970

7071
acc[ct.names.collectionField] = {
71-
type: new GraphQLList(Type),
72+
type: new GraphQLList(new GraphQLNonNull(Type)),
7273
args: {
7374
q: {type: GraphQLString},
7475
skip: {type: GraphQLInt},

test/field-config.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const test = require('tape');
44

55
const {
6+
GraphQLNonNull,
67
GraphQLString,
78
GraphQLInt,
89
GraphQLFloat,
@@ -27,6 +28,10 @@ const ctx = {
2728
}
2829
};
2930

31+
function isNonNullType(type) {
32+
return type instanceof GraphQLNonNull;
33+
}
34+
3035
test('field-config: simple types', function (t) {
3136
const tests = [
3237
['String', GraphQLString, ['hello', '']],
@@ -79,6 +84,7 @@ test('field-config: array of strings', function (t) {
7984
const config = map['Array<String>']({id: 'test'});
8085
t.ok(config.type instanceof GraphQLList);
8186
t.equal(getNamedType(config.type), GraphQLString);
87+
t.ok(isNonNullType(config.type.ofType));
8288
t.equal(config.resolve({fields: {}}), undefined);
8389

8490
[[], ['x'], ['x', 'y'], null, undefined].forEach(val => {
@@ -141,6 +147,7 @@ test('field-config: arrays of links', function (t) {
141147
[[assetConfig, AssetType], [entryConfig, EntryType]].forEach(([config, Type]) => {
142148
t.ok(config.type instanceof GraphQLList);
143149
t.equal(getNamedType(config.type), Type);
150+
t.ok(isNonNullType(config.type.ofType));
144151
t.equal(config.resolve({fields: {}}), undefined);
145152
t.equal(config.resolve({fields: {test: null}}), undefined);
146153
t.deepEqual(config.resolve({fields: {test: []}}, null, ctx), []);

0 commit comments

Comments
 (0)