How to build GraphQL for manual parsing

I have apps that currently have their own HTTP servers and am trying to figure out how to get the GraphQL engine to accept a request and return a response.

I have (I beleive) managed to build the schema docs and have a skeleton ready, but I cant see what I should do to execute a query and get a response.

If my schema looked like

type Test {
  id: ID!
  name: String!
}

type Query {
  test: Test!
}

and my query looked like

query GetTest {
  test {
    id
    name
  }
}

What class would I use and which method would I call ? from the documentation, it looks like I would probably call the GraphQL.Executer but I couldn't see how, nor was I able to see how to propperly build the executer.

Any help would be appreciated.

You could use the executer, but a higher level class that I'd recommend is the TGraphQLHttpHandler.

All it takes to Create an instance of it and execute is the schema and interfaces to adapt the HTTP response and request.

So create your own implementation if IGraphQLHttpRequest and IGraphQLHttpResponse interfaces, and pass it to the HTTP handler together with the schema. That should be enough.

Thanks for the info Wagner,

I'm still having problems. My sample source can be found here Sample Source

When I call my SendReq, I get 'Type "Query" not found'.

I think the AppSchema string const needs to be attached to a TGraphQLSchema.

Any suggestions ?

P.S. I know theres likely memory leaks in my test. I'm not concerned about that at present.

The problem is here:

constructor TGraphQL.Create;
begin
  fSchema := TSchemaDocument.Create;
  fSchema.Bind('Query', TQuery);
end;

You declared your AppSchema constant with the type definition but you don't use it anywhere? You should parse it to create a new schema:

constructor TGraphQL.Create;
begin
  fSchema := TSchemaDocument.Parse(AppSchema);
  fSchema.Bind('Query', TQuery);
end;

That got it closer, but I'm now getting EHttpException with message 'Query not provided'.

I have moved the query into the unit and updated the pastebin. With the debugger, I can see that the IGraphQLHttpRequest.GetContent has the query text and is called before I get the error.

I checked the schema using the Bookshelf demo with some modified names and that worked ok.

Are the following assumptions correct ?

the url does not have to be real or match anything specific
the Content-Type I have tried with application/octet-stream, text/html and text/plain
the Method is POST
the GetContent is 8 bit ascii (ie an ansistring is ok to be moved into the result)

I got it working. I was missing the schema init call and had the wrong content-type.

The final code is in pastebin for anyone interested.

Thanks for your help Wagner.

For basic tests, correct. Note that you can also pass GraphQL queries in the URL query (common use is when you want to use GET instead of POST). In those cases obviously the URL query must be properly formatted according to the GraphQL over HTTP specification.

The content-type of the request must be application/graphql+json or application/json, also according to the GraphQL over HTTP specification.

Correct.

The content must be UTF-8 encoded string.

Excellent! Indeed, Init is needed unless you use TGraphQLSchema component which does that automatically for you.

One minor remark, the Init should be called after calling the Bind method. After Init is called, the schema should not be modified anymore.