 Thank you very much. Okay. So let's get started. So I've already introduced my talk I'm gonna share my screen here and yeah So basically there will be three parts in this talk The first part is about the ecosystem the GraphQL ecosystem in PHP. It does exist and it is actually quite large then How to choose the right library for your project if you're doing some PHP and Finally a demo of So about the ecosystem well I'm sure all of you already know that when you're doing GraphQL you need a GraphQL client in the browser and a GraphQL middleware on the server side and you have actually implemented implementations of GraphQL middlewares in All in many languages of course not JS. It started there But also PHP, Java, C sharp or whatever Today we're going to be zooming on PHP and if we have a look at the PHP GraphQL middlewares, there are actually two kinds of Such middlewares and I will split them a bit arbitrarily arbitrarily in two sections, one that I call core libraries and another that I call proper libraries and Core libraries are actually middlewares that are quite low level They are used to pass the GraphQL queries to serve The response in JSON They are really powerful But they are kind of hard to use the developer the developer experience to use such Library is kind of poor. They are hard to use and so on top of those core libraries People build wrapper libraries, which are actually libraries that use core libraries and that are more high-level more opinionated Certainly easier to use maybe not that not as much powerful as the core library But basically most of the time when you're doing an application you will be choosing a wrapper library, right? In PHP, if we have a look at core libraries There are not that many of them. There are actually two right now. There is Webonics Which is the de facto standard in PHP almost everybody is using Webonics and then there is a newcomer that is called Velity, which is developed by Some Russian guys. It's in active development. It looks really promising There is no solid documentation yet. So Right now if you need a core library, definitely go to Webonics, but keep an eye and high on Velity There was in the past more libraries here. There was one was that was called Ushido that has been abandoned in favor of Webonics So right now it's pretty much Webonics if you want a core library And then on top of this core library, there are wrapper libraries and there are many of them and Today All those libraries are using Webonics and does a hood. Okay, it's a wrapper around Webonics to make it easier to use So basically we have API platform and the overblock GraphQL bundles that are really Used with the symphony framework if you're doing Lara rule you can use a library called lighthouse I will be present in those a bit later if you're doing What if you're using a CMS like WordPress or Drupal WordPress and Drupal have modules to plug to present a GraphQL API So if you want to do a headless CMS It's quite easy using what but WordPress or Drupal and adding a GraphQL module on them and then there is GraphQLite Which I've been authoring for the last two years and which is a framework agnostic You can use it whatever the framework you use Let's make a quick look a quick zoom on Webonics. Okay, so Webonics it is a core library here and How does it look like coding with Webonics? Well here? I've got a GraphQL type Okay, and basically this type is a story. It has fields with which all have types Building that in Webonics the code would look like this. Okay, it's kind of verbose And as I told you the developer experience is not that great, but basically I'm treating the type It's name is Story here, and I've got another of fields That are declared here with the type of the field the description if I've got arguments arguments are declared here and Finally, I have a resolver that will be called when I query this field the resolver is called to fetch the data This is really verbose, right? And also it might look a bit like Some libraries some GraphQL libraries in a JavaScript, especially if you have been doing been using Express GraphQL Webonics started by mimicking a lot what Facebook was doing in its own JavaScript library If I want to declare a query, well, I'm creating a type query whose fields are here It's actually exactly the same thing as defining a type because in GraphQL a query is a field on the type whose name is query and Finally, if I want to resolve a query I can close the exit with query static method That's in parameters the schema the schema is An object containing all the types that I have declared I'm passing in the query stream So the the GraphQL query and it will return an array that I that I can cast to Jason and return to the client so This is the experience you're getting when you're using Webonics directly, right? And the problem with this approach is that it actually requires a lot of work to set up a Graphq a GraphQL API Obviously there are gains to use GraphQL versus rest its queries are client driven. It has introspection You can see the schema from the client It has three types, which is definitely one of the really cool features But if it is too much of pain to to put the API in place Well, it may be won't be worth it So what we are going to do is we are going to use a wrapper library, which is easier to work with to Offsets the gains versus the costs and to make it worthwhile to use GraphQL Now if we need to choose a wrapper library Well, there are basically two kinds of such libraries There are schema first libraries and code first libraries in a schema first wrapper library basically Your task as a developer will be to write the GraphQL schema and Then the job of the library will be from the schema To build to to find a way to link the schema to the PHP code On the other hand, if you are using a code first approach, you will be designing objects And then the job of the library Will be to generate the schema from the classes and the objects you've been designing So and basically When you are using a language that is loosely typed you tend to have libraries that are schema first If you're doing JavaScript, you will find a lot of schema first libraries on the other end If you are using a strictly typed language like Java, you will find a lot of code first libraries PHP sits a bit in the middle because it is optionally typed You can have types, but you're not forced to and so you have the two kinds of libraries out there and basically in the schema first part you have Overblog, Lighthouse, Railty, Cylur and in the code first category API platform GraphQLite and Get Pop Let's make a quick zoom on Lighthouse Okay, so Lighthouse is a schema first library. Let's see how it works Lighthouse is built on top of Laravel When you are using Lighthouse, what you are going to do is you are going to design this GraphQL schema and Basically, you are going to annotate this schema with directives. Okay. This is a directive This is another directive and These directives will be used by Lighthouse to bind this schema to the data model directory Basically, under the hood Lighthouse is using Eloquence and Eloquence is the ORRM of Laravel So basically if I'm using the ORT directive automatically Lighthouse will put the current user in the Mequery You can do quite powerful stuff with only directives. For instance, the CRETE directive here will CRETE directly an object in database When you when you call CRETE post and it will put a post directly into the database and you can do validation Right here on each field Okay, so it's very efficient I mean you can build a complete GraphQL API in very few lines of code with this approach Also, it is very very tied to Laravel and to Eloquence, which is the ORRM so if you want to do advanced things and if you don't want to To be too much tied to your database, you will have to write custom directives, which is a bit of a pain But if you have a simple use case and you only want to do CRETE binding to a database, it is a great tool Okay, one thing that is also interesting. It has support for subscriptions, which is so you can do real-time GraphQL can answer in real-time, which is not that common in PHP So that is Lighthouse Let's now make a zoom on API platform So API platform, okay, it is a code first a library In API platform, the philosophy is the opposite. You start by writing a PHP class and then you're going to put annotations on the class And API platform will read the annotation and generate the GraphQL schema for you Okay API platform was first built with the rest philosophy in mind And What is kind of fun with the API platform is that you can expose quite easily both a rest API and a GraphQL API Very few lines of code. So you get both kind of API Support using the same code, which is cool However, if you want to have a good Control on the GraphQL schema that is generated, API platform may not be the best The best tool out there because the underlying philosophy is very much Rest, so you can read, read, update and delete a report. If you want to do some other things Well, it will be a bit harder to customize It has support for subscriptions too Yeah, so that was a zoom on Lighthouse and API platform There are many also libraries out there and I've been preparing a few schemas To help you decide what library you can use by answering a few questions. So let's jump into that So basically if you want to pick a GraphQL library, I think the first and if you are doing PHP The first question you should ask is are you developing an application or a tool? Okay. So basically If you are Doing If you are developing a tool, if your model is dynamic, for instance, if you're Sorry If you are developing a CMS and the end user can create a new content type Can treat new type, add new fields, stuff like that. Then you want to go for web links Okay, you want to go low level and read your own tool on it If you are developing an application and you have a set of classes already well defined, then you want to go for a GraphQL library Then there is a question. Do you need subscriptions? If you want to do things in real time Then you have to pick a library that supports subscriptions. There are not many out there It's Lighthouse, API Platform, Siler or web links. Otherwise, you can choose any library Then there is another question, which is do you want to do you want to use relay on your front end? If you are using relay Relay imposes a number of constraints on the schemas that are generated and not all the libraries out there have support for this kind of schema So if you want to do to use relay, you should definitely aim for those libraries Maybe Lighthouse and GraphQL bundles Finally, depending on the framework you are using, you will also have to put to pick one of those Libraries Some libraries are framework agnostics like WebNX and GraphQLite and you can use them everywhere Right? So finally the last question Do you prefer a schema first or a code first approach? If you want to go schema first, pick one here If you want to go code first, pick one here And when you answer all those questions, if you are lucky, well, you still have a choice between one or two libraries If you are unlucky, there are no libraries Answering you need, for instance, if you want to do To use a code first approach in Laravel And using subscriptions Well, you can't But yes, this approach should help you choose one of those libraries And I tried to be not too much biased But yes, of course, I altered GraphQLite And so, well, now we will enter the third phase of this talk And I will be doing a quick demo about GraphQLite Before doing the demo, I will try to explain you the philosophy that drove GraphQLite Basically, my idea was Well, let's imagine you want to do a simple echo query in PHP Okay, if you want to do such a query in GraphQL, it will look like this I'm creating a query type And basically, yes, there is a field echo, it takes a message, it will return Well, I'm doing an echo To build that using Webonics, I would need to put this code I already presented it, I need to put an echo field Which returns a string that takes into argument a string And that results by reading the arguments and returning them But if I want to write the same echo in pure PHP Basically, it only takes four lines Basically, the function name could map the query name in GraphQL So, the parameters here could map GraphQL arguments And the return type is already defined And then the body of the function is actually my resolver So, what would be really, what if I was able to simply put a query annotation here And while this query annotation would automatically tell my library GraphQLite, in my case, that this function should be actually a GraphQL query Expose it in GraphQL, that's it And so that's the philosophy behind GraphQLite, using annotations And using the fact that PHP is already typed And basically taking PHP types and mapping them automatically to GraphQL types So, this is GraphQLite GraphQLite is a mapper between PHP types and GraphQL types And what is kind of fun is that it actually plays quite nice with Doctrine Doctrine is the ORM used by Symphony And basically in Doctrine, you put annotations in your PHP objects And Doctrine is doing a mapping between PHP objects and database tables And from the PHP objects, it can generate the database model And with GraphQLite, we are doing the same thing We are starting from the PHP objects And we are generating a GraphQL schema So, let's go for the live coding Quick node, I already told you it is Fremontagnostic You can use it starting from PHP 7.2 and it's based on Webonics Now, for the hard part for me, the demo So, we are going to make a quick demo with a marketplace And basically, my marketplace has companies And each company can have several products And we will be focusing on this relationship between companies and products In the next five minutes, right? So, let's jump to the code Here, I already did set up GraphQL GraphQLite in the Symphony project The database is already loaded And I wrote a company controller Okay, so it's actually a controller Like you can find some in the MVC frameworks Except it is in the GraphQL controller namespace And if you want to put queries or mutations somewhere in your code You will put them in a controller So, my controller is fed with a company repository The company repository is the object that will access the database Okay? And I have this method, which is called GetCompanies That takes a search string And that will query the database for the string and return the results What I'm going to do is I'm going to put an annotation here And I'm going to put a query annotation to tell GraphQLite Okay, take this company's function and expose it in the GraphQL scheme If I go in Firefox here I've got a graphical test client setup So I'm going to refresh the page And oops, okay, I'm getting an error Let's make a quick zoom on this error And basically what GraphQLite is telling me Is that in the GetCompanies method A type hint is missing Okay, you remember that PHP is optionally typed But GraphQL must have types So when I'm using GraphQLite I must put type into all my functions All my queries Here I did not define any return type So I need to define one So I'm going to say GetCompanies is returning an array of companies Now I have a problem It's that PHP does not have a notion of generics So I cannot say something like Okay, it's an array of companies This is not valid PHP, right? So what I'm going to do Is I'm going to put a comment in the PHP doc And in the PHP doc I can put anything And especially I will give a type hint To GraphQLite and telling him That this array is actually an array of companies So let's go back to Firefox Refresh the page And oh, I'm getting another error Okay, let's zoom on this error It's error-driven development I'm a big fan of error-driven development It works really well I actually tried to make sure That all the error messages are quite clear It's really important And in your own code try to pay attention On the error messages you are generating You are doing your future self favor So basically it's telling me that In GetCompanies it cannot map the class Company to a known GraphQL type What GraphQLite is actually telling me Is that I did not tell him explicitly That company is a GraphQL type And so to do that I simply add an annotation Which is the type annotation And the type annotation is telling GraphQLite Okay, map this class to a type Of course a type must have some fields So I'm going to put annotations in the getters Here and I'm going to say Okay, expose ID as a field And name as a field And maybe website as a field too Also notice that I'm putting the field annotation On getters I'm not putting the annotation on the properties The properties are private So I cannot access them directly The tree has annotation on properties Because it is doing some black magic And actually accessing directly private properties I'm not doing that in GraphQLite So yes, I must put the field annotation on the getters So let's go back to GraphQL I'm refreshing the page I don't have any error So it looks good And if I'm looking at writing a query My company's probably just appeared And yeah, I can do a search On any company's container A And I'm going to display the ID and the name Place a request And it is working And if I'm adding the website Great, good So basically you can see that I managed to create a GraphQL API By basically adding three annotations Query, type and field Which is pretty cool And yeah Let's So far I only fetched fields from companies So let's do some binding with a product So I'm going to go in the get products Getter And I'm going to add a field on it I'm going to refresh the page And I'm getting another error If I'm zooming on this error It is telling me that it cannot map the product class To a known GraphQL type We already saw this error Two minutes ago, so I'm pretty sure you know what I should do I should go in the product class And instruct GraphQL on it That the product class is also a type And so I'm putting the type here And I'm going to put A field annotation On the ID, on the name On the price, on the margin of the product And if I refresh in the page Yeah, everything seems to be going great And I can access the products And access the ID and the name of the product And it's working Yeah, so that's it And basically my job When I set up a GraphQL API It becomes adding a few annotations And yeah, that's it So, yeah For the live demo I won't be going any further I will be presenting you very quickly A few additional features For instance, if you want to put To create a mutation You have a mutation annotation That can be used to create a mutation It works exactly the same as the query annotation works There is a built-in support For authentication and authorization That is plugged into your favorite framework Laravels, Infoni, whatever But basically if you are using the right annotation And passing the name of a right Well, then this query will be Visible only if the user That is currently logged has this right You can also use the right annotation And fill it directly You can even do fine-grained authorization If you want to say that Some objects can be seen, but not others In this example, I've got An email field from a user And basically I am saying that I am looking at the company Of the user, of the current user And comparing it with the company Of the user that is logged User here refers to the user That is currently logged And basically this is saying I can see the email of the people That are in the same company as me I can see the email of my colleagues But I cannot see the email Of the one that is not my colleague So with this security annotation We can have something that is going quite deep To do fine-grained authorization Another feature I like a lot Is auto wiring Basically here we are In a domain object, a product And let's admit I want to compute The VAT of a product Computing the VAT of a product Can be quite complicated Maybe I need to know The type of the product The country the user is in So it can be difficult And we typically won't do it In a domain object, but rather We will call a service But when you are in a domain object Accessing services is difficult And what we can do here Is auto wire, call the auto wire ring And basically graph polite Will automatically inject When I'm calling the VAT field It will automatically inject From the dependency injection container A service, the VAT service And I can use it directly In the resolver here So it enables me to access services And from a domain object By passing those services In parameters of the resolver Which is kind of cool Yeah, there are many, many more features Available We have support for validation For Indian types Even if they don't exist in PHP Filer codes What I invite you to do If you are interested In trying out Graph polite is having a look At the documentation Everything is, I hope, clearly documented And if you have any issue Well, you can always contact me Either on Twitter directly Or if you prefer Just open an issue On the graph polite repository Yeah, so, yeah, that's wrap it up Let's wrap this talk Basically, doing GraphQL in PHP Is definitely possible You have many possibilities out there And since GraphQL is pretty cool Well, I definitely invite you to try it out And that's it Do you have any questions? Thanks so much, David It was such an incredible talk I really enjoyed it All the live current demo was good Thank you very much Yeah, everything was incredible So that's great So, yeah, we have a question Yes, so Florian He's asking about If graph polite Is going to support Or is currently supporting subscriptions It is not currently supporting subscriptions I plan to add support For subscriptions in the future Maybe not in the coming months Because I want to release a version 4.1 of GraphQLite And I know subscriptions Won't be part of this release But yes, I would like to work on that For the end of the year Definitely That sounds super exciting, I guess I think I have another question Because I found out super, super interesting So I would like to understand Why did you decide to go and build This incredible technology And what are the future plans for it? Oh, okay Well, when I started writing GraphQLite It was two years ago And basically you add only two options at the time It was using Webonics directly Or using Ushido Which is a library that has been abandoned And basically there were really low level libraries And they were hard to use And I had this huge project For a client of mine We are just at the coding machine We are doing mostly intranet and extranet applications And a client of mine Had this huge REST API With a lot of endpoints And maybe redundant endpoints I wanted to do GraphQL Because I knew that the flexibility of GraphQLite Would be really, really beneficial for that client And the only option I had was Webonics Which was too hard to use And so I decided I started writing a small library Which became a bit more complex And I did two, three versions of the library And then I told myself Okay, I must work on it really silently Put it on open source it And that's how GraphQLite was born, actually Well, that sounds so great And I think thanks so much for contributing With the GraphQL community In this case in PHP Because I think there are not so many people That either they don't know That they are able to do this Or they realize that it's a bit more difficult To start contributing I think this is super, super, super interesting And I think if the people they do know This library, they are going to start to use it They are going to like it And they are going to adopt it in their company So thanks so much for sharing this knowledge with us You're welcome It's a pleasure to read it Thanks so much for contacting us to speak Because it's been difficult to find People that they have come from Hong Kong Although you are not in Hong Kong right now But you are really connected to the Hong Kong community So glad to have you here Thanks so much and really, really hope to see you soon Thank you very much Okay David, thanks Up next