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

Deprecate callbacks in field types #35

Closed
vladar opened this issue Apr 24, 2016 · 9 comments
Closed

Deprecate callbacks in field types #35

vladar opened this issue Apr 24, 2016 · 9 comments

Comments

@vladar
Copy link
Member

vladar commented Apr 24, 2016

Deprecate following field definition form:

[
    'name' => 'Foo',
    'fields' => [
        'bar'  => ['type' => function()  use(&$barType) { return $barType;}],
        'baz'  => ['type' => function()  use(&$bazType) { return $bazType;}],
    ]
]

in favor of:

[
    'name' => 'Foo',
    'fields' => function() use (&$barType, &$bazType) {
        return [
            'bar' => ['type' => $barType],
            'baz' => ['type' => $bazType],
        ];
    }
]
@ooflorent
Copy link

Is there any reason why? What's the motivation behind this change?

@vladar
Copy link
Member Author

vladar commented Apr 24, 2016

This seems redundant. Also first notation adds some performance overhead because every field type has to be checked with is_callable and then validated against base Type class.

Pretty much everything you can do with first notation can be done with second as well. Also that's how reference js implementation works. It turns out that maintaining this library is way easier when there is little deviation from reference implementation (at least until things settle down with GraphQL itself).

But if you have any use-cases which require this feature - please describe them. That's pretty much the reason why I created these issues - to start a discussion if somebody needs them %)

@mcg-web
Copy link
Collaborator

mcg-web commented Apr 25, 2016

👍 for this change! it will be much more easier to implements dynamics fields without having to used hacks.

@vladar
Copy link
Member Author

vladar commented Oct 23, 2016

Done in c11f257

@decebal
Copy link
Contributor

decebal commented Jan 13, 2017

must compliment you guys for referincing this issue in the actual error message! 🥇

@Alexandru-Dobre
Copy link

Is there any chance the first notation could be permitted once again ?
We have a library that uses this and json models in order to build a loopback-like php graphql api factory.
Having to move the callback to the fields rather than the type of each field would greatly complicate things since the field is linked to the middleware-connector pipeline during field construction. Here's a link to the concerned code : https://github.com/ampize/alambic/blob/master/src/Alambic/Alambic.php

@vladar
Copy link
Member Author

vladar commented Jan 20, 2017

@Alexandru-Dobre We can discuss it, but first I would like to figure out why it is impossible to change the code of your library for this notation (as it is also the only way reference js implementation allows you to define fields). I am pretty sure there should be a way to do this.

What will happen if you change these lines to:

$typeArray = [
    'name' => $type['name'],
    'fields' => function() use ($type) {
        $fields = [];
        foreach ($type['fields'] as $fieldKey => $fieldValue) {
            $fields[$fieldKey] = $this->buildField($fieldKey, $fieldValue);
        }
        return $fields;
    }
];
if (!empty($type['description'])) {
    $typeArray['description'] = $type['description'];
}
$this->alambicTypes[$typeName] = new ObjectType($typeArray);

Obviously you will have to remove closure for type option in buildField (it is also quite possible that you will have to wrap remaining code of this method to same closure as well).

Why this will not work?

@Alexandru-Dobre
Copy link

@vladar Thank you for the suggestion! It worked perfectly with minimum changes to our library code. The rest of the method doesn't need closures as only the "fields" part was exposed to a cyclic dependency issue (models for SQL databases had foreign keys pointing to each other) since we automatically exclude non-scalar fields as query args.

@vladar
Copy link
Member Author

vladar commented Jan 20, 2017

Nice!

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

5 participants