Skip to content

Commit f702ee6

Browse files
jgwhitejneurock
authored andcommitted
Unwrap non-null types (#20)
* Unwrap non-null types during selection type inference We ran into the following error when we had a non-null `edges` field in our schema (with a non-null type within the collection): Cannot read property 'cursor' of undefined This patch adds a similar non-null `edges` field to the schema and adds code to unwrap non-null types when inferring the type of a selection. * Unwrap non-null returnType during mutations
1 parent 0d98842 commit f702ee6

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

addon/fields/selections/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import createFieldInfo from '../info/create';
22
import { getFieldNameAndAlias } from '../name';
33
import { fragmentFieldsReducer, inlineFragmentFieldsReducer } from './fragments';
4-
import { partial } from '../../utils';
4+
import { partial, unwrapNonNull } from '../../utils';
55

6-
const getFieldType = (type, fieldName) =>
7-
fieldName !== '__typename' && type._fields[fieldName].type;
6+
const getFieldType = (type, fieldName) => {
7+
if (fieldName === '__typename') {
8+
return null;
9+
}
10+
11+
type = unwrapNonNull(type);
12+
13+
let field = type._fields[fieldName];
14+
let fieldType = unwrapNonNull(field.type);
15+
16+
return fieldType;
17+
}
818

919
export const selectedFieldsReducer =
1020
(getFieldNameAndAlias, getFieldType, createFieldInfo, getType, type, fragments, selections, selection) => {

addon/mocks/mutation.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { contextSet, isFunction, reduceKeys } from '../utils';
1+
import { contextSet, isFunction, reduceKeys, unwrapNonNull } from '../utils';
22
import { getRecords } from '../db';
33
import { resolveVarName } from '../filter/vars';
44

@@ -11,6 +11,8 @@ const mapVars = composeMapVars(resolveVarName);
1111

1212
export const composeMockMutation = (getRecords, mapVars) =>
1313
(db, options = {}, _, vars, __, { fieldName, returnType }) => {
14+
returnType = unwrapNonNull(returnType);
15+
1416
let { mutations = {}, varsMap = {} } = options;
1517
let mutation = mutations[fieldName];
1618
let records = getRecords(db, returnType.name);

addon/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphQLNonNull } from 'graphql';
2+
13
const sortEdgesKeysBeforePageInfo = (list) => list.sort();
24

35
export function contextPush(context, k, v) {
@@ -35,3 +37,5 @@ export const composeReduceKeys = (sortKeys) =>
3537
sortKeys(Object.keys(obj)).reduce(reducerFn, defaultValue);
3638

3739
export const reduceKeys = composeReduceKeys(sortEdgesKeysBeforePageInfo);
40+
41+
export const unwrapNonNull = (type) => type instanceof GraphQLNonNull ? type.ofType : type;

tests/dummy/app/gql/schema.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type LineItem {
1919
}
2020

2121
type LineItemConnection {
22-
edges: [LineItemEdge]
22+
edges: [LineItemEdge!]!
2323
pageInfo: PageInfo!
2424
}
2525

@@ -29,7 +29,7 @@ type LineItemEdge {
2929
}
3030

3131
type Mutation {
32-
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person
32+
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person!
3333
}
3434

3535
interface Node {

0 commit comments

Comments
 (0)