JsonApi.Resource exposes the Resource type and functions to get and set information
for your resources.
This type is used to represent either or or many relationships in your Resource object.
See withRelationship function for more information
Builds a new Resource with the specified type name
You can build your resources like this:
myResource : Post -> Resource
myResource post =
build "posts"
|> withId "post-1"
|> withLinks (Dict.fromList [ ( "self", "http://url-to-post/1" ) ])Builds a new Resource from the specified Resource and with the specified type name
You can build your resources like this:
myResource : Resource -> Resource
myResource resource =
fromResource "posts" resourceReturns the id of your resource.
From the json example above, id will return 13608770-76dd-47e5-a1c4-4d0d9c2483ad
Returns the links of your resource.
From the json example above, links will return a Dict with this value:
[ ( "self", "http://link-to-user" )
, ( "profile", "http://link-to-user-profile" )
]Returns the type of your resource.
From the json example above, type_ will return users
Sets the id of the Resource object
myResource : Post -> Resource
myResource post =
build "posts"
|> withId "post-1"Sets the links of the Resource object
myResource : Post -> Resource
myResource post =
build "posts"
|> withLinks (Dict.fromList [ ( "self", "http://url-to-post/1" ) ])Sets the attributes of the Resource object.
This is the payload of your resource.
myResource : Post -> Resource
myResource post =
build "posts"
|> withAttributes
[ ( "firstname", string "John" )
, ( "lastname", string "Doe" )
, ( "gender", string "male" )
]Adds a relationship in the Resource object.
You have to pass it the name of the relationship and a description of the relationship resource (See relationship and relationships)
myResource : Post -> Resource
myResource post =
build "posts"
|> withRelationship "creators" (relationship creator.id (creatorResource creator))Defines a relationship that can then be added to its parent Resource.
It takes the id of the resource and the resource.
creatorResource : Creator -> Resource
creatorResource creator =
build "creator"
|> withAttributes [ ( "firstname", string creator.firstname ) ]
myResource : Post -> Resource
myResource post =
build "posts"
|> withRelationship "creators" (relationship creator.id (creatorResource creator))Defines a list of relationships that can then be added to a parent Resource.
It takes a List of Tuples with the id of the resource and the resource.
commentResource : Comment -> Resource
commentResource comment =
build "comment"
|> withAttributes [ ( "content", string comment.content ) ]
myResource : Post -> Resource
myResource post =
build "posts"
|> withRelationship "comments" (relationships (List.map (\comment -> ( "comment", commentResource comment )) comments))
The
Resourcerepresents a resource. It is passed to your resource decoders, but you can also use it to encode resources to json api, via aDocument. It contains useful information for decoding and encoding your resource: resourceid,links,attributes,relationships, ..._Example of json api resource:_
And how to build it with the
JsonApi.Resourcemodule: