author : Alex Chao Nov 17th, 2019. Started
 *  function param param param param returns exports changelog import * as vscode from 'vscode' ; const fetch = require ( 'node-fetch' ) ; function schemaToStringQueryAndMutation ( schemaPiece ) { let queriesStr = '' ; schemaPiece [ 0 ] . fields . forEach ( ( one ) => { queriesStr += `${ one . name } (` ; one . args . forEach ( ( e ) => { queriesStr += `\n  ${ e . name }: ` ; let point = e . type ; let nonNull = false ; while ( point . ofType ) { if ( point . kind === 'NON_NULL' ) nonNull = true ; point = point . ofType ; } queriesStr += `${ point . name }${ nonNull ? '!' : '' }, ` ; } ) ; queriesStr = queriesStr . slice ( 0 , queriesStr . length - 2 ) ; if ( one . args . length ) queriesStr += '\n  )' ; if ( one . type . ofType ) { if ( one . type . ofType . kind !== 'LIST' ) { queriesStr += `: ${ one . type . ofType . name }${ one . type . kind === 'NON_NULL' ? '!' : '' }\n` ; } else { queriesStr += `: [${ one . type . ofType . ofType . ofType . name }${ one . type . ofType . ofType === 'NON_NULL' ? '!' : '' }]${ one . type . kind === 'NON_NULL' ? '!' : '' }\n` ; } } } ) ; return queriesStr ; } function schemaToStringTypes ( types ) { const ans = [ ] ; types . filter ( ( type ) => type . kind === 'OBJECT' ) . forEach ( ( type , index ) => { ans . push ( `${ type . name }\n  ` ) ; type . fields . forEach ( ( field ) => { ans [ index ] += `${ field . name }: ` ; let pointer = field . type ; let nonNull = false ; let isList = false ; while ( pointer . ofType ) { if ( pointer . kind === 'LIST' ) isList = true ; if ( pointer . kind === 'NON_NULL' ) nonNull = true ; pointer = pointer . ofType ; } ans [ index ] += `${ isList ? '[' : '' }${ pointer . name }${ nonNull ? '!' : '' }${ isList ? ']' : '' },\n  ` ; } ) ; } ) ; return ans ; } async function showGraphqlSchema ( serverOnFromUser , serverTurnedOnByGraphQuill , gqChannel , portNumber , url ) { const validatedURL = url || `http://localhost:${ portNumber }/graphql` ; console . log ( 'validatedURL: ' , validatedURL ) ; if ( serverOnFromUser || serverTurnedOnByGraphQuill || url ) { const all = await fetch ( validatedURL , { method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( { query : `query IntrospectionQuery {
          __schema {
            types {
              ...FullType
            }
          }
        }
        
        fragment FullType on __Type {
          kind
          name
          description
          fields(includeDeprecated: false) {
            name
            description
            args {
              ...InputValue
            }
            type {
              ...TypeRef
            }
          }
        }
        fragment InputValue on __InputValue {
          name
          description
          type { ...TypeRef }
          defaultValue
        }
        fragment TypeRef on __Type {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                  ofType {
                    kind
                    name
                    ofType {
                      kind
                      name
                      ofType {
                        kind
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }` , } ) , } ) . then ( ( res ) => res . json ( ) ) . then ( ( json ) => ( json . data . __schema . types . filter ( ( e ) => ( e . kind !== 'SCALAR' ) ) ) ) ; const queries = all . filter ( ( e ) => e . name === 'Query' ) ; const parsedQueries = schemaToStringQueryAndMutation ( queries ) ; gqChannel . append ( `QUERIES:\n${ parsedQueries }\n` ) ; const mutations = all . filter ( ( e ) => e . name === 'Mutation' ) ; if ( mutations . length ) { const parsedMutations = schemaToStringQueryAndMutation ( mutations ) ; gqChannel . append ( `\nMUTATIONS:\n${ parsedMutations }\n` ) ; } const types = all . filter ( ( e ) => e . name !== 'Mutation' && e . name !== 'Query' && e . name . slice ( 0 , 2 ) !== '__' ) ; gqChannel . append ( '\nTYPES: \n' ) ; const parsedTypesArray = schemaToStringTypes ( types ) ; parsedTypesArray . forEach ( ( oneType ) => { gqChannel . append ( `${ oneType }\n` ) ; } ) ; } } module . exports = showGraphqlSchema ;