首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用getIntrospectionQuery GraphqlJS v14

如何使用getIntrospectionQuery GraphqlJS v14
EN

Stack Overflow用户
提问于 2018-09-21 04:17:20
回答 1查看 2.3K关注 0票数 2

在过去,我们有一个文件updateSchema.js

代码语言:javascript
复制
```javascript

#!/usr/bin/env babel-节点

从‘fs’导入fs;

从“路径”导入路径;

从‘./data/ schema’导入{schema};

从“graphql”导入{graphql};

从“graphql/实用程序”导入{ introspectionQuery,printSchema };

//保存完整模式自省的JSON供Babel插件使用

(异步() => {

结果=等待(graphql(schema,introspectionQuery));

if (result.errors) {

代码语言:javascript
复制
console.error(
代码语言:javascript
复制
  'ERROR introspecting schema: ',
代码语言:javascript
复制
  JSON.stringify(result.errors, null, 2)
代码语言:javascript
复制
);

}否则{

代码语言:javascript
复制
fs.writeFileSync(
代码语言:javascript
复制
  path.join(__dirname, '../data/schema.json'),
代码语言:javascript
复制
  JSON.stringify(result, null, 2)
代码语言:javascript
复制
);

}

})();

//保存用户可读类型系统模式的简写

fs.writeFileSync(

path.join(__dirname,‘./data/schema.Graphql’),

printSchema(模式)

);

代码语言:javascript
复制
``` 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
复制
```javascript

从‘fs’导入fs;

从“路径”导入路径;

从‘./data/schema’导入{schema};

从“graphql”导入{printSchema};

const schemaPath = path.resolve(__dirname,‘./data/schema.Graphql’);

fs.writeFileSync(schemaPath,printSchema(模式));

Console.log(‘写’+ schemaPath);

代码语言:javascript
复制

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-26 14:21:16

代码语言:javascript
复制
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,
    ),
  );

应该做点什么

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52436537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档