Skip to content

Rollback graphql-js to 15.3.0#4789

Merged
Betree merged 1 commit intomasterfrom
fix/graphql-lint-deprecated
Oct 30, 2020
Merged

Rollback graphql-js to 15.3.0#4789
Betree merged 1 commit intomasterfrom
fix/graphql-lint-deprecated

Conversation

@Betree
Copy link
Copy Markdown
Member

@Betree Betree commented Oct 30, 2020

@Betree Betree self-assigned this Oct 30, 2020
@Betree Betree merged commit fa29d22 into master Oct 30, 2020
@Betree Betree deleted the fix/graphql-lint-deprecated branch October 30, 2020 13:13
@Betree
Copy link
Copy Markdown
Member Author

Betree commented Oct 30, 2020

As reference, pasting here the script that I started implementing for fetching the schema.

Details
import fetch from 'node-fetch';
import * as fs from 'fs';
import * as path from 'path';
import mkdirp from 'mkdirp';
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery';
import { buildClientSchema } from 'graphql/utilities/buildClientSchema';
import { printSchema } from 'graphql/utilities/printSchema';

interface Options {
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  headers?: { [key: string]: string };
  json?: boolean;
}

/**
 *
 * Fetch remote schema and turn it into string
 *
 * @param endpoint
 * @param options
 */
export async function getRemoteSchema(
  endpoint: string,
  options: Options,
): Promise<{ status: 'ok'; schema: string } | { status: 'err'; message: string }> {
  try {
    const introspectionQuery = getIntrospectionQuery();
    const { data, errors } = await fetch(endpoint, {
      method: options.method,
      headers: options.headers,
      body: JSON.stringify({ query: introspectionQuery }),
    }).then(res => res.json());

    if (errors) {
      return { status: 'err', message: JSON.stringify(errors, null, 2) };
    }

    if (options.json) {
      return {
        status: 'ok',
        schema: JSON.stringify(data, null, 2),
      };
    } else {
      const schema = buildClientSchema(data);
      return {
        status: 'ok',
        schema: printSchema(schema),
      };
    }
  } catch (err) {
    return { status: 'err', message: err.message };
  }
}

/**
 *
 * Prints schema to file.
 *
 * @param dist
 * @param schema
 */
export function printToFile(
  dist: string,
  schema: string,
): { status: 'ok'; path: string } | { status: 'err'; message: string } {
  try {
    const output = path.resolve(process.cwd(), dist);

    if (!fs.existsSync(output)) {
      mkdirp.sync(output);
    }
    fs.writeFileSync(output, schema);

    return { status: 'ok', path: output };
  } catch (err) {
    return { status: 'err', message: err.message };
  }
}

export async function main(endpoint): Promise<void> {
  /* Headers */
  const defaultHeaders = {
    'Content-Type': 'application/json',
  };

  /* Fetch schema */
  const schema = await getRemoteSchema(endpoint, {
    method: 'POST',
    headers: defaultHeaders,
    json: false,
  });

  if (schema.status === 'err') {
    console.error(schema.message);
  } else {
    console.log(schema.schema);
  }
}

main(process.argv[2]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant