Skip to content

Commit 96aab5e

Browse files
committed
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.
1 parent 0d98842 commit 96aab5e

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

addon/fields/selections/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@ import createFieldInfo from '../info/create';
22
import { getFieldNameAndAlias } from '../name';
33
import { fragmentFieldsReducer, inlineFragmentFieldsReducer } from './fragments';
44
import { partial } from '../../utils';
5+
import { GraphQLNonNull } from 'graphql';
56

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

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

tests/dummy/app/gql/schema.graphql

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)