Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
Model lists as Lists of NonNull elements
Browse files Browse the repository at this point in the history
  • Loading branch information
cysp committed Feb 9, 2018
1 parent 79d8028 commit f1f8fe3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/backref-types.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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));
Expand Down
7 changes: 4 additions & 3 deletions src/field-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const _get = require('lodash.get');

const {
GraphQLNonNull,
GraphQLString,
GraphQLInt,
GraphQLFloat,
Expand Down Expand Up @@ -49,15 +50,15 @@ function createObjectFieldConfig (field) {
}

function createArrayOfStringsFieldConfig (field) {
return createFieldConfig(new GraphQLList(GraphQLString), field);
return createFieldConfig(new GraphQLList(new GraphQLNonNull(GraphQLString)), field);
}

function createAssetFieldConfig (field) {
return createFieldConfig(AssetType, field, getAsset);
}

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);
}
Expand All @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _get = require('lodash.get');

const {
GraphQLSchema,
GraphQLNonNull,
GraphQLObjectType,
GraphQLList,
GraphQLString,
Expand Down Expand Up @@ -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},
Expand Down
7 changes: 7 additions & 0 deletions test/field-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const test = require('tape');

const {
GraphQLNonNull,
GraphQLString,
GraphQLInt,
GraphQLFloat,
Expand All @@ -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', '']],
Expand Down Expand Up @@ -79,6 +84,7 @@ test('field-config: array of strings', function (t) {
const config = map['Array<String>']({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 => {
Expand Down Expand Up @@ -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), []);
Expand Down

0 comments on commit f1f8fe3

Please sign in to comment.