Download the PHP package wwwision/graphql without Composer
On this page you can find all versions of the php package wwwision/graphql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wwwision/graphql
More information about wwwision/graphql
Files in wwwision/graphql
Package graphql
Short Description Base package to create GraphQL endpoints with Flow
License MIT
Informations about the package graphql
Wwwision.GraphQL
Easily create GraphQL APIs with https://flow.neos.io/.
Background
This package is a small collection of tools that'll make it easier to provide GraphQL endpoints with Neos and Flow. It is a wrapper for the PHP port of webonyx that comes with automatic Schema generation from PHP code (using wwwision/types) and an easy-to-configure PSR-15 compatible HTTP middleware.
Usage
Install via composer:
Simple tutorial
Create a class containing at least one public method with a Query
attribute (see wwwision/types-graphql for more details):
Now define a virtual object for the HTTP middleware
in some Objects.yaml
configuration:
And, lastly, register that custom middleware in Settings.yaml
:
And with that, a working GraphQL API is accessible underneath /graphql
.
Complex types
By default, all types with the same namespace as the specified API class will be resolved automatically, so you could do:
as long as there is a suitable Name
object in the same namespace (Your\Package
).
To support types from different namespaces, those can be specified as third argument of the GraphQLMiddlewareFactory
:
Authentication
Commonly the GraphQL middleware is executed before the routing middleware. So the Security\Context
is not yet initialized.
This package allows you to "simulate" an MVC request though in order to initialize security.
This is done with the fourth argument of the GraphQLMiddlewareFactory
:
[!IMPORTANT]
There must not be any gaps in the argument definitions due to the way Flow parses this configuration To only specify the simulated controller, you can pass an emptyvalue: []
array for the 3rd argument
Custom Resolvers
Starting with version 5.2 custom functions can be registered that extend the behavior of types dynamically:
[!NOTE] The
resolverMethodName
can be omitted if it is equal to the custom field name[!IMPORTANT]
There must not be any gaps in the argument definitions due to the way Flow parses this configuration To only specify custom resolves, you can pass an emptyvalue: []
array for the 3rd argument andvalue: null
for the fourth
All custom resolvers have to be public functions with the extended type as first argument (and optionally additional arguments) and a specified return type
For the example above, the corresponding resolver class could look like this:
More
See wwwision/types and wwwision/types-graphql for more examples and how to use more complex types.
FAQ
Q: How can I implement lazy-loading?
The major rewrite with version [5.0](https://github.com/bwaidelich/Wwwision.GraphQL/releases/tag/5.0.0) led to all fields of a type to be loaded and encoded by default. In my experience, that leads to a better performance due to the reduced i/o and (de)serialization that comes with lazy-loading every field. However, if you work with highly complex or nested types, the overhead of pre-loading all fields can be a problem. In that case you can simplify the structures by adding more specific rootlevel queries. Alternatively you can use [Custom Resolvers](#custom-resolvers). I would like to make it easier to provide lazily loaded fields (see https://github.com/bwaidelich/types-graphql/issues/6), but currently I don't have the personal need for this feature.Q: I have issues with Neos Flow proxy classes
The [wwwision/types](https://github.com/bwaidelich/types) package, that this library is built on top of, relies on constructors to contain all involved fields (see https://github.com/bwaidelich/types/blob/main/README.md#all-state-fields-in-the-constructor). Flow Proxy classes (and due to a [bug](https://github.com/neos/flow-development-collection/issues/3060) that is practically every class of a Flow package) override the constructor with one that has no parameters. As a work-around you can add a `#Flow\Proxy(false)` attribute to the affected classes. I'm thinking about adding an extension point to the parser to allow proxy classes out of the box (see https://github.com/bwaidelich/types/issues/6), but currently I don't have the personal need for this feature.Q: What about a GraphQL Schema generator from doctrine entities?
Exposing entities directly to an API can be problematic because it increases coupling and can impede maintainance. However, sometimes and especially with smaller API it is the most pragmatic solution to use the same entity classes and value objects in the core as well as "on the edge". Personally I would avoid exposing (or even using) doctrine entities because they tend to lead to [Anemic Domain Models](https://martinfowler.com/bliki/AnemicDomainModel.html) and couple the core domain to the infrastructure. Instead, I prefer to use the PHP type system as much as possible (and the [wwwision/types](https://github.com/bwaidelich/types) package for enforcing validation) and [adapters](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software)) to map those from/to database records. With all that, it should still be possible to derive the GraphQL schema from doctrine entities as long as the constructor contains all fields and the class is not proxied by Flow (see above): With https://github.com/bwaidelich/types/issues/6 integration could be improved probably.Q: How to deal with breaking changes in version 5.0?
[Version 5.0](https://github.com/bwaidelich/Wwwision.GraphQL/releases/tag/5.0.0) was a major rewrite of this package with a new foundation and philosophy. If that approach does not work for you at all, you can still use older versions of this package, I plan to support version 4.x for a while!Q: What about feature x?
I mainly created this package for my own projects and those of my clients, but of course it makes me happy to see it being used elsewhere. So feel free to provide [feature suggestions](https://github.com/bwaidelich/Wwwision.GraphQL/issues) or even [implementations](https://github.com/bwaidelich/Wwwision.GraphQL/pulls) but please don't expect me to comply as I have to maintain this package in my free time. If you need a specific feature implemented or bug fixed, you can of course also hire me to do so!Contribution
Contributions in the form of issues or pull requests are highly appreciated
License
See LICENSE
All versions of graphql with dependencies
neos/flow Version ^7.3 || ^8.0
wwwision/types-graphql Version ^1.2
webonyx/graphql-php Version ^15
psr/http-message Version ^1 || ^2