我已经搞清楚了,这会给我GraphQLSchema
,我希望我可以将其转换为JSON。如何操作呢?
// @flow
import path from 'path';
import fs from 'fs';
import {
buildSchema,
} from 'graphql';
const main = () => {
const schemaPath = path.resolve(__dirname, '../schema.graphql');
console.log(buildSchema(fs.readFileSync(schemaPath, 'UTF8')));
};
main();
如何实现反向转换 - 如何将GraphQL模式的JSON表示转换为GraphQL标记?
我找到的一种方法是:
import {
printSchema,
buildSchema,
buildClientSchema,
printIntrospectionSchema,
introspectionFromSchema,
} from 'graphql';
printSchema(buildClientSchema(introspectionFromSchema(buildSchema(fs.readFileSync('./schema.graphql', 'UTF8')))))
然而,这样会丢失很多数据,例如:
type Venue implements Node @preLoad {
# test
id: ID!
fuid: String!
url: String!
name: String!
cinema: Cinema!
futureEventCount: Int!
globalCinemaVenue: GlobalCinemaVenue
}
成为:
type Venue implements Node {
id: ID!
fuid: String!
url: String!
name: String!
cinema: Cinema!
futureEventCount: Int!
globalCinemaVenue: GlobalCinemaVenue
}