diff --git a/src/backref-types.js b/src/backref-types.js index 412e894..5636a99 100644 --- a/src/backref-types.js +++ b/src/backref-types.js @@ -1,7 +1,11 @@ 'use strict'; const _get = require('lodash.get'); -const {GraphQLObjectType, GraphQLList} = require('graphql'); +const { + GraphQLNonNull, + GraphQLObjectType, + GraphQLList +} = require('graphql'); module.exports = createBackrefsType; @@ -24,7 +28,7 @@ function prepareBackrefsFields (ct, ctIdToType) { function createBackrefFieldConfig (backref, Type) { return { - type: new GraphQLList(Type), + type: new GraphQLList(new GraphQLNonNull(Type)), resolve: (entryId, _, ctx) => { return ctx.entryLoader.queryAll(backref.ctId) .then(entries => filterEntries(entries, backref.fieldId, entryId)); diff --git a/src/field-config.js b/src/field-config.js index 935c7b4..f2e4f9f 100644 --- a/src/field-config.js +++ b/src/field-config.js @@ -3,6 +3,7 @@ const _get = require('lodash.get'); const { + GraphQLNonNull, GraphQLString, GraphQLInt, GraphQLFloat, @@ -49,7 +50,7 @@ function createObjectFieldConfig (field) { } function createArrayOfStringsFieldConfig (field) { - return createFieldConfig(new GraphQLList(GraphQLString), field); + return createFieldConfig(new GraphQLList(new GraphQLNonNull(GraphQLString)), field); } function createAssetFieldConfig (field) { @@ -57,7 +58,7 @@ function createAssetFieldConfig (field) { } function createArrayOfAssetsFieldConfig (field) { - return createFieldConfig(new GraphQLList(AssetType), field, (links, ctx) => { + return createFieldConfig(new GraphQLList(new GraphQLNonNull(AssetType)), field, (links, ctx) => { if (Array.isArray(links)) { return links.map(link => getAsset(link, ctx)).filter(isObject); } @@ -81,7 +82,7 @@ function createEntryFieldConfig (field, ctIdToType) { } function createArrayOfEntriesFieldConfig (field, ctIdToType) { - const Type = new GraphQLList(typeFor(field, ctIdToType)); + const Type = new GraphQLList(new GraphQLNonNull(typeFor(field, ctIdToType))); return createFieldConfig(Type, field, (links, ctx) => { if (Array.isArray(links)) { diff --git a/src/schema.js b/src/schema.js index a3cd627..5e5e3ab 100644 --- a/src/schema.js +++ b/src/schema.js @@ -4,6 +4,7 @@ const _get = require('lodash.get'); const { GraphQLSchema, + GraphQLNonNull, GraphQLObjectType, GraphQLList, GraphQLString, @@ -68,7 +69,7 @@ function createQueryFields (spaceGraph) { }; acc[ct.names.collectionField] = { - type: new GraphQLList(Type), + type: new GraphQLList(new GraphQLNonNull(Type)), args: { q: {type: GraphQLString}, skip: {type: GraphQLInt}, diff --git a/test/field-config.test.js b/test/field-config.test.js index 0f9f6bd..5294769 100644 --- a/test/field-config.test.js +++ b/test/field-config.test.js @@ -3,6 +3,7 @@ const test = require('tape'); const { + GraphQLNonNull, GraphQLString, GraphQLInt, GraphQLFloat, @@ -27,6 +28,10 @@ const ctx = { } }; +function isNonNullType(type) { + return type instanceof GraphQLNonNull; +} + test('field-config: simple types', function (t) { const tests = [ ['String', GraphQLString, ['hello', '']], @@ -79,6 +84,7 @@ test('field-config: array of strings', function (t) { const config = map['Array']({id: 'test'}); t.ok(config.type instanceof GraphQLList); t.equal(getNamedType(config.type), GraphQLString); + t.ok(isNonNullType(config.type.ofType)); t.equal(config.resolve({fields: {}}), undefined); [[], ['x'], ['x', 'y'], null, undefined].forEach(val => { @@ -141,6 +147,7 @@ test('field-config: arrays of links', function (t) { [[assetConfig, AssetType], [entryConfig, EntryType]].forEach(([config, Type]) => { t.ok(config.type instanceof GraphQLList); t.equal(getNamedType(config.type), Type); + t.ok(isNonNullType(config.type.ofType)); t.equal(config.resolve({fields: {}}), undefined); t.equal(config.resolve({fields: {test: null}}), undefined); t.deepEqual(config.resolve({fields: {test: []}}, null, ctx), []);