Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ZaninAndrea/minimalGraphql
A minimal graphql node client
https://github.com/ZaninAndrea/minimalGraphql
apollo apollo-client graphql node nodejs
Last synced: about 1 month ago
JSON representation
A minimal graphql node client
- Host: GitHub
- URL: https://github.com/ZaninAndrea/minimalGraphql
- Owner: ZaninAndrea
- Created: 2017-12-23T15:38:07.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T20:54:30.000Z (6 months ago)
- Last Synced: 2024-07-08T15:03:30.540Z (5 months ago)
- Topics: apollo, apollo-client, graphql, node, nodejs
- Language: JavaScript
- Size: 13.7 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-apollo-graphql - Minimal Graphql - Minimal node implementation of Apollo Client (Uncategorized / Uncategorized)
README
# Minimal graphql [![npm](https://img.shields.io/npm/v/minimal-graphql.svg?style=plastic)](https://www.npmjs.com/package/minimal-graphql)
Minimal graphql is a minimal node graphql client, it is built using the awesome [apollo library](https://www.apollographql.com/), but wires it up so that you can use it in node instead of the browser.
## Usage
### Initializing
The only libraries you will need are `minimal-graphql` (guess what) and `graphql-tag` (parses you graphql documents).
To create a client```js
const minimalGraphql = require("minimal-graphql")
const client = minimalGraphql(httpOptions, subscriptionOptions)
```* `httpOptions`: and object with the following props:
* `uri` (required): the uri at which your server is found. E.g.
```js
{
uri: "http://localhost:3000/graphql",
}
```* `headers` (optional): and object containing the headers you want your request to have. E.g.
```js
{
uri: "http://localhost:3000/graphql",
headers: {
Authorization: "Bearer " + bearer
}
}
```* `subscriptionOptions`(optional): if not passed you will not be able to use subscriptions. It's an object with the following props:
* `uri` (required): the uri at which your subscription server can be found (the **websocket uri** not http one). E.g.
```js
{
uri: `ws://localhost:3000/subscriptions`
}
```
* `options`(optional): and object containing `reconnect` (optional, boolean, whether to try to reconnect) and `connectionParams` (optional, an object containing the websocket connection parameters). E.g.```js
{
uri: `ws://localhost:3000/subscriptions`,
options: {
reconnect: true,
connectionParams: {
Authorization: "Bearer " + bearer,
},
},
}
```### Queries
Once you created a client you can use it's prop `query` to run a query.
The `client.query(opts)` function returns a Promise and takes an option object as input, it has this props:* `query`: a query parsed with `graphql-tag`. E.g.
```js
const gql = require("graphql-tag")// create the client here
client
.query({
query: gql`
{
user {
}
}
`,
})
.then(res => console.log(res.data))
```* `variables` (optional): containing the variables used in the graphql query. E.g.
```js
const gql = require("graphql-tag")// create the client here
client
.query({
query: gql`
query userQuery($id: ID!) {
user(id: $id) {
}
}
`,
variables: {
id: "myId",
},
})
.then(res => console.log(res.data))
```### Mutations
To run a mutation use the `client.mutation(opts)` function. It works as the `client.query(opts)` function, but instead of passing a `query` parameter in options you pass a `mutation` parameter
E.g.```js
client
.mutate({
mutation: gql`mutation logIn($email:String, password:String){
authenticateUser(email:$email, password:$password){
token
}
}`,
variables: {
email: "[email protected]",
password: "password",
},
})
.then(res => console.log(res.data))
```### Subscriptions
Finally you can use subscriptions through `client.subscribe(opts)`. The options are the same as for `client.query(opts)`, but instead of returning a Promise it returns a ZenObservable.
The ZenObservable has a `observable.subscribe(handlers)` prop that allows you to handle the subscription results. The `handlers` object has to have the following props:* `next`: the function handling successful result
* `err` (optional): the function handling errorsE.g.
```js
const gql = require("graphql-tag")client
.subscribe({
query: gql`
subscription {
deviceCreated {
id
customName
}
}
`,
})
.subscribe({
next: console.log,
err: console.log,
})
```## Contributing
This package is hosted [here](https://github.com/ZaninAndrea/minimalGraphql), feel free to contribute or ask for any new feature ;)