如何使用 getIntrospectionQuery GraphqlJS v14

How to use getIntrospectionQuery GraphqlJS v14

以前我们有一个文件updateSchema.js

```

#!/usr/bin/env babel-node

import fs from 'fs';
import path from 'path';
import { schema } from '../data/schema';
import { graphql } from 'graphql';
import { introspectionQuery, printSchema } from 'graphql/utilities';

// Save JSON of full schema introspection for Babel Relay Plugin to use
(async () => {
  const result = await (graphql(schema, introspectionQuery));
  if (result.errors) {
    console.error(
      'ERROR introspecting schema: ',
      JSON.stringify(result.errors, null, 2)
    );
  } else {
    fs.writeFileSync(
      path.join(__dirname, '../data/schema.json'),
      JSON.stringify(result, null, 2)
    );
  }
})();

// Save user readable type system shorthand of schema
fs.writeFileSync(
  path.join(__dirname, '../data/schema.graphql'),
  printSchema(schema)
);

``` 这生成了 schema.jsonschema.graphql 文件,现在在 graphql-js 的 v14 中有一个弃用通知,我们应该使用 getIntrospectionQuery[=20 而不是使用 introspectionQuery =].

现在 updateSchema.js 看起来像这样 relay-examples:

```

import fs from 'fs';
import path from 'path';
import {schema} from '../data/schema';
import {printSchema} from 'graphql';

const schemaPath = path.resolve(__dirname, '../data/schema.graphql');

fs.writeFileSync(schemaPath, printSchema(schema));

console.log('Wrote ' + schemaPath);

```

我们现在应该如何生成 schema.json 文件?

const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const {
  buildClientSchema,
  getIntrospectionQuery,
  printSchema,
} = require('graphql/utilities');

fetch('url.to.your.server', {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    query: getIntrospectionQuery(),
  }),
})
  .then(res => res.json())
  .then(schemaJSON => printSchema(buildClientSchema(schemaJSON.data)))
  .then(clientSchema =>
    fs.writeFileSync(
      path.join(__dirname, '..', 'schema.graphql'),
      clientSchema,
    ),
  );

应该可以解决问题