1515 */
1616
1717import * as graphql from 'graphql' ;
18- import * as https from 'https' ;
1918
20- const url1 =
21- 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json' ;
22-
23- function getData ( url : string ) : any {
24- return new Promise ( ( resolve , reject ) => {
25- https
26- . get ( url , response => {
27- let data = '' ;
28- response . on ( 'data' , chunk => {
29- data += chunk ;
30- } ) ;
31- response . on ( 'end' , ( ) => {
32- resolve ( JSON . parse ( data ) ) ;
33- } ) ;
34- } )
35- . on ( 'error' , err => {
36- reject ( err ) ;
37- } ) ;
38- } ) ;
39- }
40-
41- const authors : Author [ ] = [ ] ;
4219const books : Book [ ] = [ ] ;
4320
4421interface Book {
@@ -47,49 +24,13 @@ interface Book {
4724 authorIds : number [ ] ;
4825}
4926
50- interface Address {
51- country : string ;
52- city : string ;
53- }
54-
55- interface Author {
56- id : number ;
57- name : string ;
58- address : Address ;
59- }
60-
61- function addBook ( name : string , authorIds : string | number [ ] = [ ] ) {
62- if ( typeof authorIds === 'string' ) {
63- authorIds = authorIds . split ( ',' ) . map ( id => parseInt ( id , 10 ) ) ;
64- }
27+ function addBook ( name : string , authorIds : number [ ] = [ ] ) {
6528 const id = books . length ;
66- books . push ( {
67- id : id ,
68- name : name ,
69- authorIds : authorIds ,
70- } ) ;
29+ books . push ( { id, name, authorIds } ) ;
7130 return books [ books . length - 1 ] ;
7231}
7332
74- function addAuthor ( name : string , country : string , city : string ) {
75- const id = authors . length ;
76- authors . push ( { id, name, address : { country, city } } ) ;
77- return authors [ authors . length - 1 ] ;
78- }
79-
80- function getBook ( id : number ) {
81- return books [ id ] ;
82- }
83-
84- function getAuthor ( id : number ) {
85- return authors [ id ] ;
86- }
87-
8833function prepareData ( ) {
89- addAuthor ( 'John' , 'Poland' , 'Szczecin' ) ;
90- addAuthor ( 'Alice' , 'Poland' , 'Warsaw' ) ;
91- addAuthor ( 'Bob' , 'England' , 'London' ) ;
92- addAuthor ( 'Christine' , 'France' , 'Paris' ) ;
9334 addBook ( 'First Book' , [ 0 , 1 ] ) ;
9435 addBook ( 'Second Book' , [ 2 ] ) ;
9536 addBook ( 'Third Book' , [ 3 ] ) ;
@@ -98,112 +39,24 @@ function prepareData() {
9839prepareData ( ) ;
9940
10041export function buildTestSchema ( ) {
101- const Author = new graphql . GraphQLObjectType ( {
102- name : 'Author' ,
103- fields : {
104- id : {
105- type : graphql . GraphQLString ,
106- resolve ( obj , args ) {
107- return obj . id ;
108- } ,
109- } ,
110- name : {
111- type : graphql . GraphQLString ,
112- resolve ( obj , args ) {
113- return obj . name ;
114- } ,
115- } ,
116- description : {
117- type : graphql . GraphQLString ,
118- resolve ( obj , args ) {
119- return new Promise ( ( resolve , reject ) => {
120- getData ( url1 ) . then ( ( response : { [ key : string ] : string } ) => {
121- resolve ( response . description ) ;
122- } , reject ) ;
123- } ) ;
124- } ,
125- } ,
126- address : {
127- type : new graphql . GraphQLObjectType ( {
128- name : 'Address' ,
129- fields : {
130- country : {
131- type : graphql . GraphQLString ,
132- resolve ( obj , args ) {
133- return obj . country ;
134- } ,
135- } ,
136- city : {
137- type : graphql . GraphQLString ,
138- resolve ( obj , args ) {
139- return obj . city ;
140- } ,
141- } ,
142- } ,
143- } ) ,
144- resolve ( obj , args ) {
145- return obj . address ;
146- } ,
147- } ,
148- } ,
149- } ) ;
150-
15142 const Book = new graphql . GraphQLObjectType ( {
15243 name : 'Book' ,
15344 fields : {
154- id : {
155- type : graphql . GraphQLInt ,
156- resolve ( obj , args ) {
157- return obj . id ;
158- } ,
159- } ,
16045 name : {
16146 type : graphql . GraphQLString ,
162- resolve ( obj , args ) {
47+ resolve ( obj ) {
16348 return obj . name ;
16449 } ,
16550 } ,
166- authors : {
167- type : new graphql . GraphQLList ( Author ) ,
168- resolve ( obj , args ) {
169- return obj . authorIds . map ( ( id : number ) => {
170- return authors [ id ] ;
171- } ) ;
172- } ,
173- } ,
17451 } ,
17552 } ) ;
17653
17754 const query = new graphql . GraphQLObjectType ( {
17855 name : 'Query' ,
17956 fields : {
180- author : {
181- type : Author ,
182- args : {
183- id : { type : graphql . GraphQLInt } ,
184- } ,
185- resolve ( obj , args , context ) {
186- return Promise . resolve ( getAuthor ( args . id ) ) ;
187- } ,
188- } ,
189- authors : {
190- type : new graphql . GraphQLList ( Author ) ,
191- resolve ( obj , args , context ) {
192- return Promise . resolve ( authors ) ;
193- } ,
194- } ,
195- book : {
196- type : Book ,
197- args : {
198- id : { type : graphql . GraphQLInt } ,
199- } ,
200- resolve ( obj , args , context ) {
201- return Promise . resolve ( getBook ( args . id ) ) ;
202- } ,
203- } ,
20457 books : {
20558 type : new graphql . GraphQLList ( Book ) ,
206- resolve ( obj , args , context ) {
59+ resolve ( ) {
20760 return Promise . resolve ( books ) ;
20861 } ,
20962 } ,
0 commit comments