#Need Help with resolving this issue.
1 messages · Page 1 of 1 (latest)
@rigid heron
In GraphQL, you cannot use a same type for both input and outputs. Instead you can create 2 different record types and use them as input and output.
type NewDepartment record {
string name;
};
type Department record {
@graphql:ID string id;
string id;
string name;
};
isolated remote function addDepartment(NewDepartment department) returns string|error {
...
}
isolated resource function get department() returns Department[]|error {
...
}
Like this.
@vocal epoch @fervent dune @ebon bay can provide more context here i think.
Yes, as @iron socket mentioned, you cannot use the same type as an input and an output in a GraphQL service.
In GraphQL, input and output objects have different semantics. That's reason behind this error. As suggested, you can resolve the issue by defining two separate types. (Adding an ID for object types is a good practice in most cases).
`type Department record {
@graphql:ID
int id;
string name;
};
type DepartmentDetails record {
int id;
string name;
};
resource function get department() returns Department[]|error {
stream<DepartmentDetails, error?> datastream = check db->find(departmentCollection, databaseName, {}, {});
return from DepartmentDetails department in datastream
select department;
}`
I still dont undertand
The issue here is, in the function addDepartment() you have use Department record and an input type (function input parameter). But in the function department(), you are returning the same record type Deparment which is an output type. In GraphQL, input and output objects cannot be the same.
So using Department as the output type and using NewDepartment as the input types will solve this problem.