File tree Expand file tree Collapse file tree 2 files changed +58
-1
lines changed
Expand file tree Collapse file tree 2 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 33 buildClientSchema ,
44 execute ,
55 GraphQLObjectType ,
6+ GraphQLInterfaceType ,
67 GraphQLSchema ,
78} from 'graphql'
89import {
@@ -21,7 +22,8 @@ export function generateSchemaFromIntrospectionResult(introspectionResult) {
2122 if (
2223 typeName . startsWith ( '__' ) ||
2324 typeName === 'Query' ||
24- ! ( type instanceof GraphQLObjectType )
25+ ( ! ( type instanceof GraphQLObjectType ) &&
26+ ! ( type instanceof GraphQLInterfaceType ) )
2527 ) {
2628 return fields
2729 }
@@ -64,6 +66,14 @@ function executeFragment(schema, fragmentDocument) {
6466 `
6567
6668 const res = execute ( schema , query )
69+
70+ if ( res . errors && res . errors . length ) {
71+ throw res . errors [ 0 ]
72+ }
73+
74+ if ( res . data [ fieldName ] === undefined ) {
75+ throw new Error ( `fraql: type "${ typeName } " not found` )
76+ }
6777 return res . data [ fieldName ]
6878}
6979
Original file line number Diff line number Diff line change @@ -108,6 +108,53 @@ describe('mock', () => {
108108 const data2 = mocker . mockFragment ( fragment )
109109 expect ( data2 . coverPicture . cloudinaryId ) . toBe ( 'Hello World' )
110110 } )
111+
112+ it ( 'should throw an error if a type is not found' , ( ) => {
113+ const fragment = gql `
114+ fragment _ on WTF {
115+ id
116+ name
117+ }
118+ `
119+
120+ expect . assertions ( 1 )
121+ try {
122+ mocker . mockFragment ( fragment )
123+ } catch ( error ) {
124+ expect ( error . message ) . toBe ( 'fraql: type "WTF" not found' )
125+ }
126+ } )
127+
128+ it ( 'should throw an error if an interface has no __typename' , ( ) => {
129+ const fragment = gql `
130+ fragment _ on Place {
131+ id
132+ name
133+ }
134+ `
135+
136+ expect . assertions ( 1 )
137+ try {
138+ mocker . mockFragment ( fragment , { mocks : { Place : ( ) => ( { } ) } } )
139+ } catch ( error ) {
140+ expect ( error . message ) . toBe ( 'Please return a __typename in "Place"' )
141+ }
142+ } )
143+
144+ it ( 'should support interface' , ( ) => {
145+ const fragment = gql `
146+ fragment _ on Place {
147+ id
148+ name
149+ }
150+ `
151+
152+ const data = mocker . mockFragment ( fragment , {
153+ mocks : { Place : ( ) => ( { __typename : 'Hotel' } ) } ,
154+ } )
155+
156+ expect ( data . id ) . toBeDefined ( )
157+ } )
111158 } )
112159
113160 describe ( '#fromFragments' , ( ) => {
You can’t perform that action at this time.
0 commit comments