gpt4 book ai didi

node.js - 如何将 GraphQL 模式转换为 JSON 并最好地转换为 GraphQL 模式?

转载 作者:行者123 更新时间:2023-12-03 12:19:41 30 4
gpt4 key购买 nike

我发现这给了我 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
}

最佳答案

您可以使用以下软件包,我相信在本文发布时该软件包不可用:https://www.npmjs.com/package/graphql-2-json-schema

import {
graphqlSync,
getIntrospectionQuery,
IntrospectionQuery
} from 'graphql';

import { fromIntrospectionQuery } from 'graphql-2-json-schema';

const options = {
ignoreInternals: true,
nullableArrayItems: true
}

const introspection = graphqlSync(schema, getIntrospectionQuery()).data as IntrospectionQuery;

const jsonSchema = fromIntrospectionQuery(introspection, options);

关于node.js - 如何将 GraphQL 模式转换为 JSON 并最好地转换为 GraphQL 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580019/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com