Repository navigation
Parse GraphQL queries with serde_json and graphql_parser - #30
Conversation
dab5c59 to
787abf0
Compare
We'll need these for parsing incoming GraphQL queries.
Also drop the request member for now. We should probably extract everything we need from the client into a dedicated data structure and pass that around as part of the query.
We will use this to report JSON parsing errors.
This server parses incoming JSON/GraphQL requests into queries that it then passes on to the query runner.
It makes sense to treat errors that occur when starting up a GraphQL server from those that occur when processing requests. To keep the error type flexible for different server implementations, we're using an associated type here.
This makes the GraphQLServer interface fully agnostic with regards to how queries and query results are represented, parsed or served.
fb00f9e to
234733f
Compare
This makes running and testing the server a lot easier.
This is required for being able to obtain runtime handles to pass to components so they can spawn tasks against the runtime.
These tests verify that making HTTP requests with valid/invalid queries yield the correct HTTP responses. We'll build this out further in the future.
This is required for the integration tests of thegraph-hyper. These tests each launch an HTTP server and would fail trying to bind the same address/port while running concurrently. The tests run super fast right now, so this shouldn't be a problem for a while.
234733f to
618f969
Compare
fordN
left a comment
There was a problem hiding this comment.
Looking really good! I asked a few questions that came to mind when reading through it, mostly for my own understanding.
|
|
||
| impl GraphQLRequest { | ||
| /// Creates a new GraphQLRequest future based on an HTTP request and a result sender. | ||
| pub fn new(body: Chunk) -> Self { |
There was a problem hiding this comment.
self here references an instance of the GraphQLRequest struct? didn't know you could use it like this.
|
|
||
| // Ensure the JSON data is an object | ||
| let obj = json.as_object().ok_or_else(|| { | ||
| GraphQLServerError::ClientError(String::from("Request data is not an object")) |
There was a problem hiding this comment.
does this need to be a String here? as opposed to a string slice
There was a problem hiding this comment.
I mainly did this to avoid dealing with lifetimes. Can take another look though.
|
|
||
| // Create a one-shot channel to allow another part of the system | ||
| // to notify the service when the query has completed | ||
| let (sender, receiver) = oneshot::channel(); |
There was a problem hiding this comment.
oneshots seem to be an elegant way to communicate future traits
| fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
| let response = match self.result { | ||
| Ok(ref result) => { | ||
| let data = format!("{:?}", result); |
There was a problem hiding this comment.
Absolutely not. GraphQL mandates that the response be formatted in JSON, see http://facebook.github.io/graphql/October2016/#sec-Response. I'm already working on that.
|
|
||
| impl fmt::Display for HyperGraphQLServeError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "OrphanError: No component set up to handle the queries") |
There was a problem hiding this comment.
unclear about how this error message here is directly related to fmt::Display
There was a problem hiding this comment.
i see now, this is just to format the error message when being displayed
| match (req.method(), req.uri().path()) { | ||
| // POST / receives GraphQL queries | ||
| (&Method::POST, "/") => self.handle_graphql_query(req), | ||
|
|
| pub fn new(logger: &slog::Logger, runtime: Handle, store: S) -> Self { | ||
| let (sink, stream) = channel(100); | ||
| let runner = MockQueryRunner { | ||
| logger: logger.new(o!("component" => "MockQueryRunner")), |
…assembly-script Update -cli/ts for the new AssemblyScript version
This PR depends on #29.
This resolves #23 by adding a more complete GraphQL server based on Hyper in a separate
thegraph-hypercrate. This server asynchronously reads the request body, tries to parse it as JSON (using serde_json, then parses it into a GraphQL query document using graphql_parser and eventually feeds it into the system using its outgoing query stream.A simple way to test this is to launch
and (in another terminal) submit a query to the server:
curl \ -X POST \ -H "Content-Type: application/json" \ --data '{ "query": "{ allUsers(first: 5) { name } allArticles(first: 10) { title }" }' \ http://localhost:8000/In the server logs you should now see the following, demonstrating that the query is parsed and passed on to the query runner (from which this log message is printed):
This PR comes with the first