Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for using this with the Serverless framework? #24

Closed
rvaidya opened this issue Nov 30, 2016 · 3 comments
Closed

Examples for using this with the Serverless framework? #24

rvaidya opened this issue Nov 30, 2016 · 3 comments

Comments

@rvaidya
Copy link

rvaidya commented Nov 30, 2016

The example given looks like it's tied to Express.

@nodkz
Copy link
Collaborator

nodkz commented Dec 1, 2016

Serverless framework: https://github.com/serverless/serverless

It tied to Express only for batching queries. It contains simple express middleware that accepts array of queries in one http request [{query1}, {query2}]. Graphql-express only accept {}. More details can be found here why graphql-express does not support arrays.

So if your serverless server side will accept [{}, {}] this plugin will 100% compatible with your server. Otherwise, it also works, but will not support batched queries (when at page startup relay makes bunch of queries).

@rvaidya
Copy link
Author

rvaidya commented Dec 7, 2016

Thanks, with that information I was able to write a handler for batched queries.

@nodkz
Copy link
Collaborator

nodkz commented Dec 7, 2016

Ok. So I should you provide additional information.

Client side (react-relay-network-layer)

This is a place in code where is constructed batch request https://github.com/nodkz/react-relay-network-layer/blob/master/src/relay/queriesBatch.js#L21-L27

And where is read response (in the same file) https://github.com/nodkz/react-relay-network-layer/blob/master/src/relay/queriesBatch.js#L31-L39

Standard single request

{
  query:
  variables:
}

Batched request

[{
  id: // <--- added relay request ID
  query:
  variables:
},
{
  id: // <--- another request ID provided by Relay
  query:
  variables:
},
]

Server side (your handler)

Standard single response for details can see spec

{
  data: {}, // by spec should be undefined if exists `errors`
  errors: {}, // may be undefined if no error
  extensions: {} | undefined,  // some [additional info](https://github.com/graphql/express-graphql#providing-extensions), eg. performance
}

Batched response
In the express middleware it constructed in such way https://github.com/nodkz/react-relay-network-layer/blob/master/src/express-middleware/graphqlBatchHTTPWrapper.js#L36-L45
You may see payload variable name. I choose this name for standard graphql response.

[
 {
    id: // pass back id obtained from batched request item (Relay request id)
    payload: { // standard graphql response
      data: {}, 
      errors: {},
      extensions: {},
    },
 },
]

I pass Relay request Id for resolving proper response item with Relay. You may on server run all queries in parallel, and the first ready query immediately send to the client, then attach second and so on, at the end close/end connection/response. So batched response may have a different order for returned items.

@nodkz nodkz closed this as completed Dec 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants