1- import { checkValidationErrors , validateGraphQlDocuments } from '../src/index.js' ;
2- import { buildSchema , parse , GraphQLError } from 'graphql' ;
1+ import { validateGraphQlDocuments } from '../src/index.js' ;
2+ import { buildSchema , parse , GraphQLError , Source } from 'graphql' ;
33
44describe ( 'validateGraphQlDocuments' , ( ) => {
55 it ( 'Should throw an informative error when validation errors happens, also check for fragments validation even why they are duplicated' , async ( ) => {
@@ -25,146 +25,33 @@ describe('validateGraphQlDocuments', () => {
2525 }
2626 ` ;
2727
28- const result = await validateGraphQlDocuments ( schema , [
29- {
30- location : 'fragment.graphql' ,
31- document : parse ( fragment ) ,
32- } ,
33- {
34- location : 'query.graphql' ,
35- document : parse ( /* GraphQL */ `
36- query searchPage {
37- otherStuff {
38- foo
28+ const result = validateGraphQlDocuments ( schema , [
29+ parse ( new Source ( fragment , 'packages/client/src/fragments/pizzeriaFragment.fragment.graphql' ) ) ,
30+ parse (
31+ new Source (
32+ /* GraphQL */ `
33+ query searchPage {
34+ otherStuff {
35+ foo
36+ }
37+ ...pizzeriaFragment
3938 }
40- ...pizzeriaFragment
41- }
4239
43- ${ fragment }
44- ` ) ,
45- } ,
40+ ${ fragment }
41+ ` ,
42+ 'packages/client/src/pages/search/searchPage.query.graphql'
43+ )
44+ ) ,
4645 ] ) ;
4746
4847 expect ( result ) . toHaveLength ( 1 ) ;
49- expect ( result [ 0 ] . filePath ) . toBe ( 'query.graphql' ) ;
50- expect ( result [ 0 ] . errors [ 0 ] instanceof GraphQLError ) . toBeTruthy ( ) ;
51- expect ( result [ 0 ] . errors [ 0 ] . message ) . toBe (
48+ expect ( result [ 0 ] . source ?. name ) . toBe ( 'packages/client/src/pages/search/searchPage. query.graphql' ) ;
49+ expect ( result [ 0 ] instanceof GraphQLError ) . toBeTruthy ( ) ;
50+ expect ( result [ 0 ] . message ) . toBe (
5251 'Fragment "pizzeriaFragment" cannot be spread here as objects of type "Query" can never be of type "Pizzeria".'
5352 ) ;
54-
55- try {
56- checkValidationErrors ( result ) ;
57- expect ( true ) . toBeFalsy ( ) ;
58- } catch ( aggregateError : any ) {
59- const { errors } = aggregateError ;
60- expect ( Symbol . iterator in errors ) . toBeTruthy ( ) ;
61- const generator = errors [ Symbol . iterator ] ( ) ;
62-
63- const error = generator . next ( ) . value ;
64-
65- expect ( error ) . toBeInstanceOf ( Error ) ;
66- expect ( error . name ) . toEqual ( 'GraphQLDocumentError' ) ;
67- expect ( error . message ) . toEqual (
68- 'GraphQLDocumentError: Fragment "pizzeriaFragment" cannot be spread here as objects of type "Query" can never be of type "Pizzeria".'
69- ) ;
70- expect ( error . stack ) . toEqual (
71- [
72- 'GraphQLDocumentError: Fragment "pizzeriaFragment" cannot be spread here as objects of type "Query" can never be of type "Pizzeria".' ,
73- ' at query.graphql:6:13' ,
74- ] . join ( '\n' )
75- ) ;
76- }
77- } ) ;
78- } ) ;
79-
80- describe ( 'checkValidationErrors' , ( ) => {
81- it ( 'Should throw errors source files and locations' , async ( ) => {
82- const loadDocumentErrors = [
83- {
84- filePath : 'packages/server/src/modules/github-check-run/providers/documents/create-check-run.mutation.graphql' ,
85- errors : [
86- {
87- message : 'Cannot query field "randomField" on type "CheckRun".' ,
88- locations : [
89- {
90- line : 7 ,
91- column : 13 ,
92- } ,
93- ] ,
94- } ,
95- {
96- message : 'Cannot query field "randomField2" on type "CheckRun".' ,
97- locations : [
98- {
99- line : 8 ,
100- column : 13 ,
101- } ,
102- ] ,
103- } ,
104- ] ,
105- } ,
106- {
107- filePath : 'packages/server/src/modules/github-check-run/providers/documents/check-run.query.graphql' ,
108- errors : [
109- {
110- message : 'Cannot query field "randomField" on type "CheckRun".' ,
111- locations : [
112- {
113- line : 7 ,
114- column : 13 ,
115- } ,
116- ] ,
117- } ,
118- ] ,
119- } ,
120- ] ;
121-
122- let errors ;
123- try {
124- checkValidationErrors ( loadDocumentErrors as any ) ;
125- } catch ( aggregateError : any ) {
126- errors = aggregateError . errors ;
127- }
128-
129- expect ( Symbol . iterator in errors ) . toBeTruthy ( ) ;
130-
131- let error ;
132- const generator = errors [ Symbol . iterator ] ( ) ;
133-
134- error = generator . next ( ) . value ;
135-
136- expect ( error ) . toBeInstanceOf ( Error ) ;
137- expect ( error . name ) . toEqual ( 'GraphQLDocumentError' ) ;
138- expect ( error . message ) . toEqual ( 'GraphQLDocumentError: Cannot query field "randomField" on type "CheckRun".' ) ;
139- expect ( error . stack ) . toEqual (
140- [
141- 'GraphQLDocumentError: Cannot query field "randomField" on type "CheckRun".' ,
142- ' at packages/server/src/modules/github-check-run/providers/documents/create-check-run.mutation.graphql:7:13' ,
143- ] . join ( '\n' )
144- ) ;
145-
146- error = generator . next ( ) . value ;
147-
148- expect ( error ) . toBeInstanceOf ( Error ) ;
149- expect ( error . name ) . toEqual ( 'GraphQLDocumentError' ) ;
150- expect ( error . message ) . toEqual ( 'GraphQLDocumentError: Cannot query field "randomField2" on type "CheckRun".' ) ;
151- expect ( error . stack ) . toEqual (
152- [
153- 'GraphQLDocumentError: Cannot query field "randomField2" on type "CheckRun".' ,
154- ' at packages/server/src/modules/github-check-run/providers/documents/create-check-run.mutation.graphql:8:13' ,
155- ] . join ( '\n' )
156- ) ;
157-
158- error = generator . next ( ) . value ;
159-
160- expect ( error ) . toBeInstanceOf ( Error ) ;
161- expect ( error . name ) . toEqual ( 'GraphQLDocumentError' ) ;
162- expect ( error . message ) . toEqual ( 'GraphQLDocumentError: Cannot query field "randomField" on type "CheckRun".' ) ;
163- expect ( error . stack ) . toEqual (
164- [
165- 'GraphQLDocumentError: Cannot query field "randomField" on type "CheckRun".' ,
166- ' at packages/server/src/modules/github-check-run/providers/documents/check-run.query.graphql:7:13' ,
167- ] . join ( '\n' )
168- ) ;
53+ expect ( result [ 0 ] . stack )
54+ . toBe ( `Fragment "pizzeriaFragment" cannot be spread here as objects of type "Query" can never be of type "Pizzeria".
55+ at packages/client/src/pages/search/searchPage.query.graphql:6:15` ) ;
16956 } ) ;
17057} ) ;
0 commit comments