ballerina-platform / ballerina-library

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

GraphQL Service Crashes when Input Object Type Variable Value Includes an Additional Field #7317

Closed DimuthuMadushan closed 3 weeks ago

DimuthuMadushan commented 3 weeks ago

Description

GraphQL service crashes when the input object type variable value includes an additional field.

Steps to Reproduce

Consider the below service:

import ballerina/graphql;

type NewProfile record {|
    string name;
    int age;
|};

type Profile readonly & record {|
    *NewProfile;
    int id;
|};

table<Profile> key(id) profiles = table [];

service /graphql on new graphql:Listener(9090) {
    remote function addProfile(NewProfile newProfile) returns Profile {
        Profile profile = {id: profiles.nextKey(), ...newProfile};
        profiles.add(profile);
        return profile;
    }

    resource function get profiles() returns table<Profile> {
        return profiles;
    }
}

Service is crashing for the following GraphQL query.

mutation($profile: NewProfile!){
  addProfile(newProfile:$profile) {
    id
    name
    age
  }
}

variable:

{
  "profile": {
    "name": "gql",
    "age": 4,
    "address": {}
  }
}

output:

error: {ballerina/lang.map}KeyNotFound {"message":"invalid field access: field 'address' not found in record type 'input_obj:NewProfile'"}
        at ballerina.graphql.1.Engine:executeMutationMethod(engine.bal:484)
           ballerina.graphql.1.Engine:resolveRemoteMethod(engine.bal:383)
           ballerina.graphql.1.Engine:getFieldValue(engine.bal:434)
           ballerina.graphql.1.Engine:resolve(engine.bal:343)
           ballerina.graphql.1.ExecutorVisitor:execute(executor_visitor.bal:116)
           ballerina.graphql.1:$anon$method$delegate$ExecutorVisitor.execute$0(executor_visitor.bal:78)
           ballerina.graphql.1.ExecutorVisitor:visitField(executor_visitor.bal:81)
           ballerina.graphql.parser.1.FieldNode:accept(field_node.bal:38)
           ballerina.graphql.1.ExecutorVisitor:visitOperation(executor_visitor.bal:50)
           ballerina.graphql.parser.1.OperationNode:accept(operation_node.bal:41)
           ballerina.graphql.1.Engine:getResult(engine.bal:133)
           ballerina.graphql.1:getResponseFromExecution(listener_utils.bal:101)
           ballerina.graphql.1:getResponseFromQuery(listener_utils.bal:89)
           ballerina.graphql.1:getResponseFromJsonPayload(listener_utils.bal:66)
           ballerina.graphql.1:handlePostRequests(listener_utils.bal:43)
           ballerina.graphql.1.$anonType$_15:$post$.(listener_utils.bal:348)

Version

2201.9.x

Environment Details (with versions)

No response