ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 65 forks source link

Improve Validation Logic in GraphQL Union Types #1285

Closed ThisaruGuruge closed 3 years ago

ThisaruGuruge commented 3 years ago

Description: When a field in a GraphQL schema is a union type, the user should define a separate type for that particular union type. Currently, the compiler plugin allows to directly return union types, but it should be throwing a compiler error.

Describe your problem(s) Following should be valid:

import ballerina/graphql;

type Person record {
    string name;
};

type Student record {
    int age;
};

type PersonOrStudent Person|Student;

service /graphql on new graphql:Listener(9090) {
    resource function get profile() returns PersonOrStudent {
        // ...
    }
}

But the following code should be invalid:

import ballerina/graphql;

type Person record {
    string name;
};

type Student record {
    int age;
};

service /graphql on new graphql:Listener(9090) {
    resource function get profile() returns Person|Student {
        // ...
    }
}
ThisaruGuruge commented 3 years ago

Having second thoughts about this.

@shafreenAnfar Should we not allow to return union types directly, or should we not support it?

The GraphQL way is to define Union types separately, and use them. But we can support it without defining union type separately since we have the language level support for unions.

WDYT?

ThisaruGuruge commented 3 years ago

Closing this since we are going to support returning union types directly from GraphQL resources.