首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从graphql切换到本机graphql模式?

从graphql切换到本机graphql模式?
EN

Stack Overflow用户
提问于 2018-08-13 09:52:40
回答 2查看 91关注 0票数 1

目前,我正试图从graphql-js转换为文字GraphQL类型/模式,我想知道是否有人有过这方面的经验。

让我们来看看这个非常简单的例子:

代码语言:javascript
复制
const Person = new GraphQLObjectType({
name: 'Person',
  fields: () => ({
    name: {
      type: GraphQLString,
      description: 'Person name',
    },
  }),
});

我想切换到本机GraphQL模式语法,即

代码语言:javascript
复制
type Person {
  # Person name 
  name: String
}

但是,这必须是增量式的,并且考虑到graphql-js的使用,目前最好的解决方案是将GraphQL模板文本解析为GraphQLObjectType (或任何其他类型)。有没有人有这样做的经验,我似乎很遗憾地找不到任何库。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-15 03:29:39

代码语言:javascript
复制
import { printType } from 'graphql';

printType(Person)

产出:

代码语言:javascript
复制
type Person {
  """Person name"""
  name: String
}

下面是演示:

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

源代码:

https://github.com/mrdulin/nodejs-graphql/blob/master/src/convert-constructor-types-to-string-types/index.spec.ts

票数 1
EN

Stack Overflow用户

发布于 2020-01-03 00:08:58

可以使用石墨cli cli从graphql服务器提取本机graphql架构。你要做的就是..。

  1. 下载工具\ npm i -g graphql-cli
  2. 在项目目录中运行.graphqlconfig以创建graphql init文件
  3. 启动graphql服务器
  4. 运行graphql get-schema,这将在本机graphql中生成模式

样本.graphqlconfig

代码语言:javascript
复制
{
  "projects": {
    "my_sample_project": {
      "schemaPath": "schema.graphql",
      "extensions": {
        "endpoints": {
          "local": "http://localhost:8080/graphql"
        }
      }
    }
  }
}

我们利用graphql模式/查询/突变的自动生成来实现CI工作流。

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

https://stackoverflow.com/questions/51819635

复制
相关文章

相似问题

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