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.
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.
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)
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.