Inheritance

There will be times that a lot of types or inputs have some fields in common. In GraphQXL you can inherit fields between types and inputs using spread operators.

Spread operator

types and inputs can inherit fields from other types and inputs using spread operators, for example:

Open in sandbox

Source GraphQXL Compiled GraphQL
type Common {
    "Type's ID"
    id: ID!
    "Type's Name"
    name: String!
    "Type's description"
    description: String!
}

type Product {
    ...Common
    "Product's price"
    price: Float!
}





type Common {
    "Type's ID"
    id: ID!
    "Type's Name"
    name: String!
    "Type's description"
    description: String!
}

type Product {
    "Type's ID"
    id: ID!
    "Type's Name"
    name: String!
    "Type's description"
    description: String!
    "Product's price"
    price: Float!
}

Private fields

It is very common that you do not want to expose the Common type in the public API, so you can make it private by prefixing the name with a _ character (or the prefix that you provide in the CLI argument):

Open in sandbox

Source GraphQXL Compiled GraphQL
type _Common {
    "Type's ID"
    id: ID!
    "Type's Name"
    name: String!
    "Type's description"
    description: String!
}

type Product {
    ..._Common
    "Product's price"
    price: Float!
}
type Product {
    "Type's ID"
    id: ID!
    "Type's Name"
    name: String!
    "Type's description"
    description: String!
    "Product's price"
    price: Float!
}




Inheriting interfaces

A common pattern is to declare a GraphQL interface and to implement it in a type, but you need to rewrite all the fields in the type that belong to the interface as they are not implicit. You can use the spread operator also with interfaces:

Open in sandbox

Source GraphQXL Compiled GraphQL
interface Person {
    parent: String!
    childs: [String!]!
}

type Dad implements Person {
    ...Person
    job_title: String!
}

type Kid implements Person {
    ...Person
    school_name: String!
}


interface Person {
    parent: String!
    childs: [String!]!
}

type Dad implements Person {
    parent: String!
    childs: [String!]!
    job_title: String!
}

type Kid implements Person {
    parent: String!
    childs: [String!]!
    school_name: String!
}