File: README.md

package info (click to toggle)
graphql-el 0.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 92 kB
  • sloc: lisp: 245; makefile: 11
file content (106 lines) | stat: -rw-r--r-- 3,918 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# GraphQL.el

GraphQL.el provides a set of generic functions for interacting with GraphQL web services.

See also the following resources:
- [GraphQL language service][graph-lsp] and [`lsp-mode`][el-lsp]
- [`graphql-mode`][graphql-mode]
- [This brief overview of GraphQL syntax][graphql]

[graph-lsp]: https://github.com/graphql/graphql-language-service
[el-lsp]: https://github.com/emacs-lsp/lsp-mode
[graphql-mode]: https://github.com/davazp/graphql-mode
[graphql]: http://graphql.org/learn

## Syntax Overview
Two macros are provided to express GraphQL *queries* and *mutations*:
- `graphql-query` encodes the graph provided under a root `(query ...)` node.
- `graphql-mutation` encodes the graph provided under a root `(mutation ...)` node.
Both macros allow special syntax for query/mutation parameters if this is desired; see the docstrings for details.  I will note that backtick notation usually feels more natural in Lisp code.

### Basic Queries
The body of these macros is the graph of your query/mutation expressed in a Lispy DSL.  Generally speaking, we represent fields as symbols and edges as nested lists with the edge name being the head of that list.  For example,
```emacs-lisp
(graphql-query
 (myField1 myField2 (myEdges (edges (node myField3)))))
```
will construct a query that retrieves `myField1`, `myField2`, and `myField3` for every node in `myEdges`.  The query is returned as a string without any unnecessary whitespace (i.e., formatting) added.

## Following Edges
Multiple edges can of course be followed.  Here's an example using GitHub's API:
```emacs-lisp
(graphql-query
 ((viewer login)
  (rateLimit limit cost remaining resetAt)))
```

## Passing Arguments
Usually, queries need explicit arguments.  We pass them in an alist set off by the `:arguments` keyword:
``` emacs-lisp
(graphql-query
 ((repository
   :arguments ((owner . "github")
               (name . ($ repo)))
   (issues :arguments ((first . 20)
                       (states . [OPEN CLOSED]))
           (edges
            (node number title url id))))))
```
As you can see, strings, numbers, vectors, symbols, and variables can all be given as arguments.  The above evaluates to the following (formatting added):
``` graphql
query {
  repository (owner: "github", name: $repo) {
    issues (first: 20, states: [OPEN, CLOSED]) {
      edges {
        node {
          number title url id
        }
      }
    }
  }
}
```
Objects as arguments work, too, though practical examples seem harder to come by:

``` emacs-lisp
(graphql-query
 ((object :arguments ((someVariable . ((someComplex . "object")
                                       (with . ($ complexNeeds))))))))
```
gives
``` graphql
query {
  object (
    someVariable: {
      someComplex: "object",
      with: $complexNeeds
    }
  )
}
```

## Working with Responses
- `graphql-simplify-response-edges`
  Simplify structures like

      (field
       (edges
        ((node node1values...))
        ((node node2values...))))

  into `(field (node1values) (node2values))`.

## Keyword Reference
- `:arguments`
  Pass arguments to fields as an alist of parameters (as symbols) to values.  See `graphql--encode-argument-value`.
- `:op-name`, `:op-params`
  Operation name/parameters.  Given to top-level *query* or *mutation* operations for later re-use.  You should rarely (if ever) need to supply these yourself; the `graphql-query` and `graphql-mutation` macros give you natural syntax to do this.

## Planned
- `:as` keyword for [aliases][graphql-alias] (`graphql-encode`).
- `...` qualifier for [fragments][graphql-fragment] and [inline fragments][graphql-ifragment] (`graphql--encode-object`)

[graphql-alias]: http://graphql.org/learn/queries/#aliases
[graphql-variable]: http://graphql.org/learn/queries/#variables
[graphql-fragment]: http://graphql.org/learn/queries/#fragments
[graphql-ifragment]: http://graphql.org/learn/queries/#inline-fragments