Coder Social home page Coder Social logo

graph-schema-json-writer's Introduction

GraphQL schema JSON Writer

Extension to graphSchemaToJson to write the schema generated from a GraphQL type def to various outputs:

  • Typescript source files
  • GraphQL type def files

Also includes accessor functionality to better work with the schema object generated.

Usage

{
  Person: {
    fields: {
      name: {
        type: 'String',
        directives: {},
        isNullable: false,
        isList: false
      },
      age: {
        type: 'Int',
        directives: {
          range: {
            min: 0,
            max: 130
          }
        },
        isNullable: false,
        isList: false
      },
      gender: {
        type: 'Gender',
        directives: {},
        isNullable: false,
        isList: false
      }
    },
    directives: {},
    type: 'Object',
    implements: []
  },
  Gender: {
    fields: ['male', 'female'],
    directives: {},
    type: 'Enum'
  }
}

Accessor

import { schemaToJS } from "../src/schema";
import { accessor } from "graph-schema-json-writer";
const { schemaByType, filteredSchema } = accessor;
/// ... generate JSON schema
const jsSchema = schemaToJS(schema);

// schema where all entries with keys starting with __ are filtered out
const filteredMap = filteredSchema(jsSchema);

// soreted by type
const typeMap = schemaByType(jsSchema);
console.log(typeMap);
{
    Object: {
        Person: {
            // ....
        }
    },
    Enum: {
        Gender: {
            // ...
        }
    }
}

Writer

import { schemaToJS } from "../src/schema";
import { writer } from "graph-schema-json-writer";
const { writeToTypeDef } = writer;
/// ... generate JSON schema
const jsSchema = schemaToJS(schema);

// schema where all entries with keys starting with __ are filtered out
const typeDef = writeToTypeDef(jsSchema);
console.log(typeDef);

Should output the (original) GraphqL type def, nicely formatted:

type Person {
  name: String!
  age: Int! @range(min: 0, max: 130)
  gender: Gender!
}

enum Gender {
  male
  female
}

Writing typescript source files

The writer also supports writing a TypeScript class, complete with:

  • extends class
  • implements interfaces
  • decorators for class itself and fields and properties
    • imports for the decorators, interfaces and class extended
import { schemaToJS } from "../src/schema";
import { writer, createClass } from "graph-schema-json-writer";
/// ... generate JSON schema
const jsSchema = schemaToJS(schema);
const classType = createClass(jsSchema);

const importsMap = {
  Range: "class-validator",
  BaseEntity: "typeorm",
  Entity: "typeorm"
};
const body = classType.writeClass("Person", jsSchema.Person, {
  importsMap
});
console.log(sourceFileTxt);

Output a TypeScript class with decorators

import { BaseEntity, Entity } from 'typeorm';
import { Range } from 'class-validator';

@Entity()
class Person extends BaseEntity {
  name: string

  @Range(min: 0, max: 130)
  age: number
}

Writing source files

Use the SourceFile class to write source files to disk:

import { createSoureFileWriter, importsMap } from "graph-schema-json-writer";

// include typical import maps, such as for typeorm and class-validator
const myImportsMap = {
  ...importsMap.all
  // ...custom additions or overrides
};
const writeOpts = {
  importsMap: myImportsMap,
  srcFileDir: fs.join(__dirname, "db/model"),
  only: ["Person", "Gender"],
  enumRefs: true, // resolve enumRefs using importsMap
  // classRefs true, // resolve classRefs using importsMap
  validate: true // validate that each const reference has an entry in importsMap
};

const srcFileWriter = createSoureFileWriter(writeOpts);
await srcFileWriter.writeTypeDefs(jsSchema, writeOpts);
await srcFileWriter.writeIndexFiles();

Files written, assuming __dirname is /src, using default flat strategy:

  /src
    /db
      /models
        Person.ts
        Gender.ts
        index.ts

Using defaults:

import { writeTypeDefs, importsMap } from "graph-schema-json-writer";
// ...
await writeTypeDefs(jsSchema, {
  importsMap: importsMap.all,
  srcFileDir: fs.join(__dirname, "db/model"),
  strategy: "type-folder",
  only: ["Person", "Gender"],
  index: true // also add index files
});

Files written using type-folder strategy:

  /src
    /db
      /models
        /class
          Person.ts
          index.ts
        /enum/
          Gender.ts
          index.ts
        index.ts

Note: The SourceFileWriter has not yet been fully tested and could use some futher refactoring. It should include enough building blocks for you to compose a solution to fit your needs ;)

Imports map

Imports maps now have support for aliases

importsMap = {
  range: { name: "Range", importPath: "validator" }
};

Will result in an aliased import:

import { Range as range } from 'validator';

Use cases

This class writer could be used for writing classed for TypeORM, NestJS or TypeGraphQL etc.

Note that the class writer supports passing decorators in place of directives.

License

MIT

graph-schema-json-writer's People

Contributors

kristianmandrup avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.