Can´t get json-api to work when passing arguments.

My api is working with no arguments but I can´t understand how to do it with passing arguments.
defmodule AshDatastore.Documents.Syllabus do

use Ash.Resource,
data_layer: Ash.DataLayer.Ets, extensions: [AshJsonApi.Resource]
json_api do
type "syllabus"
routes do
base "/syllabuses"
get(:get_syllabus_documents, route: "/by_type/:country_code/:school_type/:document_type")
index :read
end
end
actions do
defaults [:read, :create]
code_interface do
define_for AshDatastore.Documents
define :get_syllabus_documents, args: [:country_code, :school_type, :document_type]
end
read :get_syllabus_documents do
argument :country_code, :string
argument :school_type, :string
argument :document_type, :string
filter expr(country_code== ^arg(:country_code) and school_type == ^arg(:school_type) and document_type==^arg(:document_type))
end
end

attributes do
uuid_primary_key :id
attribute :country_code, :string
attribute :school_type, :string
attribute :document_type, :string
attribute :content_map, :map
end
end
defmodule AshDatastore.Documents.Syllabus do

use Ash.Resource,
data_layer: Ash.DataLayer.Ets, extensions: [AshJsonApi.Resource]
json_api do
type "syllabus"
routes do
base "/syllabuses"
get(:get_syllabus_documents, route: "/by_type/:country_code/:school_type/:document_type")
index :read
end
end
actions do
defaults [:read, :create]
code_interface do
define_for AshDatastore.Documents
define :get_syllabus_documents, args: [:country_code, :school_type, :document_type]
end
read :get_syllabus_documents do
argument :country_code, :string
argument :school_type, :string
argument :document_type, :string
filter expr(country_code== ^arg(:country_code) and school_type == ^arg(:school_type) and document_type==^arg(:document_type))
end
end

attributes do
uuid_primary_key :id
attribute :country_code, :string
attribute :school_type, :string
attribute :document_type, :string
attribute :content_map, :map
end
end
I run this (with singel or dubbelbrackets- same result): http://localhost:4000/api/json/documents/syllabuses/by_type/"se"/"gy"/"courses" and get this: ``` { "errors": [ { "code": "NotFound", "detail": "No syllabus record found with country_code: "se", document_type: "courses", school_type: "gy"`", "id": "1db6e507-92e1-4683-a133-517993305779", "status": "404", "title": "Entity Not Found" } ], "jsonapi": { "version": "1.0" } } ```
14 Replies
ZachDaniel
ZachDaniel3y ago
Pretty sure you just need to remove the quotation marks
http://localhost:4000/api/json/documents/syllabuses/by_type/se/gy/courses
http://localhost:4000/api/json/documents/syllabuses/by_type/se/gy/courses
maxmannen
maxmannenOP3y ago
Same error
ZachDaniel
ZachDaniel3y ago
Can I see the error? It should be at least slightly different
maxmannen
maxmannenOP3y ago
json {
"errors": [
{
"code": "NotFound",
"detail": "No syllabus record found with `country_code: se, document_type: courses, school_type: gy`",
"id": "1e851c2a-0b3b-4213-a50b-3eb71d8a2689",
"status": "404",
"title": "Entity Not Found"
}
],
"jsonapi": {
"version": "1.0"
}
}
json {
"errors": [
{
"code": "NotFound",
"detail": "No syllabus record found with `country_code: se, document_type: courses, school_type: gy`",
"id": "1e851c2a-0b3b-4213-a50b-3eb71d8a2689",
"status": "404",
"title": "Entity Not Found"
}
],
"jsonapi": {
"version": "1.0"
}
}
`
ZachDaniel
ZachDaniel3y ago
What happens if you try to get it yourself?
YourApi.get(YourResource, country_code: "se", document_type: "courses", school_type: "gy")
YourApi.get(YourResource, country_code: "se", document_type: "courses", school_type: "gy")
maxmannen
maxmannenOP3y ago
yes we have an error here
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.InvalidPrimaryKey{
resource: AshDatastore.Documents.Syllabus,
value: [
country_code: "se",
document_type: "courses",
school_type: "gy"
],
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
],
stacktraces?: true,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}}
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.InvalidPrimaryKey{
resource: AshDatastore.Documents.Syllabus,
value: [
country_code: "se",
document_type: "courses",
school_type: "gy"
],
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
],
stacktraces?: true,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}}
ZachDaniel
ZachDaniel3y ago
Ah, you probably need something like this set up on your resource
identities do
identity :unique_country_document_type_school_type, [:country_code, :document_type, :school_type]
end
identities do
identity :unique_country_document_type_school_type, [:country_code, :document_type, :school_type]
end
If the resource doesn't have an identity on a set of fields, then they can't be used to look one up identity is basically "unique index"
maxmannen
maxmannenOP3y ago
Ah. This wont be an issue if I do not use ETS?
ZachDaniel
ZachDaniel3y ago
You need the identity regardless of which data layer you are using
maxmannen
maxmannenOP3y ago
Error when I compile: Identities: unique_country_document_type_school_type Must specify the pre_check_with option. Can´t find where to put that in the documentation
ZachDaniel
ZachDaniel3y ago
identity :unique_country_document_type_school_type, [:country_code, :document_type, :school_type], pre_check_with: YourApiModule
identity :unique_country_document_type_school_type, [:country_code, :document_type, :school_type], pre_check_with: YourApiModule
maxmannen
maxmannenOP3y ago
Hmm. (Mix) Could not start application ampl_dev: exited in: AmplDev.Application.start(:normal, []) (EXIT) an exception was raised: ** (Ash.Error.Invalid) Input Invalid * country_code, document_type, school_type: has already been taken. I am making some newbie error here Ahh never mind
ZachDaniel
ZachDaniel3y ago
Hey @maxmannen if your issue has been resolved please add the resolved tag and close the post, thanks!
maxmannen
maxmannenOP3y ago
Thanks for your help!

Did you find this page helpful?