From 97b481980d01f30caf18dca19f73c51f9c9689c6 Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Wed, 18 Jul 2018 20:55:26 -0500 Subject: [PATCH] fix(doesQueryContain): improve error message if type not found --- src/doesQueryContain.js | 6 ++++-- test/doesQueryContain.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/doesQueryContain.js b/src/doesQueryContain.js index 72844d9..d96e56d 100644 --- a/src/doesQueryContain.js +++ b/src/doesQueryContain.js @@ -24,14 +24,16 @@ export default function doesQueryContain( ids?: ?Set, idField?: string = 'id' ): boolean { - const potentialAncestors = getPotentialAncestors(types[typename]) + const targetType = types[typename] + if (!targetType) throw new Error(`type not found: ${typename}`) + const potentialAncestors = getPotentialAncestors(targetType) function doesNodeContain( node: Node, data: any, type: Type, ): boolean { - if (type.name === typename) { + if (type === targetType) { if (!ids) return true return data && ids.has(data[idField]) } diff --git a/test/doesQueryContain.js b/test/doesQueryContain.js index b429ca2..c8b6019 100644 --- a/test/doesQueryContain.js +++ b/test/doesQueryContain.js @@ -8,6 +8,20 @@ import gql from 'graphql-tag' import doesQueryContain from '../src/doesQueryContain' describe(`doesQueryContain`, function () { + it(`throws if type is not found`, function () { + const document = gql`{ + Device(id: 1) { + id + } + }` + let error + try { + doesQueryContain(document, types, 'Deviceg') + } catch (err) { + error = err + } + expect(error).to.exist + }) it(`basic test`, function () { const document = gql`{ Device(id: 1) {