如何使用 getIntrospectionQuery GraphqlJS v14

Eri*_*oyo 2 relay graphql relayjs

过去我们曾经有一个文件 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)
);
Run Code Online (Sandbox Code Playgroud)

``` 生成了schema.jsonschema.graphql文件,现在在 v14 中graphql-js有一个弃用通知,introspectionQuery我们应该使用getIntrospectionQueryv14 changelog而不是使用。

现在updateSchema.js看起来像这个中继示例

``

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);
Run Code Online (Sandbox Code Playgroud)

``

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

tbe*_*rgq 5

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,
    ),
  );
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩