Skip to content

Commit

Permalink
I think I figured it out.
Browse files Browse the repository at this point in the history
Its not done, but the toRunArrayResolverFunctions has to unpack things returned
in it.
  • Loading branch information
burner committed Aug 4, 2023
1 parent c4d35ee commit fadd394
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 47 deletions.
120 changes: 75 additions & 45 deletions source/graphql/graphql.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ version(LDC) {
import std.traits;
import std.meta : AliasSeq;
import std.range.primitives : popBack;
import std.algorithm.iteration : filter;
import std.range : iota;
import std.algorithm.iteration : filter, map;
import std.algorithm.searching : canFind;
import std.format : format;
import std.exception : enforce;
Expand Down Expand Up @@ -453,47 +454,73 @@ class GraphQLD(T, QContext = DefaultContext) {
);
if(tmp.type == Json.Type.object) {
if("data" in tmp) {
ret["data"] ~= tmp["data"];
}
foreach(err; tmp[Constants.errors]) {
ret[Constants.errors] ~= err;
insertPayload(ret, tmp);
//ret["data"] = tmp["data"];
}
//foreach(err; tmp[Constants.errors]) {
// ret[Constants.errors] = err;
//}
} else if(!tmp.dataIsEmpty() && tmp.isScalar()) {
ret["data"] ~= tmp;
ret["data"] = tmp;
}
}

private void toRunArrayResolverFollow(SelectionSet ss, GQLDType elemType, Json item
private void toRunArrayResolverFollow(string fieldName, SelectionSet ss, GQLDType elemType, Json item
, ref Json ret, Json variables, Document doc, ref Con context
, ref ExecutionContext ec) @trusted
{
auto t = Json(["data": item]);
writefln("to send down %s", t.toPrettyString());
Json tmp = this.executeSelectionSet(ss, elemType, t, variables,
doc, context, ec
);
tmp = tmp.type == Json.Type.object && "data" in tmp
? tmp["data"]
: tmp;
writefln("join %s\n%s", tmp.toPrettyString(), ret.toPrettyString());
if(tmp.type == Json.Type.object) {
if("data" in tmp) {
ret["data"][fieldName] = tmp["data"];
//insertPayload(ret["data"], fieldName, tmp["data"]);
//ret["data"] = tmp["data"];
}
//foreach(err; tmp[Constants.errors]) {
// ret[Constants.errors] = err;
//}
} else if(!tmp.dataIsEmpty() && tmp.isScalar()) {
ret["data"][fieldName] = tmp;
}
//writefln("%s\n%s", elemType, item.toPrettyString());
/*
if(GQLDList l = elemType.toList()) {
enforce(item.type == Json.Type.array, "Expected Array got "
~ item.toPrettyString());
foreach(it; item) {
Json tmp = this.executeSelectionSet(ss, l.elementType, it, variables,
doc, context, ec
);
if("data" in tmp) {
ret["data"] ~= tmp["data"];
}
foreach(err; tmp[Constants.errors]) {
ret.insertError(err);
//ret[Constants.errors] ~= err;
}
insertPayload(ret, fieldName, tmp);
//if("data" in tmp) {
// ret["data"] ~= tmp["data"];
//}
//foreach(err; tmp[Constants.errors]) {
// ret.insertError(err);
// //ret[Constants.errors] ~= err;
//}
}
} else {
Json tmp = this.executeSelectionSet(ss, elemType, item, variables,
doc, context, ec
);
if("data" in tmp) {
ret["data"] ~= tmp["data"];
}
foreach(err; tmp[Constants.errors]) {
ret[Constants.errors] ~= err;
}
insertPayload(ret, fieldName, tmp);
//if("data" in tmp) {
// ret["data"] ~= tmp["data"];
//}
//foreach(err; tmp[Constants.errors]) {
// ret[Constants.errors] ~= err;
//}
}
*/
}

Json executeList(SelectionSet ss, GQLDList objectType,
Expand Down Expand Up @@ -521,6 +548,12 @@ class GraphQLD(T, QContext = DefaultContext) {
: null;
GQLDMap elemTypeMap = toMap(unPacked);

Json[] items = (objectValue["data"].type == Json.Type.array
? objectValue["data"]
: Json.emptyArray()).get!(Json[])();
Json[] results = iota(items.length)
.map!(it => returnTemplate())
.array;
string[] fieldsHandledByArrayResolver;
if(arrayTypeResolverArray !is null) {
FieldRange fr = fieldRange(ss, doc
Expand Down Expand Up @@ -568,42 +601,38 @@ class GraphQLD(T, QContext = DefaultContext) {
writefln("%s\n%s", rsltType, rsltTypeUn);

size_t idx;
foreach(Json item;
"data" in rslt
&& rslt["data"].type == Json.Type.array
foreach(ref Json item;
"data" in rslt && rslt["data"].type == Json.Type.array
? rslt["data"]
: Json.emptyArray()
)
: Json.emptyArray())
{
ec.path ~= PathElement(idx);
++idx;
scope(exit) {
ec.path.popBack();
++idx;
}
this.toRunArrayResolverFollow(field.f.ss, rsltTypeUn
, item, rslt, variables, doc, context, ec);
writefln("iter %s %s", idx, item.toPrettyString());
this.toRunArrayResolverFollow(fieldName, field.f.ss, rsltTypeUn
, item, results[idx], variables, doc, context, ec);
}
joinInArray(ret, rslt, fieldName);
//joinInArray(ret, rslt, fieldName);
//return ret;
//writeln(ret.toPrettyString());
}
}
}
writefln("already handled %s", fieldsHandledByArrayResolver);
writeln(Json(results).toPrettyString());

if(this.options.asyncList == AsyncList.yes) {
Task[] tasks;
foreach(Json item;
objectValue["data"].type == Json.Type.array
? objectValue["data"]
: Json.emptyArray()
)
{

foreach(idx, ref Json item; items) {
tasks ~= runTask({
() nothrow {
try {
auto newEC = ec.dup;
this.toRun(ss, elemType, item, variables, ret, doc,
this.toRun(ss, elemType, item, variables, results[idx], doc,
context, newEC, fieldsHandledByArrayResolver
);
} catch(Exception e) {
Expand All @@ -620,22 +649,23 @@ class GraphQLD(T, QContext = DefaultContext) {
task.join();
}
} else {
size_t idx;
foreach(Json item;
objectValue["data"].type == Json.Type.array
? objectValue["data"]
: Json.emptyArray()
)
{
foreach(idx, ref Json item; items) {
ec.path ~= PathElement(idx);
++idx;
scope(exit) {
ec.path.popBack();
++idx;
}
this.toRun(ss, elemType, item, variables, ret, doc, context, ec
this.toRun(ss, elemType, item, variables, results[idx], doc, context, ec
, fieldsHandledByArrayResolver);
}
}
foreach(idx, ref it; results) {
writefln("%s %s", idx, it.toPrettyString());
ret["data"] ~= it["data"];
if(it["errors"].length > 0) {
ret["errors"] ~= it["errors"];
}
}
return ret;
}
}
Expand Down
29 changes: 28 additions & 1 deletion source/graphql/helper.d
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void insertPayload(ref Json result, string field, Json data) {
if(d !in result) {
result[d] = Json.emptyObject();
}
enforce(result[d].type == Json.Type.object);
enforce(result[d].type == Json.Type.object, "Expected Json.Type.object"
~ " got " ~ result[d].toPrettyString());
Json* df = field in result[d];
if(df) {
result[d][field] = joinJson(*df, data[d]);
Expand All @@ -102,6 +103,32 @@ void insertPayload(ref Json result, string field, Json data) {
}
}

void insertPayload(ref Json result, Json data) @trusted {
if(d in data) {
if(d !in result) {
result[d] = Json.emptyObject();
}
enforce(result[d].type == Json.Type.object);
foreach(string key, ref Json value; data) {
Json* df = key in result[d];
if(df) {
result[d][key] = joinJson(*df, value);
} else {
result[d][key] = value;
}
}
}
if(e in data) {
if(e !in result) {
result[e] = Json.emptyArray();
}
enforce(result[e].type == Json.Type.array);
if(!canFind(result[e].byValue(), data[e])) {
result[e] ~= data[e];
}
}
}

unittest {
Json old = returnTemplate();
old["data"]["foo"] = Json.emptyObject();
Expand Down
1 change: 0 additions & 1 deletion test/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ void main() {
.map!(ht => () @trusted {
Json t = characterToJson(ht);
Json g = Json.emptyObject();
g["data"] = Json.emptyObject();
foreach(v; t["data"].byKeyValue) {
if(canFind(keysToKeep, v.key)) {
g[v.key] = v.value;
Expand Down

0 comments on commit fadd394

Please sign in to comment.