代码之家  ›  专栏  ›  技术社区  ›  gomes

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

  •  0
  • gomes  · 技术社区  · 7 年前

    当前正在尝试从 graphql-js 对于文字图形ql类型/模式,我想知道是否有人有过这方面的经验。

    const Person = new GraphQLObjectType({
    name: 'Person',
      fields: () => ({
        name: {
          type: GraphQLString,
          description: 'Person name',
        },
      }),
    });
    

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

    type Person {
      # Person name 
      name: String
    }
    

    但是,这必须是增量的,并且考虑到 图形js GraphQLObjectType (或其他类型的)。有没有人有这样做的经验,我似乎找不到任何图书馆为它遗憾。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Lin Du Correcter    7 年前
    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);
      });
    });
    

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

        2
  •  0
  •   Ian    6 年前

    你可以用 graphql-cli 从graphql服务器提取本机graphql模式。你只需要。。

    1. 下载工具| npm i -g graphql-cli
    2. 跑步 graphql init 创造 .graphqlconfig图 文件
    3. 启动graphql服务器
    4. graphql get-schema 这将在本机graphql中生成您的模式

    样品

    {
      "projects": {
        "my_sample_project": {
          "schemaPath": "schema.graphql",
          "extensions": {
            "endpoints": {
              "local": "http://localhost:8080/graphql"
            }
          }
        }
      }
    }
    

    我们利用graphql模式的自动生成/ queries/mutations

    推荐文章