< Return to Video

Building a GraphQL API in PHP with GraphQLite - GraphQL Hong Kong

  • 0:01 - 0:03
    thank you very much okay so let's get
  • 0:02 - 0:06
    started
  • 0:03 - 0:10
    so i already introduced my talk uh
  • 0:06 - 0:13
    i'm gonna share my screen here
  • 0:10 - 0:14
    and yeah so basically there will be
  • 0:13 - 0:16
    three parts
  • 0:14 - 0:19
    in this talk the first part is about the
  • 0:16 - 0:22
    ecosystem the graphql ecosystem in php
  • 0:19 - 0:23
    it does exist and it is actually quite
  • 0:22 - 0:26
    large
  • 0:23 - 0:27
    then how to choose the right library for
  • 0:26 - 0:30
    your project
  • 0:27 - 0:30
    if you're doing some php and finally a
  • 0:30 - 0:34
    demo
  • 0:30 - 0:37
    of graphqlite
  • 0:34 - 0:40
    so um about the ecosystem
  • 0:37 - 0:42
    well i'm sure all of you already know
  • 0:40 - 0:44
    that when you're doing graphql you need
  • 0:42 - 0:46
    a graphql client in the browser and a
  • 0:44 - 0:48
    graphql middleware
  • 0:46 - 0:49
    on the server side and you have actually
  • 0:48 - 0:52
    implemented
  • 0:49 - 0:56
    implementations of graphql middlewares
  • 0:52 - 0:59
    in all in many languages
  • 0:56 - 1:00
    of course not js it started there but
  • 0:59 - 1:03
    also php
  • 1:00 - 1:06
    java c-sharp or whatever
  • 1:03 - 1:10
    today we're going to be zooming on php
  • 1:06 - 1:13
    and if we have a look at the php
  • 1:10 - 1:13
    graphical middlewares there are actually
  • 1:13 - 1:17
    two kinds
  • 1:13 - 1:18
    of uh such middlewares and i will split
  • 1:17 - 1:21
    them
  • 1:18 - 1:24
    a bit arbitrarily arbitrarily in two
  • 1:21 - 1:25
    uh sections ones that i call core
  • 1:24 - 1:26
    libraries
  • 1:25 - 1:28
    and another that i call dropper
  • 1:26 - 1:31
    libraries and
  • 1:28 - 1:31
    core libraries are actually middlewares
  • 1:31 - 1:35
    that are
  • 1:31 - 1:39
    quite low level they are used to parse
  • 1:35 - 1:43
    the graphical queries to serve
  • 1:39 - 1:45
    the the the response in json
  • 1:43 - 1:46
    they are really powerful but they are
  • 1:45 - 1:48
    kind of
  • 1:46 - 1:50
    hard to use the the developer the
  • 1:48 - 1:53
    developer experience
  • 1:50 - 1:56
    to use such uh library is
  • 1:53 - 1:56
    kind of poor they're hard to use and so
  • 1:56 - 2:00
    on top
  • 1:56 - 2:02
    of those core libraries people build
  • 2:00 - 2:04
    wrapper libraries which are actually
  • 2:02 - 2:06
    libraries that use core libraries
  • 2:04 - 2:08
    and that are more high level more
  • 2:06 - 2:10
    opinionated
  • 2:08 - 2:13
    certainly easier to use maybe not that
  • 2:10 - 2:15
    not as much powerful as the call library
  • 2:13 - 2:16
    but basically most of the time when you
  • 2:15 - 2:18
    are doing an application
  • 2:16 - 2:21
    you will be choosing a verbal diagram
  • 2:18 - 2:23
    right
  • 2:21 - 2:24
    in php if we have a look at core
  • 2:23 - 2:26
    libraries
  • 2:24 - 2:28
    there are not that many of them there
  • 2:26 - 2:31
    are actually two right now
  • 2:28 - 2:33
    there is webinaics which is the de facto
  • 2:31 - 2:36
    standard in php
  • 2:33 - 2:38
    almost everybody is using webinx and
  • 2:36 - 2:42
    then there is a newcomer that is called
  • 2:38 - 2:45
    ray welty which is developed by
  • 2:42 - 2:45
    some russian guys it's an active
  • 2:45 - 2:49
    development
  • 2:45 - 2:51
    it looks really promising there is no
  • 2:49 - 2:55
    solid documentation yet
  • 2:51 - 2:57
    so uh right now if you
  • 2:55 - 2:58
    need a co-library definitely go to
  • 2:57 - 3:01
    webonix
  • 2:58 - 3:04
    but keep an eye and high on
  • 3:01 - 3:07
    railway there was in the past
  • 3:04 - 3:08
    more libraries here there was one was
  • 3:07 - 3:11
    that was called
  • 3:08 - 3:12
    ushido that has been abandoned in favor
  • 3:11 - 3:14
    of weaponix so
  • 3:12 - 3:16
    right now it's pretty much robotics if
  • 3:14 - 3:20
    you want a core library
  • 3:16 - 3:22
    and then on top of this core library
  • 3:20 - 3:23
    there are wrapper libraries and there
  • 3:22 - 3:26
    are many of them
  • 3:23 - 3:29
    and today uh
  • 3:26 - 3:30
    all those libraries are using webonix
  • 3:29 - 3:32
    under the hood
  • 3:30 - 3:35
    okay it's a wrapper around webonix to
  • 3:32 - 3:38
    make it easier to use
  • 3:35 - 3:41
    so basically we have api platform and
  • 3:38 - 3:42
    the overblock graphql bundles that are
  • 3:41 - 3:44
    really
  • 3:42 - 3:46
    used with the symphony framework if
  • 3:44 - 3:48
    you're doing laravel you can use
  • 3:46 - 3:51
    a library called lighthouse i will be
  • 3:48 - 3:52
    presenting those a bit later
  • 3:51 - 3:54
    if you're doing um
  • 3:52 - 3:57
    [Music]
  • 3:54 - 3:59
    what uh if you're using a cms like
  • 3:57 - 4:02
    wordpress or drupal
  • 3:59 - 4:06
    wordpress and drupal have modules
  • 4:02 - 4:09
    to plug to present a graphql api
  • 4:06 - 4:11
    so if you want to do a headless cms it's
  • 4:09 - 4:14
    quite easy using
  • 4:11 - 4:17
    wordpress or drupal and adding a
  • 4:14 - 4:18
    a graphql module on them and then there
  • 4:17 - 4:19
    is graphqlite
  • 4:18 - 4:21
    which i've been altering for the last
  • 4:19 - 4:24
    two years and which is
  • 4:21 - 4:24
    a framework agnostic because you can use
  • 4:24 - 4:29
    it
  • 4:24 - 4:32
    whatever the framework you use
  • 4:29 - 4:33
    let's make a quick look uh a quick zoom
  • 4:32 - 4:36
    on
  • 4:33 - 4:38
    webonix okay so webonix it is the core
  • 4:36 - 4:41
    library here
  • 4:38 - 4:42
    and how does it look like coding with
  • 4:41 - 4:45
    webox well
  • 4:42 - 4:48
    here i've got a graphql type okay
  • 4:45 - 4:49
    and basically this type is a story it
  • 4:48 - 4:53
    has
  • 4:49 - 4:56
    fields with which all have types
  • 4:53 - 4:56
    building that in webmix the card would
  • 4:56 - 4:59
    look
  • 4:56 - 5:01
    like this okay it's kind of verbose
  • 4:59 - 5:03
    and as i told you the developer
  • 5:01 - 5:04
    experience is not that great but
  • 5:03 - 5:08
    basically
  • 5:04 - 5:12
    i'm creating a type it's its name is
  • 5:08 - 5:15
    history here and i've got a number of
  • 5:12 - 5:17
    fields that are declared here with the
  • 5:15 - 5:19
    type of the field the description
  • 5:17 - 5:21
    if i've got arguments the arguments are
  • 5:19 - 5:23
    declared here
  • 5:21 - 5:24
    and finally i have a resolver that will
  • 5:23 - 5:27
    be called
  • 5:24 - 5:27
    when i query this fill the resolver is
  • 5:27 - 5:30
    called
  • 5:27 - 5:34
    to fetch the data
  • 5:30 - 5:34
    this is really verbals right
  • 5:34 - 5:42
    and also it might look a bit like
  • 5:39 - 5:45
    some libraries some graphql libraries in
  • 5:42 - 5:46
    javascript especially if you have been
  • 5:45 - 5:50
    doing
  • 5:46 - 5:53
    been using express graphql
  • 5:50 - 5:56
    webonix started by mimicking a lot what
  • 5:53 - 5:59
    facebook was doing in its own javascript
  • 5:56 - 6:03
    library if i want to declare
  • 5:59 - 6:06
    a query well i'm creating a type query
  • 6:03 - 6:08
    whose fields are here it's actually
  • 6:06 - 6:08
    exactly the same thing as defining a
  • 6:08 - 6:11
    type
  • 6:08 - 6:11
    because in graphql a query is a field on
  • 6:11 - 6:16
    the
  • 6:11 - 6:18
    type whose name is query and finally
  • 6:16 - 6:20
    if i want to resolve a query i can
  • 6:18 - 6:21
    compose the exit with query static
  • 6:20 - 6:23
    method
  • 6:21 - 6:25
    plus in parameters the schema the schema
  • 6:23 - 6:27
    is
  • 6:25 - 6:28
    an object containing all the types that
  • 6:27 - 6:31
    i have declared
  • 6:28 - 6:33
    i'm passing in the query string so the
  • 6:31 - 6:34
    graphical query and it will return an
  • 6:33 - 6:38
    array that that
  • 6:34 - 6:39
    i can cast to json and return to the
  • 6:38 - 6:43
    client
  • 6:39 - 6:45
    so this is the the experience
  • 6:43 - 6:46
    you're getting when you're using webinx
  • 6:45 - 6:50
    directly
  • 6:46 - 6:50
    right and the problem with this approach
  • 6:50 - 6:54
    is that it
  • 6:50 - 6:58
    actually requires a lot of work
  • 6:54 - 7:01
    to set up a great graphql api
  • 6:58 - 7:02
    obviously there are games to use graphql
  • 7:01 - 7:05
    versus rest
  • 7:02 - 7:09
    its queries are client-driven
  • 7:05 - 7:10
    it has introspection you can see the
  • 7:09 - 7:13
    schema from the client
  • 7:10 - 7:13
    it has three types which is definitely
  • 7:13 - 7:16
    one of the
  • 7:13 - 7:17
    really cool features but if it is too
  • 7:16 - 7:21
    much of pain to
  • 7:17 - 7:24
    um to put the api in place
  • 7:21 - 7:27
    uh well uh it maybe won't be worth it
  • 7:24 - 7:29
    so what we are going to do is we are
  • 7:27 - 7:32
    going to use a wrapper library
  • 7:29 - 7:37
    which is easier to work with to
  • 7:32 - 7:40
    offset the gains versus the costs
  • 7:37 - 7:42
    and to make it worthwhile to use
  • 7:40 - 7:42
    graphical
  • 7:43 - 7:47
    now if we need to choose a proper
  • 7:45 - 7:50
    library
  • 7:47 - 7:54
    well there are basically two kinds of
  • 7:50 - 7:57
    such libraries there are
  • 7:54 - 7:58
    schema first libraries and code first
  • 7:57 - 8:01
    libraries
  • 7:58 - 8:03
    in the schema first dropper library
  • 8:01 - 8:05
    basically
  • 8:03 - 8:06
    your task as a developer will be to
  • 8:05 - 8:10
    write the
  • 8:06 - 8:10
    graphql schema and then the job of the
  • 8:10 - 8:14
    library
  • 8:10 - 8:17
    will be from the schema to build
  • 8:14 - 8:20
    to to find a way to link the schema
  • 8:17 - 8:22
    to the ph key code on the other end if
  • 8:20 - 8:25
    you are using a code first approach
  • 8:22 - 8:28
    you will be designing objects and then
  • 8:25 - 8:30
    the job of the library will be to
  • 8:28 - 8:32
    generate the schema
  • 8:30 - 8:34
    from the classes and the objects you've
  • 8:32 - 8:37
    been
  • 8:34 - 8:40
    designing so uh
  • 8:37 - 8:41
    and basically when you are using a
  • 8:40 - 8:46
    language
  • 8:41 - 8:48
    that is loosely typed you tend to have
  • 8:46 - 8:50
    libraries that are schema first if you
  • 8:48 - 8:52
    are doing javascript you will find a lot
  • 8:50 - 8:55
    of schemas libraries
  • 8:52 - 8:56
    on the other end if you are using a
  • 8:55 - 8:59
    strictly typed language
  • 8:56 - 9:01
    like java you will find a lot of code
  • 8:59 - 9:04
    first libraries
  • 9:01 - 9:04
    php sits a bit in the middle because it
  • 9:04 - 9:06
    is
  • 9:04 - 9:08
    optionally tight you can have types but
  • 9:06 - 9:09
    you're not forced to
  • 9:08 - 9:11
    and so you have the two kinds of
  • 9:09 - 9:14
    libraries out there and
  • 9:11 - 9:17
    uh basically uh in the schema first
  • 9:14 - 9:18
    part you have uh overblog lighthouse
  • 9:17 - 9:22
    reality sciler
  • 9:18 - 9:22
    and the code first category api platform
  • 9:22 - 9:26
    graphical light
  • 9:22 - 9:26
    and get bob
  • 9:27 - 9:31
    let's make a quick zoom on lighthouse
  • 9:30 - 9:34
    okay so lighthouse
  • 9:31 - 9:36
    is a schema first library let's see how
  • 9:34 - 9:41
    it works
  • 9:36 - 9:43
    lighthouse is built on top of laravel
  • 9:41 - 9:45
    when you are using lighthouse what we
  • 9:43 - 9:48
    you are going to do
  • 9:45 - 9:51
    is you are going to design this
  • 9:48 - 9:54
    graphql schema
  • 9:51 - 9:58
    and basically you are going to annotate
  • 9:54 - 9:59
    this schema with directives okay this is
  • 9:58 - 10:02
    a directive
  • 9:59 - 10:05
    this is another directive and
  • 10:02 - 10:07
    these directives will be used uh by
  • 10:05 - 10:10
    lighthouse
  • 10:07 - 10:12
    to bind this schema to the database
  • 10:10 - 10:15
    model directly
  • 10:12 - 10:17
    uh basically under the hood lighthouse
  • 10:15 - 10:20
    is using yellow points
  • 10:17 - 10:21
    and yellow point is the orm of laravel
  • 10:20 - 10:24
    so basically if i'm using the
  • 10:21 - 10:29
    old directive
  • 10:24 - 10:32
    automatically lighthouse will put
  • 10:29 - 10:33
    the current user in the mcquery you can
  • 10:32 - 10:35
    do
  • 10:33 - 10:37
    quite powerful stuff with only
  • 10:35 - 10:38
    directives for instance the create
  • 10:37 - 10:41
    directive here
  • 10:38 - 10:42
    will create directly an object in
  • 10:41 - 10:45
    database
  • 10:42 - 10:47
    when you when you call critpost and it
  • 10:45 - 10:50
    will put a post directly into the
  • 10:47 - 10:53
    database and you can do validation
  • 10:50 - 10:53
    right here on each field
  • 10:53 - 11:01
    okay so it's very efficient
  • 10:58 - 11:05
    i mean you can build a complete
  • 11:01 - 11:07
    graphql api in very few lines of code
  • 11:05 - 11:10
    with this approach
  • 11:07 - 11:14
    also it is very very tied to laravel
  • 11:10 - 11:14
    and to eloquent which is the oram so if
  • 11:14 - 11:17
    you want
  • 11:14 - 11:19
    to do advanced things and if you don't
  • 11:17 - 11:21
    want to
  • 11:19 - 11:22
    to be too much tied to your database you
  • 11:21 - 11:25
    will have to write
  • 11:22 - 11:26
    a custom directive which is a bit of a
  • 11:25 - 11:28
    pain
  • 11:26 - 11:30
    but if you have a simple use case and
  • 11:28 - 11:32
    you only want to
  • 11:30 - 11:34
    back to to do quick by binding to a
  • 11:32 - 11:36
    database it is a great tool
  • 11:34 - 11:39
    okay one thing that is also interesting
  • 11:36 - 11:44
    it has support for subscriptions
  • 11:39 - 11:47
    which is uh so you can do real-time
  • 11:44 - 11:49
    graph you graphql can
  • 11:47 - 11:52
    can answer in real time which is not
  • 11:49 - 11:55
    that common in php
  • 11:52 - 11:58
    so that is lighthouse let's now make
  • 11:55 - 12:01
    a zoom on api platform so api platform
  • 11:58 - 12:04
    okay it is a code first library
  • 12:01 - 12:05
    in api platform the philosophy is the
  • 12:04 - 12:08
    opposite
  • 12:05 - 12:12
    you start by writing a php class
  • 12:08 - 12:15
    and then you're going to put annotations
  • 12:12 - 12:16
    on the class an api platform will read
  • 12:15 - 12:19
    the annotation
  • 12:16 - 12:20
    and generate the graphical schema for
  • 12:19 - 12:23
    you
  • 12:20 - 12:26
    okay um api platform was
  • 12:23 - 12:27
    first built with the rest philosophy in
  • 12:26 - 12:29
    mind
  • 12:27 - 12:31
    and uh what is kind of fun with the api
  • 12:29 - 12:34
    platform is that you can expose
  • 12:31 - 12:36
    quite easily both a rest api and a
  • 12:34 - 12:38
    graphql api
  • 12:36 - 12:41
    very few lines of code so you get both
  • 12:38 - 12:41
    kind of api
  • 12:41 - 12:48
    support using the same code
  • 12:45 - 12:52
    which is cool however
  • 12:48 - 12:54
    if you want to have a good control on
  • 12:52 - 12:56
    you the graphical schema that is
  • 12:54 - 12:59
    generated api platform
  • 12:56 - 13:02
    may not be the best uh the best
  • 12:59 - 13:03
    tool out there because the underlying
  • 13:02 - 13:05
    philosophy
  • 13:03 - 13:07
    is very much uh rest so you have credit
  • 13:05 - 13:09
    you have you can create read update and
  • 13:07 - 13:13
    delete the report
  • 13:09 - 13:16
    if you want to do some some other things
  • 13:13 - 13:18
    uh well it will be a bit harder to
  • 13:16 - 13:21
    customize
  • 13:18 - 13:24
    it has support for subscriptions too so
  • 13:21 - 13:25
    yeah so that was the zoom on lighthouse
  • 13:24 - 13:27
    and
  • 13:25 - 13:28
    api platform there are many also
  • 13:27 - 13:32
    libraries out there
  • 13:28 - 13:34
    and i've been preparing a few schemas
  • 13:32 - 13:35
    to help you decide what library you can
  • 13:34 - 13:37
    use
  • 13:35 - 13:40
    by answering a few questions so let's
  • 13:37 - 13:40
    jump into that
  • 13:41 - 13:44
    so basically if you want to pick a
  • 13:43 - 13:46
    graphical library
  • 13:44 - 13:49
    i think the first que and if you are
  • 13:46 - 13:52
    doing php
  • 13:49 - 13:52
    the first question you should ask is are
  • 13:52 - 13:56
    you developing
  • 13:52 - 13:59
    an application or a tool okay
  • 13:56 - 14:03
    so basically um
  • 13:59 - 14:06
    if you are
  • 14:03 - 14:06
    doing uh if you are developing a tool if
  • 14:06 - 14:10
    your model
  • 14:06 - 14:14
    is dynamic for instance if uh
  • 14:10 - 14:17
    you're sorry
  • 14:14 - 14:18
    if you are developing a cms and the end
  • 14:17 - 14:20
    user
  • 14:18 - 14:21
    can create a new content type you can
  • 14:20 - 14:23
    create new type
  • 14:21 - 14:25
    add new fields stuff like that then you
  • 14:23 - 14:27
    want to go for webinars
  • 14:25 - 14:28
    okay you want to go low level and build
  • 14:27 - 14:30
    your own tool
  • 14:28 - 14:31
    on it if you are developing an
  • 14:30 - 14:32
    application and you have a set of
  • 14:31 - 14:35
    classes
  • 14:32 - 14:38
    already well defined then you want to go
  • 14:35 - 14:39
    for a wrapper library then there is a
  • 14:38 - 14:42
    question
  • 14:39 - 14:42
    do you need subscriptions if you want to
  • 14:42 - 14:46
    do things
  • 14:42 - 14:48
    in real time then you have to pick
  • 14:46 - 14:49
    a library that supports subscriptions
  • 14:48 - 14:51
    there are not many out there
  • 14:49 - 14:52
    it's lighthouse api platform silo of all
  • 14:51 - 14:56
    webinars
  • 14:52 - 14:58
    otherwise you can choose any library
  • 14:56 - 14:59
    then there is another question which is
  • 14:58 - 15:03
    do you want
  • 14:59 - 15:06
    do you want to do do you want to use
  • 15:03 - 15:09
    relay on your front end if you are
  • 15:06 - 15:12
    using relay a relay imposes
  • 15:09 - 15:14
    a number of constraints of the on the
  • 15:12 - 15:16
    schemas that are generated
  • 15:14 - 15:17
    and not all the libraries out there have
  • 15:16 - 15:21
    support for
  • 15:17 - 15:23
    this kind of schema so if you want to do
  • 15:21 - 15:25
    to use relay you should definitely aim
  • 15:23 - 15:29
    for those libraries
  • 15:25 - 15:31
    maybe lighthouse and graphical bundles
  • 15:29 - 15:32
    finally depending on the framework you
  • 15:31 - 15:35
    are using
  • 15:32 - 15:39
    you will also have to to put to pick
  • 15:35 - 15:42
    one of those
  • 15:39 - 15:45
    libraries some libraries
  • 15:42 - 15:47
    are framework agnostics like webinarics
  • 15:45 - 15:51
    and graphic lite and you can use them
  • 15:47 - 15:54
    everywhere alright so
  • 15:51 - 15:56
    finally the last question do you prefer
  • 15:54 - 15:59
    a schema first or
  • 15:56 - 16:02
    a code first approach if you want
  • 15:59 - 16:05
    to go schema first pick one here
  • 16:02 - 16:06
    if you want to go code first pick one
  • 16:05 - 16:09
    here
  • 16:06 - 16:12
    and when you unsword all those questions
  • 16:09 - 16:14
    if you are lucky well you still have a
  • 16:12 - 16:16
    choice between one or two libraries
  • 16:14 - 16:18
    if you are unlucky there are no
  • 16:16 - 16:20
    libraries uh
  • 16:18 - 16:22
    answering you need for instance if you
  • 16:20 - 16:27
    want uh to do
  • 16:22 - 16:30
    to use a code first approach in laravel
  • 16:27 - 16:33
    and using subscriptions well
  • 16:30 - 16:37
    you can't basically
  • 16:33 - 16:39
    and but yes this
  • 16:37 - 16:41
    approach should help you choose uh one
  • 16:39 - 16:44
    of those libraries
  • 16:41 - 16:48
    okay and uh i try to be
  • 16:44 - 16:52
    not too much biased but yes of course i
  • 16:48 - 16:55
    i altered a graphic light and so well
  • 16:52 - 16:56
    now we will enter the third phase of
  • 16:55 - 16:58
    this talk
  • 16:56 - 17:00
    and i will be doing a quick demo about
  • 16:58 - 17:03
    graphic light
  • 17:00 - 17:05
    before doing the demo i will try to
  • 17:03 - 17:08
    explain you the philosophy
  • 17:05 - 17:12
    that drove a graphical light basically
  • 17:08 - 17:16
    my id was uh well let's imagine
  • 17:12 - 17:19
    you want to do a simple echo query
  • 17:16 - 17:22
    in php okay
  • 17:19 - 17:26
    if you want to do such a query
  • 17:22 - 17:30
    in graphql it will look like this i'm
  • 17:26 - 17:34
    creating a query type and basically
  • 17:30 - 17:38
    yes there is a echo it takes a message
  • 17:34 - 17:41
    it will return um well i
  • 17:38 - 17:45
    i'm doing an echo to build that
  • 17:41 - 17:48
    using webonix i would need to put cisco
  • 17:45 - 17:48
    i already presented it i need to put an
  • 17:48 - 17:50
    echo field
  • 17:48 - 17:52
    which returns a string that takes into
  • 17:50 - 17:54
    argument a string
  • 17:52 - 17:57
    and and that results by reading the
  • 17:54 - 18:00
    arguments and returning them
  • 17:57 - 18:02
    but if i want to write the same echo in
  • 18:00 - 18:06
    pure php
  • 18:02 - 18:10
    basically it only takes four lines
  • 18:06 - 18:14
    basically the function name could map
  • 18:10 - 18:17
    the query name in graphql the parameters
  • 18:14 - 18:19
    here could
  • 18:17 - 18:21
    map graphical arguments and the return
  • 18:19 - 18:23
    type is already defined
  • 18:21 - 18:25
    and then the body of the function is
  • 18:23 - 18:28
    actually my resolver
  • 18:25 - 18:31
    so what would be really cool
  • 18:28 - 18:32
    would be what if i was able to simply
  • 18:31 - 18:35
    put
  • 18:32 - 18:39
    a query annotation here
  • 18:35 - 18:43
    and well this query annotation
  • 18:39 - 18:46
    uh would automatically
  • 18:43 - 18:46
    tell my library graphically like in my
  • 18:46 - 18:50
    case
  • 18:46 - 18:52
    that this function should be actually
  • 18:50 - 18:53
    a graphical query expose it in graphql
  • 18:52 - 18:56
    that's it
  • 18:53 - 18:59
    and so that's the philosophy behind
  • 18:56 - 19:02
    graphqlite using annotations
  • 18:59 - 19:06
    and using the fact that php
  • 19:02 - 19:09
    is already typed and
  • 19:06 - 19:10
    basically taking php types and mapping
  • 19:09 - 19:13
    them
  • 19:10 - 19:15
    automatically to graphical types so
  • 19:13 - 19:17
    this is graphic light graphicalite is a
  • 19:15 - 19:21
    mapper between php types
  • 19:17 - 19:24
    and graphql types and
  • 19:21 - 19:26
    what is kind of fun is that it actually
  • 19:24 - 19:29
    plays quite nice with doctrine
  • 19:26 - 19:32
    okay doctrine is the orm
  • 19:29 - 19:34
    used by a symphony
  • 19:32 - 19:37
    and basically in doctrine you put
  • 19:34 - 19:40
    annotations in your php objects
  • 19:37 - 19:41
    and doctrine is doing a mapping between
  • 19:40 - 19:44
    php objects
  • 19:41 - 19:45
    and database tables and from the php
  • 19:44 - 19:48
    objects
  • 19:45 - 19:49
    it can generate the database model and
  • 19:48 - 19:50
    with graphilite we are doing the same
  • 19:49 - 19:53
    thing
  • 19:50 - 19:57
    we are starting from the php object and
  • 19:53 - 19:57
    we are generating a graphql schema
  • 19:57 - 20:04
    yeah so let's go for the live coding
  • 20:02 - 20:06
    quick note i already told it is
  • 20:04 - 20:09
    framework agnostic
  • 20:06 - 20:12
    you can use it starting from php 7.2
  • 20:09 - 20:12
    and it's based on webonix
  • 20:13 - 20:21
    now for the hard part for me
  • 20:17 - 20:24
    the demo yeah so we are going
  • 20:21 - 20:27
    to uh make a quick demo with a
  • 20:24 - 20:30
    marketplace and basically my marketplace
  • 20:27 - 20:33
    has companies and in each company
  • 20:30 - 20:36
    can have several products and we will be
  • 20:33 - 20:38
    focusing on this relationship between
  • 20:36 - 20:41
    companies and products
  • 20:38 - 20:44
    in the last uh in the next five minutes
  • 20:41 - 20:48
    right so
  • 20:44 - 20:53
    let's jump to the code here
  • 20:48 - 20:56
    i already did set up a
  • 20:53 - 20:58
    graphql a graphical light in
  • 20:56 - 20:59
    a symphony project the database is
  • 20:58 - 21:04
    already loaded
  • 20:59 - 21:07
    and i wrote a company controller okay so
  • 21:04 - 21:11
    it's actually a controller like like
  • 21:07 - 21:14
    like you can find some in nbc frameworks
  • 21:11 - 21:14
    except it is in the graphql controller
  • 21:14 - 21:17
    namespace
  • 21:14 - 21:18
    okay and if you want to put queries or
  • 21:17 - 21:20
    mutations
  • 21:18 - 21:21
    somewhere in your code you will put them
  • 21:20 - 21:24
    in a controller
  • 21:21 - 21:25
    so my controller is fed with a company
  • 21:24 - 21:27
    repository
  • 21:25 - 21:29
    the company repository is the object
  • 21:27 - 21:33
    that will access the database
  • 21:29 - 21:35
    okay and i have this
  • 21:33 - 21:37
    method which is called get companies
  • 21:35 - 21:39
    that takes
  • 21:37 - 21:41
    a search string and that will query the
  • 21:39 - 21:42
    database for the string and return the
  • 21:41 - 21:45
    results
  • 21:42 - 21:46
    what i'm going to do is i'm going to put
  • 21:45 - 21:50
    an annotation
  • 21:46 - 21:53
    here and
  • 21:50 - 21:55
    i'm going to put a query annotation to
  • 21:53 - 21:58
    tell graphic life okay
  • 21:55 - 22:01
    take this company's
  • 21:58 - 22:04
    [Music]
  • 22:01 - 22:06
    function and expose it in the graphql
  • 22:04 - 22:09
    schema
  • 22:06 - 22:10
    if i go in firefox here i've got a
  • 22:09 - 22:13
    graphical
  • 22:10 - 22:14
    test client setup so i'm going to
  • 22:13 - 22:18
    refresh
  • 22:14 - 22:21
    the page and oops
  • 22:18 - 22:24
    okay i'm getting an error let's make
  • 22:21 - 22:28
    a quick zoom on this error and basically
  • 22:24 - 22:32
    what graphicality is telling me is that
  • 22:28 - 22:36
    in the get companies method a type hint
  • 22:32 - 22:39
    is missing okay you remember
  • 22:36 - 22:42
    that php is optionally typed but graphql
  • 22:39 - 22:44
    must have types so when i'm using
  • 22:42 - 22:48
    graphqlite i must put type
  • 22:44 - 22:51
    into all my functions uh
  • 22:48 - 22:54
    all my queries here i did not define
  • 22:51 - 22:54
    any return type so i need to define one
  • 22:54 - 22:58
    so
  • 22:54 - 23:00
    i'm going to say company get companies
  • 22:58 - 23:01
    is returning an array of companies now i
  • 23:00 - 23:04
    have a problem
  • 23:01 - 23:07
    it's that php does not have the notion
  • 23:04 - 23:08
    of generics so i cannot say something
  • 23:07 - 23:12
    like okay it's an array of company
  • 23:08 - 23:13
    this is not valid php right so what i'm
  • 23:12 - 23:16
    gonna do
  • 23:13 - 23:19
    is i'm going to put an uh
  • 23:16 - 23:23
    a code a command in the php doc and
  • 23:19 - 23:23
    in the php dock i can put anything and
  • 23:23 - 23:27
    especially
  • 23:23 - 23:29
    i will give a type hint to uh
  • 23:27 - 23:31
    to graph the light and tell him that
  • 23:29 - 23:36
    this array is actually
  • 23:31 - 23:40
    an array of companies so let's go back
  • 23:36 - 23:43
    to firefox refresh the page
  • 23:40 - 23:47
    and oh i'm getting another error
  • 23:43 - 23:49
    okay let's zoom on this lever
  • 23:47 - 23:50
    it's error driven development i'm a big
  • 23:49 - 23:55
    fan of ever
  • 23:50 - 23:59
    driven development it works really well
  • 23:55 - 24:01
    no i and i actually tried to
  • 23:59 - 24:03
    make sure that all the error messages
  • 24:01 - 24:06
    are quite clear
  • 24:03 - 24:10
    it's really important and well
  • 24:06 - 24:12
    in your own code try uh to pay attention
  • 24:10 - 24:14
    on the other messages you are generating
  • 24:12 - 24:17
    you are doing
  • 24:14 - 24:20
    your future self favor uh yeah
  • 24:17 - 24:21
    so basically it's telling me that in get
  • 24:20 - 24:24
    companies
  • 24:21 - 24:26
    it cannot map the class company to a
  • 24:24 - 24:28
    known required type
  • 24:26 - 24:31
    what graduality is actually tell me
  • 24:28 - 24:35
    telling me is that i did not tell
  • 24:31 - 24:38
    him explicitly that company
  • 24:35 - 24:41
    is a graphical type and so to do that
  • 24:38 - 24:43
    i simply add an annotation which is the
  • 24:41 - 24:46
    type annotation
  • 24:43 - 24:48
    and the type annotation is
  • 24:46 - 24:50
    telling graphical life okay map this
  • 24:48 - 24:53
    class to a tie
  • 24:50 - 24:54
    of course a type must have some face so
  • 24:53 - 24:58
    i'm going to
  • 24:54 - 25:01
    put annotations in the in the gators
  • 24:58 - 25:04
    here and i'm going to say okay expose
  • 25:01 - 25:08
    id as a fill and name
  • 25:04 - 25:10
    as a field and maybe website
  • 25:08 - 25:11
    as a field too also notice that i'm
  • 25:10 - 25:14
    putting
  • 25:11 - 25:16
    the field annotation on getters i'm not
  • 25:14 - 25:19
    putting the annotation
  • 25:16 - 25:24
    on the properties the properties are are
  • 25:19 - 25:24
    private so i cannot access them directly
  • 25:24 - 25:27
    doctrine has annotation on properties
  • 25:27 - 25:30
    because
  • 25:27 - 25:30
    it is doing some black magic and
  • 25:30 - 25:33
    actually
  • 25:30 - 25:35
    accessing directly private properties
  • 25:33 - 25:38
    i'm not doing that in graphic light
  • 25:35 - 25:40
    okay so yes i must put the fill
  • 25:38 - 25:44
    annotation on the getters
  • 25:40 - 25:47
    so let's go back to
  • 25:44 - 25:50
    graphical i'm refreshing the page
  • 25:47 - 25:53
    i don't have any error so it looks good
  • 25:50 - 25:57
    and if i'm looking at writing a query
  • 25:53 - 26:00
    i can my company pool will just appear
  • 25:57 - 26:03
    and yeah i can do a search on any
  • 26:00 - 26:05
    companies containing the a
  • 26:03 - 26:06
    and i'm going to display the id and the
  • 26:05 - 26:10
    name
  • 26:06 - 26:11
    place the request and it is working and
  • 26:10 - 26:15
    if i'm
  • 26:11 - 26:19
    adding the website right
  • 26:15 - 26:21
    good so basically you can see that i
  • 26:19 - 26:24
    managed to create
  • 26:21 - 26:25
    a graphql api by basically adding three
  • 26:24 - 26:29
    annotations
  • 26:25 - 26:32
    query type and fill which is pretty cool
  • 26:29 - 26:36
    um and yeah
  • 26:32 - 26:38
    let's so far i only fetched
  • 26:36 - 26:40
    fields from companies so let's do some
  • 26:38 - 26:43
    binding with a product
  • 26:40 - 26:48
    so i'm going to go in the get products
  • 26:43 - 26:51
    getter and i'm gonna add a field on it
  • 26:48 - 26:53
    i'm gonna refresh the page
  • 26:51 - 26:55
    and i'm getting another evolve if i'm
  • 26:53 - 26:57
    zooming on this error
  • 26:55 - 26:58
    it is telling me that it cannot map the
  • 26:57 - 27:02
    product class
  • 26:58 - 27:03
    to a known graphical type okay we
  • 27:02 - 27:06
    already saw
  • 27:03 - 27:08
    this error two minutes ago so i'm pretty
  • 27:06 - 27:10
    sure you know what i should do i should
  • 27:08 - 27:13
    go in the product class
  • 27:10 - 27:14
    and instruct graphicalize that the
  • 27:13 - 27:17
    product
  • 27:14 - 27:20
    class is also a tie
  • 27:17 - 27:22
    and so i'm putting a type here and i'm
  • 27:20 - 27:22
    going to
  • 27:22 - 27:30
    put a field annotation
  • 27:26 - 27:33
    on the id on the name
  • 27:30 - 27:35
    on the price on the margin of the
  • 27:33 - 27:39
    product and
  • 27:35 - 27:39
    if i'm refreshing the page
  • 27:39 - 27:43
    yeah everything seems to be going great
  • 27:41 - 27:47
    and i can access the products
  • 27:43 - 27:49
    and access the id and the name of the
  • 27:47 - 27:52
    product
  • 27:49 - 27:56
    and it's working yeah
  • 27:52 - 27:59
    so that's it and basically
  • 27:56 - 28:02
    my job when i set up a graphql api
  • 27:59 - 28:03
    becomes adding a few annotations and
  • 28:02 - 28:05
    yeah
  • 28:03 - 28:05
    that's it
  • 28:06 - 28:13
    so yeah for the live demo
  • 28:10 - 28:16
    i won't be going any further i will be
  • 28:13 - 28:16
    presenting you very quickly a few
  • 28:16 - 28:20
    additional
  • 28:16 - 28:22
    features for instance if you want to put
  • 28:20 - 28:24
    to create a mutation you have a mutation
  • 28:22 - 28:26
    annotation
  • 28:24 - 28:27
    that can be used to create a mutation it
  • 28:26 - 28:31
    works exactly the same as
  • 28:27 - 28:33
    the query annotation works
  • 28:31 - 28:35
    there is a brilliant support for
  • 28:33 - 28:37
    authentication and authorization
  • 28:35 - 28:38
    that is plugged into your favorite
  • 28:37 - 28:42
    framework
  • 28:38 - 28:43
    laravel symphony whatever but basically
  • 28:42 - 28:48
    if you are using the right annotation
  • 28:43 - 28:51
    and passing the name of a write
  • 28:48 - 28:54
    well then this query will be
  • 28:51 - 28:56
    visible only if the user that is
  • 28:54 - 28:58
    currently logged
  • 28:56 - 29:00
    has this right you can also use the
  • 28:58 - 29:02
    right annotation and feels directly
  • 29:00 - 29:05
    you can even do fine-grained
  • 29:02 - 29:08
    authorization
  • 29:05 - 29:10
    if you want to say that some objects can
  • 29:08 - 29:14
    be seen but not others
  • 29:10 - 29:17
    in this example i've got an email field
  • 29:14 - 29:20
    from a user and basically i am saying
  • 29:17 - 29:20
    that i am looking at the component of
  • 29:20 - 29:24
    the
  • 29:20 - 29:27
    the user of the current user
  • 29:24 - 29:28
    and comparing it with the company of the
  • 29:27 - 29:31
    user that is logged
  • 29:28 - 29:32
    user here refers to the user that is
  • 29:31 - 29:35
    currently loaded
  • 29:32 - 29:37
    and basically this is saying i can see
  • 29:35 - 29:40
    the email of the people that are
  • 29:37 - 29:41
    in the same company as me i can see the
  • 29:40 - 29:43
    email of my colleagues
  • 29:41 - 29:45
    but i cannot see the email of someone
  • 29:43 - 29:48
    that is
  • 29:45 - 29:48
    not my colleague so with this security
  • 29:48 - 29:51
    annotation
  • 29:48 - 29:54
    we can have something that is going
  • 29:51 - 29:57
    quite deep
  • 29:54 - 30:00
    to do fine-grained authorization
  • 29:57 - 30:01
    another feature i like a lot is auto
  • 30:00 - 30:05
    wiring
  • 30:01 - 30:09
    uh basically here we are in a domain
  • 30:05 - 30:12
    object a product and uh
  • 30:09 - 30:13
    let's admit i want to compute the v80 of
  • 30:12 - 30:15
    a product
  • 30:13 - 30:16
    computing the v80 of a product can be
  • 30:15 - 30:20
    quite complicated
  • 30:16 - 30:23
    maybe i need to know uh the type of the
  • 30:20 - 30:27
    product the country the
  • 30:23 - 30:30
    uh the user is in so it can be difficult
  • 30:27 - 30:31
    and we typically won't do it in the
  • 30:30 - 30:34
    domain object but rather
  • 30:31 - 30:36
    rather we will call a service but you
  • 30:34 - 30:39
    when you are in a domain object
  • 30:36 - 30:42
    accessing services accessing services is
  • 30:39 - 30:46
    difficult and what we can do here is
  • 30:42 - 30:47
    auto wire called the auto wire ring and
  • 30:46 - 30:48
    basically graphic light will
  • 30:47 - 30:51
    automatically
  • 30:48 - 30:52
    inject when i'm calling the v80 field it
  • 30:51 - 30:55
    will automatically inject
  • 30:52 - 30:56
    from the dependency injection container
  • 30:55 - 30:59
    the service
  • 30:56 - 31:01
    the v80 service and i can use it
  • 30:59 - 31:05
    directly in the resolver here
  • 31:01 - 31:07
    so it enables me to access services and
  • 31:05 - 31:08
    from a domain object by passing those
  • 31:07 - 31:11
    services
  • 31:08 - 31:12
    in parameters of the resolver which is
  • 31:11 - 31:15
    kind of
  • 31:12 - 31:17
    kind of cool yeah there are many many
  • 31:15 - 31:20
    more features
  • 31:17 - 31:21
    available uh we have support for
  • 31:20 - 31:24
    validation
  • 31:21 - 31:24
    uh for uh indium types even if they
  • 31:24 - 31:28
    don't
  • 31:24 - 31:30
    exist in php file uploads um
  • 31:28 - 31:32
    what i invite you to do if you are
  • 31:30 - 31:35
    interested
  • 31:32 - 31:38
    in trying out graphqlite is
  • 31:35 - 31:41
    having a look at the documentation
  • 31:38 - 31:45
    everything is i hope clearly documented
  • 31:41 - 31:45
    and if you have any issue well you can
  • 31:45 - 31:49
    always
  • 31:45 - 31:52
    contact me either on
  • 31:49 - 31:56
    twitter directly or if you prefer
  • 31:52 - 31:59
    uh just open an issue
  • 31:56 - 32:02
    on the graphqlite repository yeah
  • 31:59 - 32:05
    so yeah that's repetitive let's wrap uh
  • 32:02 - 32:08
    this um this talk uh
  • 32:05 - 32:09
    basically uh doing graphql in php is
  • 32:08 - 32:13
    definitely possible
  • 32:09 - 32:13
    you have many possibilities out there uh
  • 32:13 - 32:17
    and this is
  • 32:13 - 32:20
    since graphql is pretty cool uh
  • 32:17 - 32:21
    well i definitely invite you to to try
  • 32:20 - 32:24
    it out
  • 32:21 - 32:26
    and uh that's it do you have any
  • 32:24 - 32:26
    questions
  • 32:27 - 32:30
    thanks so much david it was such
  • 32:29 - 32:33
    incredible talk
  • 32:30 - 32:36
    i really enjoyed it all the life current
  • 32:33 - 32:36
    day we were scared
  • 32:36 - 32:39
    yeah everything was incredible so that's
  • 32:38 - 32:43
    great uh so yeah
  • 32:39 - 32:46
    do yeah we have a question yes
  • 32:43 - 32:50
    uh so florian he's asking about
  • 32:46 - 32:53
    if graph graphic graph live
  • 32:50 - 32:55
    is is gonna support or is currently
  • 32:53 - 32:57
    supporting subscriptions
  • 32:55 - 33:00
    it is not currently supporting
  • 32:57 - 33:03
    subscriptions
  • 33:00 - 33:07
    i plan to add support for subscriptions
  • 33:03 - 33:10
    in the future maybe
  • 33:07 - 33:11
    not in the in the two coming months
  • 33:10 - 33:14
    because i want to
  • 33:11 - 33:17
    to release a version 4.1 of graphqlite
  • 33:14 - 33:18
    and i'm no subscriptions won't be part
  • 33:17 - 33:23
    of this release
  • 33:18 - 33:25
    but yes i would like to to work on that
  • 33:23 - 33:26
    for the end of the year definitely
  • 33:25 - 33:28
    definitely well that
  • 33:26 - 33:29
    that's that sounds super exciting i
  • 33:28 - 33:32
    guess and
  • 33:29 - 33:33
    i think i have another question because
  • 33:32 - 33:35
    i found
  • 33:33 - 33:37
    out super super interesting so i'd like
  • 33:35 - 33:40
    to understand like
  • 33:37 - 33:44
    like why do you why did you decide to
  • 33:40 - 33:46
    go and build this incredible technology
  • 33:44 - 33:48
    and what are kind of like the future
  • 33:46 - 33:52
    plans for for it
  • 33:48 - 33:55
    oh okay uh um
  • 33:52 - 33:56
    well when i started uh writing
  • 33:55 - 34:00
    graphically light it was
  • 33:56 - 34:03
    two years ago and basically you had only
  • 34:00 - 34:05
    two options at the time it was using
  • 34:03 - 34:06
    webonix directly
  • 34:05 - 34:09
    or uh using um
  • 34:06 - 34:11
    [Music]
  • 34:09 - 34:12
    using ushido which is a library that has
  • 34:11 - 34:14
    been abandoned
  • 34:12 - 34:18
    and basically they were really low level
  • 34:14 - 34:18
    libraries and they were hard to use
  • 34:18 - 34:22
    and i have this huge project for a
  • 34:20 - 34:24
    client of mine
  • 34:22 - 34:27
    uh we are just at the coding machine we
  • 34:24 - 34:31
    are doing mostly internet and extranet
  • 34:27 - 34:35
    applications and a client of mine
  • 34:31 - 34:37
    uh had uh this huge rest api
  • 34:35 - 34:39
    with a lot of end points and maybe read
  • 34:37 - 34:42
    on the end points
  • 34:39 - 34:42
    i wanted to do graphql because i knew
  • 34:42 - 34:45
    that
  • 34:42 - 34:46
    the flexibility of graph create would be
  • 34:45 - 34:49
    really really beneficial
  • 34:46 - 34:51
    for that client and uh the only option i
  • 34:49 - 34:55
    had was webonix which was
  • 34:51 - 34:58
    too hard to use uh and so i decided
  • 34:55 - 35:01
    i started writing a small library which
  • 34:58 - 35:04
    became a bit more complex
  • 35:01 - 35:06
    and uh i did two three versions of the
  • 35:04 - 35:09
    library and then i
  • 35:06 - 35:09
    i told myself okay uh i i must work on
  • 35:09 - 35:13
    it
  • 35:09 - 35:15
    really seriously put it on um
  • 35:13 - 35:16
    open source it and well that's how
  • 35:15 - 35:19
    graphic light
  • 35:16 - 35:20
    was born actually well that sounds that
  • 35:19 - 35:22
    sounds
  • 35:20 - 35:23
    so great and i think like thanks so much
  • 35:22 - 35:24
    for
  • 35:23 - 35:26
    contributing with the graphql community
  • 35:24 - 35:28
    in this case in the php because
  • 35:26 - 35:30
    yeah i think there are not so many
  • 35:28 - 35:33
    people that either they don't know that
  • 35:30 - 35:35
    they are able to do these or they
  • 35:33 - 35:36
    figure out they like realize that it's a
  • 35:35 - 35:38
    bit more difficult so it's even like a
  • 35:36 - 35:42
    start you know like to contribute so
  • 35:38 - 35:45
    i i i think like like like this is super
  • 35:42 - 35:47
    super super interested and i think like
  • 35:45 - 35:48
    a if the people they do know this
  • 35:47 - 35:49
    library they are going to start to use
  • 35:48 - 35:50
    it
  • 35:49 - 35:52
    they are going to like it and they are
  • 35:50 - 35:54
    going to like adopting their company so
  • 35:52 - 35:56
    um yeah thanks so much for sharing this
  • 35:54 - 35:59
    knowledge with us
  • 35:56 - 35:59
    you're welcome yeah
  • 35:59 - 36:02
    yeah thanks so much for like like like
  • 36:01 - 36:04
    like contacting us to
  • 36:02 - 36:05
    speak uh because it's it's been
  • 36:04 - 36:07
    difficult to find
  • 36:05 - 36:08
    kind of like people that they have
  • 36:07 - 36:10
    companies in hong kong although you are
  • 36:08 - 36:11
    not in hong kong right now but i
  • 36:10 - 36:13
    you're like really connected to the hong
  • 36:11 - 36:16
    kong community so
  • 36:13 - 36:17
    i am so so glad to have you here and
  • 36:16 - 36:20
    thanks so much and
  • 36:17 - 36:23
    i really really hope to see you soon
  • 36:20 - 36:27
    thank you very much okay david thanks
  • 36:23 - 36:27
    so up next so
Title:
Building a GraphQL API in PHP with GraphQLite - GraphQL Hong Kong
Description:

more » « less
Duration:
36:26

English subtitles

Revisions