UNPKG

@magidoc/rollup-plugin-gql-schema

Version:

A Rollup and ViteJS plugin that allows to parse a GraphQL Schema from a target URL and save it to a target output folder, or to parse it from the disk and convert it to a desired format.

32 lines (29 loc) 1.04 kB
import axios from 'axios'; import { getIntrospectionQuery, buildClientSchema } from 'graphql'; async function queryGraphQLSchema(url, parameters) { const actualMethod = parameters.method || 'POST'; const body = JSON.stringify({ operationName: 'IntrospectionQuery', query: parameters.query ?? getIntrospectionQuery().trim(), variables: null, }); return axios({ url: url, method: actualMethod, headers: { 'Content-Type': 'application/json', ...(parameters.headers || {}), }, data: body, responseType: 'json', }) .then((res) => res.data) .then((res) => { if (res.errors && res.errors.length > 0) { throw new Error(`Introspection query failed: \n Method: ${actualMethod} \n Response: ${JSON.stringify(res)} \n\n Query: ${body.replaceAll('\\n', '\n')}`); } return buildClientSchema(res.data); }); } export { queryGraphQLSchema as default }; //# sourceMappingURL=query.js.map