You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+192-47
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# GraphQL For Serverless ·[](https://www.npmjs.com/package/graphql-serverless)[](https://travis-ci.org/nicolasdao/graphql-serverless)[](https://opensource.org/licenses/BSD-3-Clause)[](#this-is-what-we-re-up-to)
2
2
3
-
__*graphql-serverless*__ is a middleware for [_**webfunc**_](https://github.com/nicolasdao/webfunc), that allows to deploy [GraphQL](http://graphql.org/learn/) apis (including an optional [GraphiQL interface](https://github.com/graphql/graphiql)) to the most popular serverless platforms:
3
+
__*graphql-serverless*__ is a middleware for [_**webfunc**_](https://github.com/nicolasdao/webfunc), that allows to deploy [GraphQL](http://graphql.org/learn/) apis (including an optional [GraphiQL interface](https://github.com/graphql/graphiql)) to the most popular serverless platforms. _**GraphQl Subscriptions**_ over websocket are also supported out-of-the-box (also supported in GraphiQL). Without changing a single line of code, seamlessly deploy to:
4
4
-[Zeit Now](https://zeit.co/now) (using express under the hood)
Copy/paste the following in your terminal if you want to run your first GraphQL api ([http://localhost:4000](http://localhost:4000)) including a GraphiQL interface ([http://localhost:4000/graphiql](http://localhost:4000/graphiql)) on your local machine in less than 30 seconds:
@@ -36,81 +36,226 @@ _If you're already logged in, then simply run this:_
Using the template above (i.e. [graphql-universal-server](https://github.com/nicolasdao/graphql-universal-server.git)) is the easiest way to start a new GraphQL project from scratch. However, if you really want to start on a blank page, simply create an index.js as follow:
54
+
## Basics
55
+
Using the template above (i.e. [graphql-universal-server](https://github.com/nicolasdao/graphql-universal-server.git)) is the easiest way to start a new GraphQL project from scratch.
56
+
57
+
However, if you really want to start on a blank page:
58
+
59
+
1. Create a new npm project: `npm init`
60
+
2. Install the following: `npm install graphql-serverless webfunc --save`
-[http://localhost:4000](http://localhost:4000): This is the GraphQL endpoint that your client can start querying.
131
+
-[http://localhost:4000/graphiql](http://localhost:4000/graphiql): This is the GraphiQL Web UI that you can use to test and query your GraphQL server.
132
+
133
+
>If you need best practices on how to structure your GraphQL project, clone the [graphql-universal-server](https://github.com/nicolasdao/graphql-universal-server.git) project and see by yourself.
134
+
135
+
## GraphQl Subscriptions
136
+
137
+
> __WARNING: This feature is only available on [Zeit Now serverless](https://zeit.co/now) or on localhost.__
138
+
> Even though _graphql-serverless_ relies on [_webfunc_](https://github.com/nicolasdao/webfunc) to deploy on FaaS solutions like AWS Lambdas or Google Functions, because those hosting platforms do not natively support websocket, GraphQl Subscriptions can't be deployed there.
139
+
140
+
_graphql-serverless_ exposes a helper method `setupSubscriptions` that can host a websocket endpoint for GraphQl Subscriptions. In the following example, we will slightly modify the code above to:
141
+
- (MODIFICATION A) Configure a new websocket endpoint for all subscriptions.
142
+
- (MODIFICATION B) Add a PubSub queue so that publisher can publish messages onto topics and subscribers can listen to certain topics so that clients using websocket can receive updates.
143
+
- (MODIFICATION C) Add a new GraphQl Mutation to insert a new product. This insert will act as a publisher. It will add a message to the PubSub topic once the product has been successfully inserted.
144
+
- (MODIFICATION D) Add a new GraphQl Subscription that listen to a specific topic on the PubSub queue and uses websocket to inform the client that a new product has been inserted.
Execute `node index.js` and then browse to [http://localhost:4000/graphiql](http://localhost:4000/graphiql). Start a subscription as follow:
228
+
```js
229
+
subscription {
230
+
productInserted {
231
+
id
232
+
name
233
+
}
234
+
}
102
235
```
103
-
node index.js
236
+
237
+
At that point, the client is simply listening to any new messages on the 'productInserted' topic. Time to publish a new messages on that topic. Open a new tab and browse again to [http://localhost:4000/graphiql](http://localhost:4000/graphiql). There insert a new product as follow:
238
+
```js
239
+
mutation {
240
+
productInsert(product: {
241
+
name:"Product C"
242
+
}) {
243
+
id
244
+
name
245
+
}
246
+
}
104
247
```
105
248
106
-
This will serve 2 endpoints:
249
+
Once the product has been inserted, you should be able to observe that your subscription client has noticed it.
107
250
108
-
-[http://localhost:4000](http://localhost:4000): This is the GraphQL endpoint that your client can start querying.
109
-
-[http://localhost:4000/graphiql](http://localhost:4000/graphiql): This is the GraphiQL Web UI that you can use to test and query your GraphQL server.
251
+
## Customizing GraphiQL
110
252
111
-
>If you need best practices on how to structure your GraphQL project, clone the [graphql-universal-server](https://github.com/nicolasdao/graphql-universal-server.git) project and see by yourself.
253
+
## Managing GraphQl Errors
112
254
113
-
## Contributing
255
+
## Creating Custom Middleware
256
+
257
+
258
+
# Contributing
114
259
```
115
260
npm test
116
261
```
@@ -127,6 +272,7 @@ Our other open-sourced projects:
127
272
*[__*graphql-serverless*__](https://github.com/nicolasdao/graphql-serverless): GraphQL (incl. a GraphiQL interface) middleware for [webfunc](https://github.com/nicolasdao/webfunc).
128
273
*[__*schemaglue*__](https://github.com/nicolasdao/schemaglue): Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
129
274
*[__*graphql-s2s*__](https://github.com/nicolasdao/graphql-s2s): Add GraphQL Schema support for type inheritance, generic typing, metadata decoration. Transpile the enriched GraphQL string schema into the standard string schema understood by graphql.js and the Apollo server client.
275
+
*[__*graphql-authorize*__](https://github.com/nicolasdao/graphql-authorize.git): Authorization middleware for [graphql-serverless](https://github.com/nicolasdao/graphql-serverless). Add inline authorization straight into your GraphQl schema to restrict access to certain fields based on your user's rights.
130
276
131
277
#### React & React Native
132
278
*[__*react-native-game-engine*__](https://github.com/bberak/react-native-game-engine): A lightweight game engine for react native.
@@ -135,7 +281,6 @@ Our other open-sourced projects:
135
281
#### Tools
136
282
*[__*aws-cloudwatch-logger*__](https://github.com/nicolasdao/aws-cloudwatch-logger): Promise based logger for AWS CloudWatch LogStream.
0 commit comments