Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Intersection Types in GraphQL Services #2620

Closed
ThisaruGuruge opened this issue Jan 25, 2022 · 0 comments · Fixed by ballerina-platform/module-ballerina-graphql#605
Closed
Assignees
Labels
module/graphql Issues related to Ballerina GraphQL module Points/4 Team/PCM Protocol connector packages related issues Type/Improvement

Comments

@ThisaruGuruge
Copy link
Member

ThisaruGuruge commented Jan 25, 2022

Description:
Currently, GraphQL services cannot use intersection types (with readonly & T) as inputs. This should be allowed.

The following service should be valid.

import ballerina/graphql;

public type Person record {
    string name;
    int age;
};

public type Student record {
    string name;
    string subject;
};

public type Book readonly & record {
    string name;
    string author;
};

public type Movie readonly & record {
    string name;
    string director;
};

service /graphql on new graphql:Listener(4000) {

    // Intersection with a type reference as an input
    isolated resource function get name(Person & readonly person) returns string {
        return person.name;
    }

    // Intersection with a type reference as an output
    isolated resource function get subject() returns Student & readonly {
        Student student = {name: "Jesse Pinkman", subject: "Arts and Crafts"};
        return student.cloneReadOnly();
    }

    // type reference of an intersection type as an output
    isolated resource function get director() returns Movie {
        Movie movie = {name: "Pulp Fiction", director: "Quentin Tarantino"};
        return movie;
    }

    // type reference of an intersection type as an input
    isolated resource function get author(Book book) returns string {
        return book.author;
    }

    // Intersection with a type reference array as an input
    isolated resource function get names(Person[] & readonly people) returns string[] {
        return people.'map(person => person.name);
    }

    // Intersection with a type reference array as an output
    isolated resource function get subjects() returns Student[] & readonly {
        Student[] students = [{name: "Jesse Pinkman", subject: "Arts and Crafts"}];
        return students.cloneReadOnly();
    }

    // type reference of an intersection type array as an output
    isolated resource function get directors() returns Movie[] {
        Movie[] movies = [{name: "Pulp Fiction", director: "Quentin Tarantino"}];
        return movies;
    }

    // type reference of an intersection type array as an input
    isolated resource function get authors(Book[] books) returns string[] {
        return books.map(book => book.author);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/graphql Issues related to Ballerina GraphQL module Points/4 Team/PCM Protocol connector packages related issues Type/Improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant