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

Add Mutation Support in GraphQL Services #360

Merged
merged 12 commits into from
Aug 5, 2021
Merged

Conversation

ThisaruGuruge
Copy link
Member

@ThisaruGuruge ThisaruGuruge commented Aug 2, 2021

Purpose

With this PR, the Ballerina GraphQL services can support mutation operations. Mutations are supported through remote functions.

Fixes: #1492

Examples

Consider the following service:

import ballerina/graphql;

service /graphql on new graphql:Listener(9000) {
    private Person p;

    function init() {
        Address a =  { number: 308, street: "Negra Arroyo Lane", city: "Albuquerque" };
        self.p = new(1, "Walter White", 51, a);
    }

    resource function get person() returns Person {
        return self.p;
    }

    remote function updateName(string name) returns Person {
        self.p.setName(name);
        return self.p;
    }

    remote function updateAge(int age) returns Person {
        self.p.setAge(age);
        return self.p;
    }

    remote function updateCity(string city) return Person {
        self.p.updateCity(city);
        return self.p;
    }
}

public distinct service class Person {
    private final int id;
    private string name;
    private int age;
    private Address address;

    public function init(int id, string name, int age, Address address) {
        self.id = id;
        self.name = name;
        self.age = age;
        self.address = address;
    }

   resource function get id() returns int {
        return self.id;
    }

   resource function get name() returns string {
        return self.name;
    }

   function setName(string name) {
        self.name = name;
    }

    isolated resource function get subject() returns string {
        return self.subject;
    }

   resource function get address() returns Address {
        return self.address;
    }

    remote function updateCity(string city) {
        self.address.city = city;
    }

   function setAge(int age) {
        self.age = age;
    }
}

public type Address record {|
    int number;
    string street;
    string city;
|};

This generates the following schema:

type Query {
    person: Person!
}

type Mutation {
    updateName(name: String!): Person!
    updateAge(age: Int!): Person!
    updateCity(city: String!): Person!
}

type Person {
    id: Int!
    name: String!
    age: Int!
    address: Address!
}

type Address {
    number: Int!
   street: String!
    city: String!
}

This can be queried using the following document:

mutation {
    updateName(name: "Mr. Lambert") {
        name
        address {
            city
        }
    }

    updateCity(city: "New Hampshire") {
        name
        address {
            city
        }
    }
}

The result will be:

{
  "data": {
    "updateName": {
      "name": "Mr. Lambert",
      "address": {
        "city": "Albuquerque"
      }
    },
    "updateCity": {
      "name": "Mr. Lambert",
      "address": {
        "city": "New Hampshire"
      }
    }
  }
}

Note the fields are updated sequentially. The first mutation field changes the name, and the second field changes the city. These two operations are executed sequentially to avoid race conditions.

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests

@codecov
Copy link

codecov bot commented Aug 2, 2021

Codecov Report

Merging #360 (4a1f6fa) into master (6e5b481) will increase coverage by 0.17%.
The diff coverage is 95.78%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #360      +/-   ##
============================================
+ Coverage     93.76%   93.94%   +0.17%     
- Complexity      230      246      +16     
============================================
  Files            48       48              
  Lines          2326     2394      +68     
  Branches       1066     1082      +16     
============================================
+ Hits           2181     2249      +68     
+ Misses          131      130       -1     
- Partials         14       15       +1     
Impacted Files Coverage Δ
ballerina/validator_visitor.bal 93.57% <92.85%> (+0.75%) ⬆️
...ina/stdlib/graphql/runtime/schema/FieldFinder.java 98.69% <94.73%> (+0.14%) ⬆️
...allerina/stdlib/graphql/runtime/engine/Engine.java 95.18% <95.23%> (-1.38%) ⬇️
ballerina/engine_utils.bal 100.00% <100.00%> (ø)
ballerina/executor_visitor.bal 90.00% <100.00%> (+0.81%) ⬆️
ballerina/listener_utils.bal 95.55% <100.00%> (+1.43%) ⬆️
...ina/stdlib/graphql/runtime/engine/EngineUtils.java 95.71% <100.00%> (+0.25%) ⬆️
...dlib/graphql/runtime/engine/ResponseGenerator.java 97.11% <100.00%> (ø)
.../graphql/runtime/schema/SchemaRecordGenerator.java 100.00% <100.00%> (ø)
...rina/stdlib/graphql/runtime/schema/TypeFinder.java 95.55% <100.00%> (+0.61%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e5b481...4a1f6fa. Read the comment docs.

@shafreenAnfar shafreenAnfar merged commit 6f9018a into master Aug 5, 2021
@ThisaruGuruge ThisaruGuruge deleted the mutation-support branch August 5, 2021 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Mutation Operation
3 participants