youshido-php/GraphQLBundle

The document seems not right...

yarcowang opened this issue · 13 comments

By following the doc written in README.md

I should get {"errors":[{"message":"You have to set GraphQL Schema to process"}]} this error, but I got:

[
  {
    "message": "Schema class AppBundle\\GraphQL\\Schema does not exist"
  }
]

( I don't the array as the root level result... )

After I generate by php bin/console graphql:configure AppBundle and also added the config. When visit localhost:8000/graphql?query={hello}, I got:

{
  "data": {
    "hello": null
  },
  "errors": [
    {
      "message": "Notice: Undefined index: name"
    }
  ]
}

Just first touch graphQL...

Hi @yarcowang , it seems like you forgot to add routing for the default GraphQL Controller to your app.

it's from the readme:

Add the routing reference to the app/config/routing.yml:

graphql:
    resource: "@GraphQLBundle/Controller/"

@viniychuk I've already added that...

$ cat app/config/routing.yml 
app:
    resource: '@AppBundle/Controller/'
    type: annotation

graphql:
    resource: "@GraphQLBundle/Controller/"

@yarcowang your default controller intercept the routing – that's the default response from the Symofony's autogenerated stuff... Also check if you have bundle enabled and so on... Just try to do everything step by step again

bjunc commented

I got the same responses. Documentation seems to be out of date.

Being unfamiliar with GraphQL, I actually came to this repo first (not the Youshido/GraphQL repo). After some head scratching, I realized the sentence "this bundle provides you with" is actually referring to the main GraphQL repo. It'd be great if there were Symfony-specific examples. It took me a little while rummaging through the GraphQLController and the main GraphQL repo examples to pull together a quick Postman example using an application/graphql request.

Are you open to pull requests for examples?

Hi !

I've got the same problem :/
The problem comes from the build action, into the the new Schema class generated :

    public function build(SchemaConfig $config)
    {
        $config->getQuery()->addFields([
            'hello' => [
                'type'    => new StringType(),
                'args'    => [
                    'name' => [
                        'type' => new StringType(),
                        'default' => 'Stranger'
                    ]
                ],
                'resolve' => function ($context, $args) {
                    return 'Hello ' . $args['name'];
                }
            ]
        ]);
    }

The $args argument in the resolve function seems to be an empty array :/

Any Idea ?

I would like to test GraphqlBundle before to make a choice between this one and FOSRestBundle.

Btw, I'm on Symfony 3.3.2

Okay men sorry for the earlier post ( @bjunc I undertood what you meant but telling the solution would have been better :P)

@yarcowang here is the right syntax of the http query :

http://127.0.0.1:8000/graphql?query={hello(name:"john")}

@viniychuk As mentioned bjunc, that would be a great idea to implement simple examples of how GraphQL works with symfony for those who are not familiar with it (like me ^^).

Sorry for the double post and hope that would help !

bjunc commented

@bastos71 after some poking at it, I ultimately abandoned the "hello world" example. When I started reading through the extensive Youshido/GraphQL repo docs, I was able to create a schema from scratch, and make calls through Postman and the graphql explorer. I would be happy to create some basic examples and do a pull request.

Also, I created a javascript (axios) gist for my own notes, just to show the difference between sending a request through application/json and application/graphql. While not specific to Symfony (or even PHP), I think some broader explanation of GraphQL would help people adopt and evolve this bundle.

Here's the gist, if you're interested: application/graphql vs application/json

I even started creating a REST wrapper around GraphQL. Basically, I lifted some of the logic in the GraphQLController (Youshido\GraphQLBundle\Controller), and exposed CRUD methods that run GraphQL queries (and mutations!). For me, this was a pretty helpful learning experience.

/**
 * @Route("/api/v1/users/{accountId}")
 * @Method("GET")
 */

public function getUserAction($accountId, Request $request)
{
    $query = "{ user(id: $accountId) { 
        id first_name last_name mail full_name phone dob
        address { thoroughfare city state postal_code } 
    } }";
    $responseData = $this->processGraphQL($query);
    return new JsonResponse($responseData);
}

The processGraphQL() method is the custom method I spoke about that lifts some logic from the GraphQLController. From there, the schema resolve methods essentially act as a serializer for your entities (I'm not using the Symfony Serializer or JMS). You can pretty easily add a voter system as well, for top level permissions, or even nested fields. There is some (albeit vague) reference to this in the docs, but I think it needs further explanation / examples. I could definitely see how that could hurt performance.

Deserializing is a little more complicated, and I am still working through how I think it is "best" done. I created a custom denormalizer to convert the GraphQL input back into entities that can be saved.

Personally, I think the PHP and Symfony community are going to need real-world examples before they can feel comfortable really getting into this. The Node.js community seems to be much further along in this regard.

Seems pretty cool !
I'm still learning how to work with Graphql and it seems to be kind of magic ^^

I do not think I've gone further enough in Graphql to understand all your work in it's globality, but for sure I agree with you : it has such potential to build an easy-to-use (and easy-to-build) API 👍

But I think we've gone too far for this issue ^^

@bastos71 @bjunc @yarcowang The problem is in dependencies. Updating it and will make sure we have the up to date example. Sorry guys.

all fixed in v1.3.4

@bastos71
Thanks for the syntax. Yes, it could work. But for the schema does have the definition of the default value 'default' => 'Stranger', I suppose it can output Hello Stranger.

@bjunc
I don't think you are on the right way of thinking GraphQL. In my mind, GraphQL = REST + Serializer, or a higher level thinking of getting data.
It does not have any sense to envelope the GraphQL to REST. REST is for getting one main entities and it's related things (like topics and comments); GraphQL is to get several entities even they have no relationships but only for performance. (Not quite sure whether it is right nowadays, cause there is HTTP2. But for mobile client end, maybe such performance is still required for there's the limitation of creating a lot of sockets -- not quite sure.)

bjunc commented

@yarcowang I think you're missing the point of the exercise. It was two fold:

  1. Get familiarity with GraphQL within the context of the familiarity I have with REST.
  2. Since it is likely that we would continue to offer a REST API, the ability to wrap a REST endpoint around a GraphQL query/mutation would mean easier maintenance of two concurrent APIs.

I'm not arguing that GraphQL can do more than REST, and that the thought process is a bit different. It'd be silly to do all the GraphQL work to only offer a REST endpoint. But if you're offering a GraphQL endpoint, you can also offer a REST endpoint using simplified queries so you're not forcing your integrators to use GraphQL before they're ready.

@bjunc Yes, that sounds reasonable. But what I'm thinking is to envelope the REST API as GraphQL (just like some integration gateway or serializer), that would save a lot of time on migration from current RESTful projects to GraphQL. (And I think there may already exist such project, or at least in javascript ). Maybe PHP is not ready for this.