22
33* A Julia GraphQL client for seamless integration with a server*
44
5+ [ ![ Stable] ( https://img.shields.io/badge/docs-stable-blue.svg )] ( https://deloittedigitalapac.github.io/GraphQLClient.jl/stable )
6+ [ ![ Stable] ( https://img.shields.io/badge/docs-dev-blue.svg )] ( https://deloittedigitalapac.github.io/GraphQLClient.jl/dev )
7+ [ ![ Build Status] ( https://github.com/DeloitteDigitalAPAC/GraphQLClient.jl/workflows/CI/badge.svg?branch=main )] ( https://github.com/DeloitteDigitalAPAC/GraphQLClient.jl/actions?query=workflow%3ACI+branch%3Amain )
8+ [ ![ Codecov] ( https://codecov.io/gh/DeloitteDigitalAPAC/GraphQLClient.jl/branch/main/graph/badge.svg )] ( https://codecov.io/gh/DeloitteDigitalAPAC/GraphQLClient.jl )
9+ [ ![ ColPrac: Contributor's Guide on Collaborative Practices for Community Packages] ( https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet )] ( https://github.com/SciML/ColPrac )
10+
511This package is intended to make connecting to and communicating with GraphQL servers easy whilst integrating easily with the wider Julia ecosystem.
612
713## Key Features
814
9- - Querying, mutating and subscribing without manual writing of query strings
10- - Deserializing responses directly using StructTypes
15+ - ** Querying** , ** mutating** and ** subscribing** without manual writing of query strings
16+ - Deserializing responses directly using ** StructTypes**
1117- Type stable querying
12- - Construction of Julia types from GraphQL objects
13- - Using introspected schema for various purposes, such as getting all possible output fields from a query
18+ - ** Construction of Julia types** from GraphQL objects
19+ - Using ** introspection ** to help with querying
1420
1521## Installation
1622
@@ -30,92 +36,94 @@ julia> using Pkg; Pkg.add("GraphQLClient")
3036
3137A client can be instantiated by using the ` Client ` type
3238
33- ``` julia-repl
34- julia> using GraphQLClient
39+ ``` julia
40+ using GraphQLClient
3541
36- julia> client = Client("https://countries.trevorblades.com")
37- GraphQLClient Client
38- endpoint: https://countries.trevorblades.com
39- ws_endpoint: wss://countries.trevorblades.com
42+ client = Client(" https://countries.trevorblades.com" )
4043```
4144
42- This will, by default, use a query to introspect the server schema, populating
43- several fields of the ` Client ` object which can then be used to help with
44- querying.
45+ This will, by default, use a query to introspect the server schema.
4546
4647### Querying
4748
48- We can query it without having to type a full GraphQL query by hand
49+ We can query a ` client ` without having to type a full GraphQL query by hand, with the response containing fields obtained by introspection
4950
50- ``` julia-repl
51- julia> response = query(client, "countries")
52- GraphQLClient.GQLResponse{Any}
53- data: Dict{String, Any}
54- countries: Vector{Any}
51+ ``` julia
52+ response = query(client, " countries" )
5553```
5654
57- We can add arguments and request fields in the response
55+ We can add arguments and speciy fields in the response
5856
59- ``` julia-repl
60- julia> query_args = Dict("filter" => Dict("code" => Dict("eq" => "AU"))); # Filter for countries with code equal to AU
57+ ``` julia
58+ query_args = Dict(" filter" => Dict(" code" => Dict(" eq" => " AU" )))
59+ response = query(client, " countries" ; query_args= query_args, output_fields= " name" );
60+ response. data[" countries" ]
61+ # 1-element Vector{Any}:
62+ # Dict{String, Any}("name" => "Australia")
63+ ```
6164
62- julia> response = query(client, "countries"; query_args=query_args, output_fields="name");
65+ Or we can query with the query string directly
6366
64- julia> response.data["countries"]
65- 1-element Vector{Any}:
66- Dict{String, Any}("name" => "Australia")
67+ ``` julia
68+ query_string = """
69+ query(
70+ \$ eq: String
71+ ){
72+ countries(
73+ filter:{
74+ code:{
75+ eq:\$ eq
76+ }
77+ }
78+ ){
79+ name
80+ }
81+ }
82+ """
83+
84+ variables = Dict(" eq" => " AU" )
85+
86+ response = GraphQLClient. execute(client, query_string, variables= variables)
6787```
6888
69- We can define a ` StructType ` to deserialise the result into
7089
71- ``` julia-repl
72- julia> using StructTypes
90+ We can define a ` StructType ` to deserialise the result into
7391
74- julia> struct CountryName
75- name::String
76- end
92+ ``` julia
93+ using StructTypes
7794
78- julia> StructTypes.StructType(::Type{CountryName}) = StructTypes.OrderedStruct()
95+ struct CountryName
96+ name:: String
97+ end
98+ StructTypes. StructType(:: Type{CountryName} ) = StructTypes. OrderedStruct()
7999
80- julia> response = query(client, query_alias, Vector{CountryName}, query_args=query_args, output_fields="name")
81- GraphQLClient.GQLResponse{Vector{CountryName}}
82- data: Dict{String, Union{Nothing, Vector{CountryName}}}
83- country_names: Vector{CountryName}
100+ response = query(client, " countries" , Vector{CountryName}, query_args= query_args, output_fields= " name" )
84101
85- julia> response.data["country_names "][1]
86- CountryName("Australia")
102+ response. data[" countries " ][1 ]
103+ # CountryName("Australia")
87104```
88105
89106Or we can use introspection to build the type automatically
90107
91- ``` julia-repl
92- julia> Country = GraphQLClient.introspect_object(client, "Country")
93- ┌ Warning: Cannot introspect field country on type State due to recursion of object Country
94- └ @ GraphQLClient ../GraphQLClient/src/type_construction.jl:75
95- ┌ Warning: Cannot introspect field countries on type Continent due to recursion of object Country
96- └ @ GraphQLClient ../GraphQLClient/src/type_construction.jl:75
97- GraphQLClient.var"##Country#604"
98-
99- julia> response = query(client, query_alias, Vector{Country}, query_args=query_args, output_fields="name")
100- GQLResponse{Vector{GraphQLClient.var"##Country#604"}}
101- data: Dict{String, Union{Nothing, Vector{GraphQLClient.var"##Country#604"}}}
102- country_names: Vector{GraphQLClient.var"##Country#604"}
103-
104- julia> response.data["country_names"][1]
105- Country
106- name : Australia
108+ ``` julia
109+ Country = GraphQLClient. introspect_object(client, " Country" )
110+
111+ response = query(client, " countries" , Vector{Country}, query_args= query_args, output_fields= " name" )
112+
113+ response. data[" countries" ][1 ]
114+ # Country
115+ # name : Australia
107116```
108117
109118### Mutations
110119
111120Mutations can be constructed in a similar way, except the arguments are not a keyword argument as typically
112121a mutation is doing something with an input. For example
113122
114- ``` julia-repl
115- julia> response = mutate(client, "mutation_name", Dict("new_id" => 1))
123+ ``` julia
124+ response = mutate(client, " mutation_name" , Dict(" new_id" => 1 ))
116125```
117126
118- Unlike with ` query ` , the output fields are not introspected as mutations often do not have a response.
119127
120128### Subscriptions
121129
0 commit comments