Skip to content

Commit

Permalink
errors where not actually working, crazy to think it worked as well a…
Browse files Browse the repository at this point in the history
…s it did
  • Loading branch information
Robert Schadek committed Jul 18, 2019
1 parent 4ce8932 commit 9781103
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 33 deletions.
1 change: 1 addition & 0 deletions source/graphql/constants.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum Constants {
interfacesNames = "interfacesNames",
isDeprecated = "isDeprecated",
kind = "kind",
errors = "errors",
locations = "locations",
mutation = "mutation",
mutationType = "mutationType",
Expand Down
34 changes: 19 additions & 15 deletions source/graphql/graphql.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import std.exception : enforce;
import vibe.core.core;
import vibe.data.json;

import graphql.builder;
import graphql.ast;
import graphql.argumentextractor;
import graphql.helper;
import graphql.ast;
import graphql.builder;
import graphql.constants;
import graphql.directives;
import graphql.tokenmodule;
import graphql.schema.types;
import graphql.helper;
import graphql.schema.resolver;
import graphql.schema.types;
import graphql.tokenmodule;

@safe:

Expand Down Expand Up @@ -72,8 +73,8 @@ class GraphQLD(T, QContext = DefaultContext) {
ret["data"] = Json.emptyObject();
ret["data"] = parent[name];
} else {
ret["error"] = Json.emptyArray();
ret["error"] = Json(format(
ret[Constants.errors] = Json.emptyArray();
ret[Constants.errors] = Json(format(
"no field name '%s' found on type '%s'",
name,
parent.getWithDefault!string("__typename")
Expand Down Expand Up @@ -220,8 +221,8 @@ class GraphQLD(T, QContext = DefaultContext) {
ret["data"][key] = value;
}
}
foreach(err; tmp["error"]) {
ret["error"] ~= err;
foreach(err; tmp[Constants.errors]) {
ret[Constants.errors] ~= err;
}
}
return ret;
Expand Down Expand Up @@ -321,14 +322,17 @@ class GraphQLD(T, QContext = DefaultContext) {
"data" in objectValue ? objectValue["data"] : objectValue,
arguments, context
);
if(de.dataIsEmpty()) {
return de;
}
auto retType = this.schema.getReturnType(objectType, field.name);
if(retType is null) {
this.executationTraceLog.logf("ERR %s %s", objectType.name,
field.name
);
Json ret = Json.emptyObject();
ret["error"] = Json.emptyArray();
ret["error"] ~= Json(format(
ret[Constants.errors] = Json.emptyArray();
ret[Constants.errors] ~= Json(format(
"No return type for member '%s' of type '%s' found",
field.name, objectType.name
));
Expand Down Expand Up @@ -361,7 +365,7 @@ class GraphQLD(T, QContext = DefaultContext) {
);
if(rslt.dataIsNull()) {
this.executationTraceLog.logf("%s", rslt);
rslt["error"] ~= Json("NonNull was null");
rslt[Constants.errors] ~= Json("NonNull was null");
}
} else if(GQLDNullable nullType = objectType.toNullable()) {
this.executationTraceLog.logf("nullable %s", nullType.name);
Expand All @@ -373,7 +377,7 @@ class GraphQLD(T, QContext = DefaultContext) {
);
if(rslt.dataIsEmpty()) {
rslt["data"] = null;
rslt.remove("error");
rslt.remove(Constants.errors);
}
} else if(GQLDList list = objectType.toList()) {
this.executationTraceLog.logf("list %s", list.name);
Expand Down Expand Up @@ -401,8 +405,8 @@ class GraphQLD(T, QContext = DefaultContext) {
if("data" in tmp) {
ret["data"] ~= tmp["data"];
}
foreach(err; tmp["error"]) {
ret["error"] ~= err;
foreach(err; tmp[Constants.errors]) {
ret[Constants.errors] ~= err;
}
} else if(!tmp.dataIsEmpty() && tmp.isScalar()) {
ret["data"] ~= tmp;
Expand Down
13 changes: 7 additions & 6 deletions source/graphql/helper.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import std.datetime : DateTime;
import vibe.data.json;

import graphql.ast;
import graphql.constants;

@safe:

enum d = "data";
enum e = "error";
enum e = Constants.errors;

string firstCharUpperCase(string input) {
import std.conv : to;
Expand All @@ -33,7 +34,7 @@ string firstCharUpperCase(string input) {
Json returnTemplate() {
Json ret = Json.emptyObject();
ret["data"] = Json.emptyObject();
ret["error"] = Json.emptyArray();
ret[Constants.errors] = Json.emptyArray();
return ret;
}

Expand Down Expand Up @@ -101,8 +102,8 @@ bool dataIsEmpty(ref const(Json) data) {
import std.experimental.logger;
if(data.type == Json.Type.object) {
foreach(key, value; data.byKeyValue()) {
if(key != "error" && !value.dataIsEmpty()) {
//if(key != "error") { // Issue #22 place to look at
if(key != Constants.errors && !value.dataIsEmpty()) {
//if(key != Constants.errors) { // Issue #22 place to look at
return false;
}
}
Expand All @@ -128,7 +129,7 @@ bool dataIsEmpty(ref const(Json) data) {
}

unittest {
string t = `{ "error": {} }`;
string t = `{ "errors" : {} }`;
Json j = parseJsonString(t);
assert(j.dataIsEmpty());
}
Expand Down Expand Up @@ -338,7 +339,7 @@ T getWithDefault(T)(Json data, string[] paths...) {
}

unittest {
Json d = parseJsonString(`{"error":[],"data":{"commanderId":8,
Json d = parseJsonString(`{"errors":[],"data":{"commanderId":8,
"__typename":"Starship","series":["DeepSpaceNine",
"TheOriginalSeries"],"id":43,"name":"Defiant","size":130,
"crewIds":[9,10,11,1,12,13,8],"designation":"NX-74205"}}`);
Expand Down
6 changes: 6 additions & 0 deletions source/graphql/schema/typeconversions.d
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,14 @@ Json typeFields(T)() {
iv[Constants.name] = paraNames[idx];
// needed for interfacesForType
iv[Constants.__typename] = Constants.__InputValue;
iv[Constants.description] = Json(null);
iv[Constants.typenameOrig] =
typeToParameterTypeName!(paraTypes[idx]);
static if(!is(paraDefs[idx] == void)) {
iv[Constants.defaultValue] = serializeToJson(paraDefs[idx])
.toString();
} else {
iv[Constants.defaultValue] = Json(null);
}
tmp[Constants.args] ~= iv;
}}
Expand Down Expand Up @@ -440,12 +443,15 @@ Json directivesToJson(Directives)() {
// two default directives of GraphQL skip and include
// both have one parameter named "if".
iv[Constants.name] = stripLeft(paraNames[idx], "_");
iv[Constants.description] = Json(null);
// needed for interfacesForType
iv[Constants.__typename] = Constants.__InputValue;
iv[Constants.typenameOrig] = typeToTypeName!(paraTypes[idx]);
static if(!is(paraDefs[idx] == void)) {
iv[Constants.defaultValue] = serializeToJson(paraDefs[idx])
.toString();
} else {
iv[Constants.defaultValue] = Json(null);
}
tmp[Constants.args] ~= iv;
}}
Expand Down
1 change: 1 addition & 0 deletions source/graphql/testschema.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Query {
)
SearchResult search(string name);
Nullable!Starship starship(long id);
Starship starshipDoesNotExist();
Starship[] starships(float overSize = 100.0);
Starship[] shipsselection(long[] ids);
Nullable!Character character(long id);
Expand Down
42 changes: 31 additions & 11 deletions test/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ void main() {
}
);

graphqld.setResolver("queryType", "starshipDoesNotExist",
delegate(string name, Json parent, Json args,
ref CustomContext con) @safe
{
Json ret = Json.emptyObject();
ret["errors"] = Json.emptyArray();
ret["errors"] ~= Json("That ship does not exists");
return ret;
}
);

graphqld.setResolver("queryType", "starship",
delegate(string name, Json parent, Json args,
ref CustomContext con) @safe
Expand Down Expand Up @@ -272,13 +283,20 @@ void main() {
res.bodyReader.readAllUTF8()
);
if(q.st == ShouldThrow.yes) {
enforce("error" in ret
&& ret["error"].length != 1,
enforce("errors" in ret
&& ret["errors"].length != 1,
format("%s", ret.toPrettyString())
);
Json p = parseJsonString(q.expectedResult);
writefln("%s\n%s", p.toPrettyString(),
ret.toPrettyString());
assert(p == ret, format(
"got: %s\nexpeteced: %s",
p.toPrettyString(),
ret.toPrettyString()));
} else {
enforce("error" in ret
&& ret["error"].length == 0,
enforce("errors" in ret
&& ret["errors"].length == 0,
format("%s", ret.toPrettyString())
);
enforce("data" in ret
Expand All @@ -298,15 +316,17 @@ void main() {
} catch(Exception e) {
hasThrown = true;
if(q.st == ShouldThrow.no) {
writefln("IM DIENING NOW %s %s\n%s", __LINE__, e, q);
assert(false, e.msg);
writefln("IM DIENING NOW %s %s %s\n%s", tqIdx, __LINE__, e,
q
);
assert(false, format("%s %s", tqIdx, e.msg));
}
}
if(q.st == ShouldThrow.yes && !hasThrown) {
writefln("I SHOULD HAVE THROWN NOW %s %s\n%s", __LINE__, e,
q
writefln("I SHOULD HAVE THROWN NOW %s %s %s\n%s", tqIdx,
__LINE__, e, q
);
assert(false);
assert(false, format("%s", tqIdx));
}
//});
//qt.join();
Expand Down Expand Up @@ -386,8 +406,8 @@ void hello(HTTPServerRequest req, HTTPServerResponse res) {
e = cast(Exception)e.next;
}
Json ret = Json.emptyObject;
ret["error"] = Json.emptyArray;
ret["error"] ~= Json(app.data);
ret["errors"] = Json.emptyArray;
ret["errors"] ~= Json(app.data);
res.writeJsonBody(ret);
return;
}
Expand Down
19 changes: 18 additions & 1 deletion test/source/testqueries.d
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,22 @@ mutation one {
}
}
}
`, ShouldThrow.no)
`, ShouldThrow.no),
TestQuery(`
{
starshipDoesNotExist {
id
commander {
name
}
}
}`, ShouldThrow.yes,
`{
"error" : [
"That ship does not exists"
],
"data" : {
}
}`
)
];

0 comments on commit 9781103

Please sign in to comment.