Skip to content

Commit f01ed1f

Browse files
committed
Add Usage section to README.
1 parent f8a72a0 commit f01ed1f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,53 @@ Add `argo_graphql` to your project's dependencies in your `Makefile` for [`erlan
2828
]}.
2929
```
3030

31+
## Usage
32+
33+
Using the ["Introduction to GraphQL" example](https://graphql.org/learn/):
34+
35+
```erlang
36+
% Load the GraphQL Service Document.
37+
SD = argo_graphql_service_document:from_string("
38+
type Query {
39+
me: User
40+
}
41+
42+
type User {
43+
id: ID
44+
name: String
45+
}
46+
"),
47+
% Load the GraphQL Executable Document.
48+
ED = argo_graphql_executable_document:from_string("
49+
query MyQuery {
50+
me {
51+
name
52+
}
53+
}
54+
"),
55+
% Derive the Argo Wire Type based on the GraphQL Service Document
56+
% and the GraphQL Executable Document.
57+
{{some, <<"MyQuery">>}, ArgoWireType} = argo_typer:derive_wire_type(SD, ED, none).
58+
59+
% Convert a JSON response to an Argo Value.
60+
JsonValue = #{<<"data">> => #{<<"me">> => #{<<"name">> => <<"Luke Skywalker">>}}},
61+
ArgoValue = argo_value:from_json(ArgoWireType, JsonValue),
62+
% Encode Argo Value using default settings.
63+
ArgoEncoded = argo_value:to_writer(ArgoValue),
64+
% Compare output size to the JSON encoding.
65+
JsonEncoded = jsone:encode(JsonValue),
66+
41 = byte_size(JsonEncoded),
67+
22 = byte_size(ArgoEncoded),
68+
% Argo encoding is roughly 46% smaller than JSON encoding in this case.
69+
46 = trunc((1 - (byte_size(ArgoEncoded) / byte_size(JsonEncoded))) * 100).
70+
71+
% For decoding, use the Argo Wire Type and the Argo encoding bytes.
72+
{<<>>, ArgoValue} = argo_value:from_reader(ArgoWireType, ArgoEncoded).
73+
74+
% Optionally convert back to JSON representation.
75+
JsonResponse = argo_value:to_json(ArgoValue).
76+
```
77+
3178
## License
3279

3380
`argo` is MIT licensed, as found in the [LICENSE](LICENSE.md) file.

0 commit comments

Comments
 (0)