Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected plural when making relationships #63

Closed
ben221199 opened this issue Apr 3, 2021 · 4 comments
Closed

Unexpected plural when making relationships #63

ben221199 opened this issue Apr 3, 2021 · 4 comments

Comments

@ben221199
Copy link

I made a schema for my model Token with the following fields:

public function fields(): iterable{
	return [
		ID::make(),
		Str::make('token'),
		Str::make('token_type'),
		DateTime::make('created_at')->sortable()->readOnly(),
		Boolean::make('is_deleted'),
		BelongsTo::make('parent','parent'),
		BelongsTo::make('users'),
	];
}
public function parent(): BelongsTo{
	return $this->belongsTo(self::class);
}

public function user(): BelongsTo{
	return $this->belongsTo(User::class);
}

However, when I request the endpoint, I get an 500 error. When I look at the logs:

[2021-04-03 11:49:26] local.ERROR: Unable to encode compound document. {"userId":1,"exception":"[object] (LogicException(code: 0): Unable to encode compound document.
...
[previous exception] [object] (LogicException(code: 0): No schema for JSON:API resource type parents.
...

In the error, the resource type is suddenly plural. Why does this happen? (It also happens with user.)

Note, for my class Token, the following constants exist (and as you see, they are all different):

  • Token::class The full name of the class
  • Token::TYPE The type of the resource; in this case auth_token; will be in the JSON type field: type="auth_token"
  • Token::URI The uri of the resource; in this case tokens; will be in the URL of my API: /v1/tokens/1?page[number]=2
  • Token::PARAM The param of the resource; in this case token; will be the name of the parameter: /v1/tokens/{token}
@catalinux
Copy link

You will have de declare the type

Smth like that

`

BelongsToMany::make('users')->type('user')

`

@ben221199
Copy link
Author

ben221199 commented Apr 3, 2021

Thanks, that worked, but why is this needed? With this code I connect a field name (parent) to a relation name (also parent). This relation already knows the model type (self::class -> Token::class) of the resources it is related to and with that class name you couls actually resolve the right schema (TokenSchema), right?

@lindyhopchris
Copy link
Contributor

It's because the default behaviour of the package is to assume that resource types are pluralized.

Hmmm, yeah I suppose for Eloquent relations you could lookup the inverse schema to get the type. My only hesitancy with doing that is it adds a load of extra processing (imagine every schema had to look it up for every relationship, that's potentially a lot of relationships).

One other thing is that the relationship fields are not currently injected with the schema they belong to. That would have to happen so they can get the model class, to then lookup the relation, to then get the model from the relation, to then resolve the correct schema using that related model, to then get the schema type. That again would need to happen for every single relation.

@ben221199
Copy link
Author

Okay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants