Skip to content

Update graphql-js to 15.4.0#4801

Closed
Betree wants to merge 2 commits intomasterfrom
deps/graphql-update
Closed

Update graphql-js to 15.4.0#4801
Betree wants to merge 2 commits intomasterfrom
deps/graphql-update

Conversation

@Betree
Copy link
Copy Markdown
Member

@Betree Betree commented Nov 2, 2020

For graphql/graphql-js#2834

WIP script for updating schema, including deprecated fields:

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]);

@Betree
Copy link
Copy Markdown
Member Author

Betree commented Jan 6, 2021

Fix hasn't been released yet, it should be in next version

@Betree
Copy link
Copy Markdown
Member Author

Betree commented Jan 27, 2021

Will continue #5191

@Betree Betree closed this Jan 27, 2021
@kewitz kewitz deleted the deps/graphql-update branch January 24, 2024 15:32
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