Using AshGraphql to fetch a single item without providing arguments

My app uses attribute-based multitenancy. There is a resource called WebSite, and the tenant would normally have only one of these. I am trying to create a GraphQL query called getWebSite that returns the tenant's WebSite, but the query response has the error, "In argument "id": Expected type "ID!", found null." I don't want the query to take any arguments. Here is the test that reproduces there error:
# This returns an error
query = """
query GetWebSite {
getWebSite {
id
}
}
"""

resp_body =
conn
|> set_authorized_user(user)
|> set_org(org) # tenant
|> put_req_header("content-type", "application/json")
|> post("/api/gql", query: query)
|> json_response(200)

# resp_body:
# %{"errors" => [%{"locations" => [%{"column" => 3, "line" => 2}], "message" => "In argument \"id\": Expected type \"ID!\", found null."}]}
# This returns an error
query = """
query GetWebSite {
getWebSite {
id
}
}
"""

resp_body =
conn
|> set_authorized_user(user)
|> set_org(org) # tenant
|> put_req_header("content-type", "application/json")
|> post("/api/gql", query: query)
|> json_response(200)

# resp_body:
# %{"errors" => [%{"locations" => [%{"column" => 3, "line" => 2}], "message" => "In argument \"id\": Expected type \"ID!\", found null."}]}
Here are the relevant parts of the WebSite resource:
actions do
read :get_web_site do
prepare build(limit: 1)
end
end

graphql do
type :web_site

queries do
get :get_web_site, :get_web_site
end
end
actions do
read :get_web_site do
prepare build(limit: 1)
end
end

graphql do
type :web_site

queries do
get :get_web_site, :get_web_site
end
end
How do I solve this?
5 Replies
ZachDaniel
ZachDaniel3y ago
I believe the pattern for this is get :get_web_site, :get_web_site, identity: false to instruct it to not use any of the identities on the resource to look it up
moxley
moxleyOP3y ago
Thank you. I will try this... It worked!
ZachDaniel
ZachDaniel3y ago
🥳
moxley
moxleyOP3y ago
Is there documentation on this? I couldn't find any documentation on the AshGraph queries and mutations DSL. Never mind. I found it under the Ash.Resource documentation.
ZachDaniel
ZachDaniel3y ago
👍

Did you find this page helpful?