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

Allow embedding to rely on an interface #138

Open
smyrman opened this issue Sep 18, 2017 · 4 comments
Open

Allow embedding to rely on an interface #138

smyrman opened this issue Sep 18, 2017 · 4 comments

Comments

@smyrman
Copy link
Collaborator

smyrman commented Sep 18, 2017

Today embedding is done by checking if a schema.FieldValidator (or fallback validator stored within a resource's index) type asserts to &schema.Resource or &schema.Connection. This makes it hard to write custom reference Validators and have them work just as well as the default ones. This could sometimes be useful when you have very application specific validation requirements for linked resources.

One solution to this, cold be to let schema.Resource and schema.Connection implement an interface each that let's you lookup the linked resource. E.g.

type ReferenceType interface {
        LinkedResource() query.Resource
}
type ConnectionType interface {
        LinkedResource() (field string, query.Resource)
}

An alternate (and faster) implementation would be to return the Path instead of the query.Resource, but that might be less flexible, e.g. if we wanted to support exotic use-cases such as "external resources".

@smyrman
Copy link
Collaborator Author

smyrman commented Sep 18, 2017

As an example of the latter use-case, and "external resource" could be a resource (e.g. "users") that should be fetched from a different REST-API that we have somehow managed to wrap into a query.Resource compatible type.

@rs
Copy link
Owner

rs commented Sep 18, 2017

Sounds like a good idea.

@smyrman smyrman changed the title Allow embedding to rely on an interface. Allow embedding to rely on an interface Sep 19, 2017
@smyrman
Copy link
Collaborator Author

smyrman commented Sep 19, 2017

There is one challenge I don't know how to solve (with or without this change) and that is how to support embedding if a schema.Reference is member of a nested type such as a schema.Dict or schema.Array....

"x": {
    Validator: &schema.Dict{
        ValuesValidator: &schema.Reference{},
    },
},
"y": {
    Validator: &schema.Array{
        ValuesValidator: &schema.Reference{},
    },
},

Today I believe it only works for subschemas via the field.Schema attribute:

if def != nil && def.Schema != nil {

With regards to #77 that's probably not the best way to deal with this. Maybe another interface to inform about children?

type NestedType interface {
    Children() schema.Fields
}

Sub-schemas (i.e. schema.Object) would simply returns it's Fields instance, where as Arrays and Dicts would use a wildchar "*" maybe. For types without Fields (e.g. schema.Dict and schema.Array), a field could be created ad-hock, or the validators could actually change to replace the ValuesValidator with a Field attribute...

This would make the REST embedding syntax for nested "arbitrary fields" function like this:

fields=x{*},y{*}

What about GraphQL?

@smyrman
Copy link
Collaborator Author

smyrman commented Sep 6, 2018

Should probably deprecate schema.ReferenceChecker, and use query.Resource as the inteface for item extraction.

type Resource interface {

Otherwise see in light of #77, #192, #194.

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

No branches or pull requests

2 participants