Skip to content

Transpile Your Graphql Schema To Support Type Inheritance

Notifications You must be signed in to change notification settings

wenfzhao/graphql-schema-transpiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-schema-transpiler

Transpile Your GraphQL Schema To Support Type Inheritance This is a transpiler to allow graphql schema to have inheritance between type, input and interface. Allows single/multiple inheritance among type, input, and interface to reduce/elimate repeated code and make writing schema much easier and cleaner.

Features

  • Inheritance for type, input, interface
  • When implements interface, all fields are copied over
  • Allow inheritance between type and input

Installation

npm install graphql-schema-transpiler --save

API

transpileSchema(schema[, options])

Transpile the schema to stardard GraphQL Schema.

schema

string - schema string to transpile

option

options.addMissingTypesAndInputs

bool? - When inheriting between type and input, fields will be copied with the correct type. Fields with output type in parent will be changed to input type when copied to child of input type

options.debug

bool? - enable debug log

How To Use It

const { transpileSchema } = require('graphql-schema-transpiler');
//or
// import { transpileSchema } from 'graphql-schema-transpiler';

const schema = `

interface Searchable {
  searchQuery: String!
}

type Person {
  firstName: String!
  middleName: String
  lastName: String!
}

type Author extends Person implements Searchable {
  email: String
}

type Book implements Searchable {
  id: Int!
  title: String!
  authors: [Author] 
}

# provide default to override inheritted field
input BookInput extends Book {
  id: Int
}

`

//transpile schema string
const transpiledSchema = transpileSchema(schema, {
  addMissingTypesAndInputs: true,
});

//will transpile into
const transpiledSchema = `

interface Searchable {
  searchQuery: String!
}

type Person {
  firstName: String!
  middleName: String
  lastName: String!
}

type Author implements Searchable {
  email: String
  firstName: String!
  middleName: String
  lastName: String!
  searchQuery: String!
}

type Book implements Searchable {
  id: Int!
  title: String!
  authors: [Author]
  searchQuery: String!
}

# provide default to override inheritted field
input BookInput {
  id: Int
  title: String!
  authors: [AuthorInput]
  searchQuery: String!
}

input AuthorInput implements Searchable {
  email: String
  firstName: String!
  middleName: String
  lastName: String!
  searchQuery: String!
}

`

Type Inheritance

Single Inheritance

const schema = `

type Person {
  firstName: String!
  middleName: String
  lastName: String!
}

type Employee extends Person {
  jobTitle: String!
  company: String!
}
`

Multiple Inheritance

const schema = `

type Node {
  id: ID!
}

type Address {
  streetAddress: String
  city: String
  state: String
}

type Person extends Node, Address {
}

`

Inheritance Between Type And Input

const schema = `

input Address {
  id: Int!
  streetAddress: String
  city: String
  state: String
}

type AddressInput extends Address {
  id: Int
}

`

Interface Inheritance

const schema = `

interface Element {
  id: ID!
}

interface DomElement extends Element {
  path: String!
}

`

Implementation

const schema = `

interface Element {
  id: ID!
}

type DomElement implements Element {
  path: String!
}

`

//will transpile into
const transpiledSchema = `

interface Element {
  id: ID!
  name: String!
}

type DomElement implements Element {
  id: ID!
  name: String!
  path: String!
}

`

About

Transpile Your Graphql Schema To Support Type Inheritance

Resources

Stars

Watchers

Forks

Packages

No packages published