在不启动服务器的情况下生成 json 架构?

Generate a json schema without starting a server?

是否有独立的工具可以将模块化的 graphql 模式转换为 json 模式?

我有一个使用 apollo-graphql 和 graphql-tools 的 graphql 服务器 makeExecutableSchema。它遵循描述的模式 here

// schema.js
import { makeExecutableSchema } form 'graphql-tools';
const Author = `type Author { ... }`;
const Post   = `type Post   { ... }`;
const Query  = `type Query  { ... }`;

export const typeDefs = [Author, Post, Query];

export const schema = makeExecutableSchema({
  typeDefs: typeDefs,
  resolvers: { ... },
});

如何创建 schema.json 表单 typeDefsschema


我需要 json 架构才能使用 relay-compilerapollo-codegenapollo-codegen 包含此脚本以从 graphql 服务器创建模式...

apollo-codegen introspect-schema http://localhost:8080/graphql --output schema.json

...但我想在自动构建中创建模式和 运行 apollo-codegen。我不想创建服务器。


I would submit this as an answer, but the question has been marked off-topic ¯\_(ツ)_/¯

@daniel-rearden 的回答为我指明了正确的方向。 makeExecutableSchema returns a GraphQLSchema 因此可以使用 graphqlprintSchemaintrospectionQuery 来获得 json 或 graphql 语言表示模式。

// export.js
import { schema } from './schema.js'
import { graphql, introspectionQuery, printSchema } from 'graphql';

// Save json schema
graphql(schema, introspectionQuery).then(result => {
  fs.writeFileSync(
    `${yourSchemaPath}.json`,
    JSON.stringify(result, null, 2)
  );
});

// Save user readable type system shorthand of schema
fs.writeFileSync(
  `${yourSchemaPath}.graphql`,
  printSchema(schema)
);

graphql-to-json。我相信有一个 CLI 工具可以做到这一点。

或者,您可以编写自己的脚本并使用 node 执行它。您不必启动服务器来 运行 查询,您只需要一个模式,您可以 运行 直接针对它进行查询。您可以查看示例 here.