Skip to content

Commit

Permalink
docs: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 29, 2024
1 parent adf2906 commit fec58cb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
inputs: ["{mix,.formatter}.exs", "{config,examples,lib,test}/**/*.{ex,exs}"],
line_length: 80
]
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Examples for GraphQL Websocket Client

To make the examples work, boot up the test server provided in the
`docker-compose.yml`. By default, the server is running on port 8080.

Then, from the project root, use `mix run` to run any of the example scripts.

```sh
mix run examples/query.exs
```
14 changes: 14 additions & 0 deletions examples/query.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{:ok, socket} =
GraphQLWSClient.start_link(url: "ws://localhost:8080/subscriptions")

socket
|> GraphQLWSClient.query!("""
query GetPosts {
posts {
id
body
author
}
}
""")
|> IO.inspect()
40 changes: 40 additions & 0 deletions examples/stream.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{:ok, socket} =
GraphQLWSClient.start_link(url: "ws://localhost:8080/subscriptions")

Task.start_link(fn ->
socket
|> GraphQLWSClient.stream!("""
subscription PostCreated {
postCreated {
id
author
body
}
}
""")
|> Stream.each(&IO.inspect/1)
|> Stream.run()
end)

defmodule QueryLoop do
def query(socket) do
Process.sleep(2000)

GraphQLWSClient.query(
socket,
"""
mutation CreatePost($author: String!, $body: String!) {
createPost(author: $author, body: $body) {
id
}
}
""",
%{"author" => "Tobi", "body" => "Lorem Ipsum"}
)

query(socket)
end
end

QueryLoop.query(socket)
Process.sleep(15000)

0 comments on commit fec58cb

Please sign in to comment.