GraphQLite.jl
Simple, fast, limited-scope implementation of GraphQL in Julia. Converts GraphQL input into a composition of arrays and Dicts.
Setup
using GraphQLite
API index
GraphQLite.dict2struct
GraphQLite.resolve
GraphQLite.resolveinput
GraphQLite.runmutation
GraphQLite.runquery
Components
GraphQLite.runquery
— Functionrunquery(gql::String, vars::Union{<:AbstractDict,Nothing}=nothing, extra::Union{<:AbstractDict,Nothing}=nothing; kwargs...)
Example:
get_cart = "
query GetCarts(\$id: Int!){
myShoppingCart: getCart(id: $id){
items { id name alternateNames brandId brand{name} }
}
}
"
response = runquery(get_cart, json2dict("""{"id":1}"""))
@assert response isa Dict
@assert response[:myShoppingCart] isa Dict
@assert response[:myShoppingCart][:items] isa Vector{<:AbstractDict}
GraphQLite.runmutation
— Functionrunmutation(gql::String, vars::Union{<:AbstractDict,Nothing}=nothing, extra::Union{<:AbstractDict,Nothing}=nothing; kwargs...)
Example:
add_item = "
mutation AddShoppingCartItem($input: CartItemInput!){
addItemToCart(input: $input){
items { $item_fragment }
}
}
"
response = runmutation(
add_item,
json2dict("""{"input":{"cartId":1, "itemId":5}}"""),
)
@assert response isa Dict
@assert response[:addItemToCart] isa Dict
@assert response[:addItemToCart][:items] isa Vector{<:AbstractDict}
GraphQLite.resolve
— Functionresolve(parent::T, field::Val{F}, args)
Example:
function GraphQLite.resolve(parent::GQLQuery, field::Val{:getCart}, args)
# return a object of type Cart
end
GraphQLite.resolveinput
— Functionresolveinput(field::Val{F}, d::T)
Example:
@schema """
...
type Mutation {
addItemToCart(input: CartItemInput): Cart
}
...
input CartItemInput {
cartId: Int!
itemId: Int!
quantity: Int!
}
...
"""
@kwdef struct CartItemInput
quantity::Int = 1
cart_id::Int
item_id::Int
end
GraphQLite.resolveinput(::Val{:CartItemInput}, d::Dict) = CartItemInput(d)
GraphQLite.dict2struct
— Functiondict2struct(::Type{T}, d::Dict)
Convenience method for converting a Dict to a custom type.
Example:
@kwdef struct CartItemInput
quantity::Int = 1
cart_id::Int
item_id::Int
end
@assert dict2struct(CartItemInput, Dict(:cart_id=>1, :item_id=>2)) == CartItemInput(1, 1, 2)