目前,我正试图从graphql-js转换为文字GraphQL类型/模式,我想知道是否有人有过这方面的经验。
让我们来看看这个非常简单的例子:
const Person = new GraphQLObjectType({
name: 'Person',
fields: () => ({
name: {
type: GraphQLString,
description: 'Person name',
},
}),
});我想切换到本机GraphQL模式语法,即
type Person {
# Person name
name: String
}但是,这必须是增量式的,并且考虑到graphql-js的使用,目前最好的解决方案是将GraphQL模板文本解析为GraphQLObjectType (或任何其他类型)。有没有人有这样做的经验,我似乎很遗憾地找不到任何库。
发布于 2018-08-15 03:29:39
import { printType } from 'graphql';
printType(Person)产出:
type Person {
"""Person name"""
name: String
}下面是演示:
import { expect } from 'chai';
import { printType, printSchema, buildSchema, GraphQLSchema } from 'graphql';
import { logger } from '../util';
import { Person } from './';
describe('test suites', () => {
it('convert constructor types to string types', () => {
const stringTypeDefs = printType(Person).replace(/\s/g, '');
logger.info(printType(Person));
const expectValue = `
type Person {
"""Person name"""
name: String
}
`.replace(/\s/g, '');
expect(stringTypeDefs).to.be.equal(expectValue);
});
it('buildSchema', () => {
const stringTypeDefs = printType(Person);
const schema = buildSchema(stringTypeDefs);
expect(schema).to.be.an.instanceof(GraphQLSchema);
});
it('printSchema', () => {
const stringTypeDefs = printType(Person);
const schema = printSchema(buildSchema(stringTypeDefs));
logger.info(schema);
const expectValue = `
type Person {
"""Person name"""
name: String
}
`.replace(/\s/g, '');
expect(schema.replace(/\s/g, '')).to.be.eql(expectValue);
});
});源代码:
发布于 2020-01-03 00:08:58
可以使用石墨cli cli从graphql服务器提取本机graphql架构。你要做的就是..。
npm i -g graphql-cligraphql init文件graphql get-schema,这将在本机graphql中生成模式样本.graphqlconfig
{
"projects": {
"my_sample_project": {
"schemaPath": "schema.graphql",
"extensions": {
"endpoints": {
"local": "http://localhost:8080/graphql"
}
}
}
}
}我们利用graphql模式/查询/突变的自动生成来实现CI工作流。
https://stackoverflow.com/questions/51819635
复制相似问题