Skip to content

Instantly share code, notes, and snippets.

@schveiguy
Last active January 14, 2020 17:12
Show Gist options
  • Save schveiguy/47d01f36e8eb41512529c68843ebbf77 to your computer and use it in GitHub Desktop.
Save schveiguy/47d01f36e8eb41512529c68843ebbf77 to your computer and use it in GitHub Desktop.
Sample graphql program that fails
import std.stdio;
import vibe.d;
import graphql.parser;
import graphql.builder;
import graphql.lexer;
import graphql.ast;
import graphql.helper;
import graphql.schema;
import graphql.traits;
import graphql.argumentextractor;
import graphql.graphql;
import graphql.uda;
import std.experimental.logger;
@GQLDUda(TypeKind.OBJECT) struct SubType
{
int x;
string foo();
string foo2(int x);
}
@GQLDUda(TypeKind.OBJECT) struct Query
{
int x();
SubType subType();
}
class Schema
{
Query queryType;
}
GraphQLD!Schema graphqld;
void main(string[] args)
{
GQLDOptions opts;
graphqld = new GraphQLD!(Schema)(opts);
graphqld.executationTraceLog = new std.experimental.logger.filelogger.FileLogger(stdout);
graphqld.executationTraceLog.logLevel = std.experimental.logger.LogLevel.trace;
graphqld.setResolver("queryType", "x",
delegate(string name, Json parent, Json args,
ref DefaultContext con) @safe
{
Json ret = Json.emptyObject;
ret["data"] = 1;
return ret;
}
);
graphqld.setResolver("queryType", "subType",
delegate(string name, Json parent, Json args,
ref DefaultContext con) @safe
{
Json ret = Json.emptyObject;
SubType result;
result.x = 5;
ret["data"] = toGraphqlJson!Schema(result);
return ret;
}
);
graphqld.setResolver("SubType", "foo",
delegate(string name, Json parent, Json args,
ref DefaultContext con) @safe
{
writeln("here!");
Json ret = Json.emptyObject;
ret["data"] = "hello!";
writeln("returning ", ret);
return ret;
}
);
graphqld.setResolver("SubType", "foo2",
delegate(string name, Json parent, Json args,
ref DefaultContext con) @safe
{
Json ret = Json.emptyObject;
ret["data"] = "hello, error";
if("x" in args)
{
import std.format;
ret["data"] = format("hello, %s", args["x"].get!long);
}
return ret;
}
);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["0.0.0.0"];
listenHTTP(settings, &hello);
finalizeCommandLineOptions(&args);
lowerPrivileges();
runEventLoop();
}
void hello(HTTPServerRequest req, HTTPServerResponse res) {
if("Origin" in req.headers) {
res.headers.addField("Access-Control-Allow-Origin", "*");
}
res.headers.addField("Access-Control-Allow-Credentials", "true");
res.headers.addField("Access-Control-Allow-Methods",
"POST, GET, OPTIONS, DELETE"
);
res.headers.addField("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, " ~ "X-CSRF-TOKEN");
Json j = req.json;
//writefln("input %s req %s headers %s", j, req.toString(), req.headers);
string toParse;
if(j.type == Json.Type.object && "query" in j) {
toParse = j["query"].get!string();
} else if(j.type == Json.Type.object && "mutation" in j) {
toParse = j["mutation"].get!string();
} else {
toParse = req.headers["Referer"].urlDecode();
string toFind = "?query=";
auto idx = toParse.indexOf(toFind);
if(idx != -1) {
toParse = toParse[idx + toFind.length .. $];
}
//writeln(toParse);
}
Json vars = Json.emptyObject();
if(j.type == Json.Type.object && "variables" in j) {
vars = j["variables"];
}
//writeln(j.toPrettyString());
auto l = Lexer(toParse);
auto p = Parser(l);
try {
import graphql.validation.querybased;
import graphql.validation.schemabased;
Document d = p.parseDocument();
const(Document) cd = d;
QueryValidator fv = new QueryValidator(d);
fv.accept(cd);
noCylces(fv.fragmentChildren);
allFragmentsReached(fv);
SchemaValidator!Schema sv = new SchemaValidator!Schema(d,
graphqld.schema
);
sv.accept(cd);
DefaultContext con;
Json gqld = graphqld.execute(d, vars, con);
res.writeJsonBody(gqld);
return;
} catch(Throwable e) {
import std.array: appender;
auto app = appender!string();
while(e) {
writeln(e.toString());
app.put(e.toString());
e = cast(Exception)e.next;
}
Json ret = Json.emptyObject;
ret.insertError(app.data);
res.writeJsonBody(ret);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment