{"size": 2313, "ext": "js", "max_stars_repo_path": "introspection.js", "max_stars_repo_name": "signalnerve/workers-graphql-proxy", "max_stars_repo_stars_event_min_datetime": "2020-06-04T16:09:30.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-26T14:35:00.000Z", "max_issues_repo_path": "introspection.js", "max_issues_repo_name": "signalnerve/workers-graphql-proxy", "max_issues_repo_issues_event_min_datetime": "2021-01-10T16:06:57.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-08T12:42:18.000Z", "max_forks_repo_path": "introspection.js", "max_forks_repo_name": "signalnerve/workers-graphql-proxy", "max_forks_repo_forks_event_min_datetime": "2020-06-25T14:45:30.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-31T21:34:16.000Z"}
{"max_stars_count": 40, "max_issues_count": 1, "max_forks_count": 6, "avg_line_length": 16.5214285714, "max_line_length": 91, "alphanum_fraction": 0.5551232166}
import { buildClientSchema, parse } from 'graphql'
import { rawRequest } from 'graphql-request'

const INTROSPECTION = `
query IntrospectionQuery {
  __schema {
    queryType {
      name
      __typename
    }
    mutationType {
      name
      __typename
    }
    subscriptionType {
      name
      __typename
    }
    types {
      ...FullType
      __typename
    }
    directives {
      name
      description
      locations
      args {
        ...InputValue
        __typename
      }
      __typename
    }
    __typename
  }
}
fragment FullType on __Type {
  kind
  name
  description
  fields(includeDeprecated: true) {
    name
    description
    args {
      ...InputValue
      __typename
    }
    type {
      ...TypeRef
      __typename
    }
    isDeprecated
    deprecationReason
    __typename
  }
  inputFields {
    ...InputValue
    __typename
  }
  interfaces {
    ...TypeRef
    __typename
  }
  enumValues(includeDeprecated: true) {
    name
    description
    isDeprecated
    deprecationReason
    __typename
  }
  possibleTypes {
    ...TypeRef
    __typename
  }
  __typename
}
fragment InputValue on __InputValue {
  name
  description
  type {
    ...TypeRef
    __typename
  }
  defaultValue
  __typename
}
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
                __typename
              }
              __typename
            }
            __typename
          }
          __typename
        }
        __typename
      }
      __typename
    }
    __typename
  }
  __typename
}
`

export default async evt => {
  const storedSchemaData = await GRAPHQL_STORE.get('schema')
  if (storedSchemaData) {
    const storedSchema = JSON.parse(storedSchemaData)
    console.log('cached schema hit')
    return buildClientSchema(storedSchema)
  } else {
    const { data } = await rawRequest(ENDPOINT, INTROSPECTION)
    evt.waitUntil(GRAPHQL_STORE.put('schema', JSON.stringify(data), { expirationTtl: 60 }))
    return buildClientSchema(data)
  }
}
