View Source JSONAPIPlug.QueryParser behaviour (jsonapi_plug v2.0.0)
Parses a JSON:API query parameter to a user defined format.
You can implement a custom format for JSON:API
query parameters that do not define
a standard format: filter, page and sort.
To customize the default parser implement a module adopting the JSONAPIPlug.QueryParser
behaviour for your sort format:
defmodule MyApp.API.QueryParsers.Sort do
...
@behaviour JSONAPIPlug.QueryParser
# Transforms the query parameter value to a user defined format
@impl JSONAPIPlug.QueryParser
def parse(jsonapi_plug, sort) do
...my sort query parameter parsing logic...
end
end
and configure it in your API configuration under the parsers
key.
config :my_app, MyApp.API,
parsers: [
filter: MyApp.API.QueryParser.Filter,
page: MyApp.API.QueryParser.Page,
sort: MyApp.API.QueryParser.Sort
]
The parsers take the query parameter value as input and its return value is placed under an
attribute with the same name as the query parameter in the JSONAPIPlug
structure in the conn
private
assigns so that you can retrieve it and use it in your application logic.
You can return a standard JSON:API
error to the client by raising JSONAPIPlug.Exceptions.InvalidQuery
exception at any point in your parser code.
Summary
Callbacks
Transforms the value of the JSON:API 'filter' query parameter
Callbacks
@callback parse(JSONAPIPlug.t(), Plug.Conn.query_param()) :: term() | no_return()
Transforms the value of the JSON:API 'filter' query parameter