在过去,我们有一个文件updateSchema.js
```javascript#!/usr/bin/env babel-节点
从‘fs’导入fs;
从“路径”导入路径;
从‘./data/ schema’导入{schema};
从“graphql”导入{graphql};
从“graphql/实用程序”导入{ introspectionQuery,printSchema };
//保存完整模式自省的JSON供Babel插件使用
(异步() => {
结果=等待(graphql(schema,introspectionQuery));
if (result.errors) {
console.error( 'ERROR introspecting schema: ', JSON.stringify(result.errors, null, 2));}否则{
fs.writeFileSync( path.join(__dirname, '../data/schema.json'), JSON.stringify(result, null, 2));}
})();
//保存用户可读类型系统模式的简写
fs.writeFileSync(
path.join(__dirname,‘./data/schema.Graphql’),
printSchema(模式)
);
``` That generated the `schema.json` and `schema.graphql` files, now in the v14 of `graphql-js` theres a deprecation notice that instead of using `introspectionQuery` we should use `getIntrospectionQuery`[v14 changelog](https://github.com/graphql/graphql-js/releases/tag/v14.0.0).现在updateSchema.js看起来像这个继电器-实例
```javascript从‘fs’导入fs;
从“路径”导入路径;
从‘./data/schema’导入{schema};
从“graphql”导入{printSchema};
const schemaPath = path.resolve(__dirname,‘./data/schema.Graphql’);
fs.writeFileSync(schemaPath,printSchema(模式));
Console.log(‘写’+ schemaPath);
我们现在应该如何生成schema.json文件?
发布于 2018-09-26 14:21:16
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,
),
);应该做点什么
https://stackoverflow.com/questions/52436537
复制相似问题