Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benthepoet/mithril-graphql
A lightweight GraphQL client for the Mithril framework.
https://github.com/benthepoet/mithril-graphql
Last synced: about 2 months ago
JSON representation
A lightweight GraphQL client for the Mithril framework.
- Host: GitHub
- URL: https://github.com/benthepoet/mithril-graphql
- Owner: benthepoet
- License: mit
- Created: 2017-05-27T03:41:56.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-24T13:21:20.000Z (over 3 years ago)
- Last Synced: 2024-10-11T20:08:27.234Z (2 months ago)
- Language: CoffeeScript
- Size: 7.81 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-mithril - mithril-graphql - A lightweight GraphQL client for the Mithril framework. (Uncategorized / Uncategorized)
README
# mithril-graphql
A lightweight GraphQL client for the Mithril framework.## Usage
This module is best utilized with Babel (ES2015+) or CoffeeScript as they both support multiline strings.### ES5
```
var GraphClient = require('mithril-graphql');var options = {
url: '/graphql'
};var query = 'query User($id: Int!) { user(id: $id) { id name } }';
var client = new GraphClient(options, queries);
client
.get(query, { id: 1 })
.then(function (response) {
console.log(reponse.data);
});
```### Babel (ES2015+)
```
const GraphClient = require('mithril-graphql');const options = {
url: '/graphql'
};const query = `
query User($id: Int!) {
user(id: $id) {
id
name
}
}
';const client = new GraphClient(options, queries);
client
.get(query, { id: 1 })
.then(response => {
console.log(reponse.data);
});
```### CoffeeScript
```
GraphClient = require('mithril-graphql')options = {
url: '/graphql'
}query = """
query User($id: Int!) {
user(id: $id) {
id
name
}
}
"""client = new GraphClient(options, queries)
client
.get(query, { id: 1 })
.then(response ->
console.log(reponse.data)
)
```## Additional notes
For optimal use, it is recommended that GraphQL queries utilize variables opposed to string interpolation. Before the client sends
the request it will try to minify the query. Minification will strip out any carriage returns that are followed immediately by tabs or spaces.### Good
#### queries.js
```
const queries = {
User: `
query User($id: Int!) {
user(id: $id) {
id
name
}
}
`
};
```#### graph.js
```
const GraphClient = require('mithril-graphql');
const queries = require('./queries');const options = {
url: '/graphql'
};const client = new GraphClient(options);
client
.get(queries.User, { id: 1 })
.then((response) => {
console.log(response.data);
});
```### Bad
#### graph.js
```
const GraphClient = require('mithril-graphql');const options = {
url: '/graphql'
};const id = 1;
const query = `
query {
user(id: ${id}) {
id
name
}
}
';const client = new GraphClient(options, queries);
client
.get(query)
.then(response => {
console.log(reponse.data);
});
```## GraphClient
### constructor(options)
Creates a new GraphClient.### get(query, [variables])
Sends a GraphQL query using the HTTP GET method.### post(query, [variables])
Sends a GraphQL query using the HTTP POST method.