GraphZahl icon indicating copy to clipboard operation
GraphZahl copied to clipboard

Better support for generics

Open nerdsupremacist opened this issue 5 years ago • 3 comments
trafficstars

Currently Generics are not very easy to use...

The users usually have to define their own type names and more importantly. We end up creating one version per generic parameter used.

We should extract the non generic stuff and put it in an interface

nerdsupremacist avatar Apr 01 '20 10:04 nerdsupremacist

Example of what I mean:

If I write

class Foo<Bar> {
    let bar: Bar
    let baz: String
    let item: Int
    
    func other() -> [Bar] { ... } 
}

And in our Schema we use: Foo<Bool> and Foo<Double?>

I want a schema that looks like this:

interface FooBase {
    baz: String!
    item: Int!
}

type FooOfBoolean implements FooBase {
    baz: String!
    item: Int!
    bar: Boolean!
    other: [Boolean!]!
}

type FooOfOptionalOfFloat implements FooBase {
    baz: String!
    item: Int!
    bar: Float
    other: [Float]!
}

nerdsupremacist avatar Apr 17 '20 14:04 nerdsupremacist

This should also be compatible with our strategies for resolving subclassing:

So if we also have:

class SpecificFoo<T>: Foo<T> {
    let specific: T?
    let someData: Bool
}

then our final schema should be:

// Bases
interface FooBase {
    baz: String!
    item: Int!
}

interface SpecificFooBase {
    baz: String!
    item: Int!
    someData: Boolean!
}

// Interface Definitions of Foo superclass
interface FooOfBoolean {
    baz: String!
    item: Int!
    bar: Boolean!
    other: [Boolean!]!
}

interface FooOfOptionalOfFloat {
    baz: String!
    item: Int!
    bar: Float
    other: [Float]!
}

// Concrete Implementations of Foo super class
type __FooOfBoolean implements FooBase, FooOfBoolean {
    baz: String!
    item: Int!
    bar: Boolean!
    other: [Boolean!]!
}

type __FooOfOptionalOfFloat implements SpecificFooBase, FooBase, FooOfOptionalOfFloat {
    baz: String!
    item: Int!
    bar: Float
    other: [Float]!
}

// Concrete Implementations of Foo subclass
type SpecificFooOfBoolean implements SpecificFooBase, FooBase, FooOfBoolean {
    // Super Class
    baz: String!
    item: Int!
    bar: Boolean!
    other: [Boolean!]!

    // Subclass
    someData: Boolean!
    specific: Boolean
}

type SpecificFooOfOptionalOfFloat implements SpecificFooBase, FooBase, FooOfOptionalOfFloat {
    // Super Class
    baz: String!
    item: Int!
    bar: Float
    other: [Float]!
    
    // Subclass
    someData: Boolean!
    specific: Float
}

nerdsupremacist avatar Apr 17 '20 15:04 nerdsupremacist

Or move to using this as soon as its possible: https://github.com/graphql/graphql-spec/issues/190

nerdsupremacist avatar Apr 17 '20 16:04 nerdsupremacist