@@ -7,8 +7,7 @@ Detect and interact with GraphQL APIs for efficient data extraction.
77Automatically detect GraphQL endpoints:
88
99``` typescript
10- import { detectGraphQL } from ' ts-web-scraper'
11- import { createScraper } from ' ts-web-scraper'
10+ import { createScraper , detectGraphQL } from ' ts-web-scraper'
1211
1312const scraper = createScraper ()
1413
@@ -21,7 +20,7 @@ const detection = detectGraphQL(result.html, result.url)
2120console .log (' Has GraphQL:' , detection .hasGraphQL )
2221console .log (' Endpoints found:' , detection .endpoints .length )
2322
24- detection .endpoints .forEach (endpoint => {
23+ detection .endpoints .forEach (( endpoint ) => {
2524 console .log (' URL:' , endpoint .url )
2625 console .log (' Method:' , endpoint .method )
2726})
@@ -38,7 +37,8 @@ const isValid = await verifyGraphQLEndpoint('https://api.example.com/graphql')
3837
3938if (isValid ) {
4039 console .log (' Valid GraphQL endpoint' )
41- } else {
40+ }
41+ else {
4242 console .log (' Not a GraphQL endpoint' )
4343}
4444```
@@ -54,7 +54,7 @@ const client = new GraphQLClient({
5454 endpoint: ' https://api.example.com/graphql' ,
5555 method: ' POST' ,
5656 headers: {
57- ' Authorization' : ' Bearer your-token' ,
57+ Authorization: ' Bearer your-token' ,
5858 },
5959})
6060
@@ -80,7 +80,8 @@ const response = await client.query({
8080
8181if (response .data ) {
8282 console .log (' User:' , response .data .user )
83- } else if (response .errors ) {
83+ }
84+ else if (response .errors ) {
8485 console .error (' GraphQL errors:' , response .errors )
8586}
8687```
@@ -182,10 +183,10 @@ console.log('Mutation type:', schema?.mutationType?.name)
182183console .log (' Types:' , schema ?.types ?.length )
183184
184185// Explore available types
185- schema ?.types ?.forEach (type => {
186+ schema ?.types ?.forEach (( type ) => {
186187 console .log (` Type: ${type .name } ` )
187188 if (type .fields ) {
188- type .fields .forEach (field => {
189+ type .fields .forEach (( field ) => {
189190 console .log (` - ${field .name }: ${field .type .name } ` )
190191 })
191192 }
@@ -207,7 +208,8 @@ const isHealthy = await client.healthCheck()
207208
208209if (isHealthy ) {
209210 console .log (' GraphQL endpoint is healthy' )
210- } else {
211+ }
212+ else {
211213 console .error (' GraphQL endpoint is down' )
212214}
213215```
@@ -243,7 +245,7 @@ const jsCode = `
243245const queries = extractGraphQLQueries (jsCode )
244246
245247console .log (' Found queries:' , queries .length )
246- queries .forEach (query => {
248+ queries .forEach (( query ) => {
247249 console .log (query )
248250})
249251```
@@ -253,8 +255,7 @@ queries.forEach(query => {
253255Automatic detection and extraction:
254256
255257``` typescript
256- import { createScraper } from ' ts-web-scraper'
257- import { detectGraphQL , GraphQLClient } from ' ts-web-scraper'
258+ import { createScraper , detectGraphQL , GraphQLClient } from ' ts-web-scraper'
258259
259260const scraper = createScraper ()
260261
@@ -291,7 +292,8 @@ if (detection.hasGraphQL && detection.endpoints.length > 0) {
291292 })
292293
293294 console .log (' Products:' , response .data ?.products )
294- } catch (error ) {
295+ }
296+ catch (error ) {
295297 console .error (' GraphQL query failed:' , error )
296298 }
297299}
@@ -336,7 +338,7 @@ import { GraphQLClient } from 'ts-web-scraper'
336338
337339const client = new GraphQLClient ({
338340 endpoint: ' https://api.example.com/graphql' ,
339- retries: 3 , // Retry on failure
341+ retries: 3 , // Retry on failure
340342})
341343
342344const response = await client .query ({
@@ -351,7 +353,7 @@ const response = await client.query({
351353
352354if (response .errors ) {
353355 console .error (' GraphQL returned errors:' )
354- response .errors .forEach (error => {
356+ response .errors .forEach (( error ) => {
355357 console .error (' -' , error .message )
356358 if (error .locations ) {
357359 console .error (' at line' , error .locations [0 ].line )
@@ -372,7 +374,7 @@ if (response.data) {
372374Analyze query types:
373375
374376``` typescript
375- import { getOperationType , getOperationName } from ' ts-web-scraper'
377+ import { getOperationName , getOperationType } from ' ts-web-scraper'
376378
377379const query1 = `
378380 query GetUser($id: ID!) {
@@ -391,11 +393,11 @@ const query2 = `
391393 }
392394`
393395
394- console .log (' Query 1 type:' , getOperationType (query1 )) // "query"
395- console .log (' Query 1 name:' , getOperationName (query1 )) // "GetUser"
396+ console .log (' Query 1 type:' , getOperationType (query1 )) // "query"
397+ console .log (' Query 1 name:' , getOperationName (query1 )) // "GetUser"
396398
397- console .log (' Query 2 type:' , getOperationType (query2 )) // "mutation"
398- console .log (' Query 2 name:' , getOperationName (query2 )) // "UpdateUser"
399+ console .log (' Query 2 type:' , getOperationType (query2 )) // "mutation"
400+ console .log (' Query 2 name:' , getOperationName (query2 )) // "UpdateUser"
399401```
400402
401403## GET Requests
@@ -407,7 +409,7 @@ import { GraphQLClient } from 'ts-web-scraper'
407409
408410const client = new GraphQLClient ({
409411 endpoint: ' https://api.example.com/graphql' ,
410- method: ' GET' , // Use GET instead of POST
412+ method: ' GET' , // Use GET instead of POST
411413})
412414
413415// Query is encoded in URL parameters
@@ -423,12 +425,7 @@ console.log('Products:', response.data?.products)
423425Full GraphQL scraping workflow:
424426
425427``` typescript
426- import { createScraper } from ' ts-web-scraper'
427- import {
428- detectGraphQL ,
429- verifyGraphQLEndpoint ,
430- GraphQLClient ,
431- } from ' ts-web-scraper'
428+ import { createScraper , detectGraphQL , GraphQLClient , verifyGraphQLEndpoint } from ' ts-web-scraper'
432429
433430async function scrapeGraphQLSite(url : string ) {
434431 // 1. Scrape the page
@@ -489,12 +486,13 @@ async function scrapeGraphQLSite(url: string) {
489486 })
490487
491488 console .log (' Available queries:' )
492- response .data ?.__type ?.fields ?.forEach (field => {
489+ response .data ?.__type ?.fields ?.forEach (( field ) => {
493490 console .log (` - ${field .name }: ${field .description } ` )
494491 })
495492
496493 return { client , schema , endpoint: endpoint .url }
497- } catch (error ) {
494+ }
495+ catch (error ) {
498496 console .error (' Failed to query endpoint:' , error )
499497 }
500498 }
@@ -530,8 +528,8 @@ import { GraphQLClient } from 'ts-web-scraper'
530528const client = new GraphQLClient ({
531529 endpoint: ' https://api.example.com/graphql' ,
532530 method: ' POST' ,
533- timeout: 30000 , // 30 second timeout
534- retries: 3 , // Retry up to 3 times
531+ timeout: 30000 , // 30 second timeout
532+ retries: 3 , // Retry up to 3 times
535533 headers: {
536534 ' User-Agent' : ' MyBot/1.0' ,
537535 },
0 commit comments