{"id":13396232,"url":"https://github.com/folkloreinc/laravel-graphql","last_synced_at":"2025-03-13T22:31:55.427Z","repository":{"id":36360172,"uuid":"40664963","full_name":"folkloreinc/laravel-graphql","owner":"folkloreinc","description":"Facebook GraphQL for Laravel 5. It supports Relay, eloquent models, validation and GraphiQL.","archived":true,"fork":false,"pushed_at":"2019-02-10T07:09:32.000Z","size":539,"stargazers_count":1767,"open_issues_count":123,"forks_count":233,"subscribers_count":59,"default_branch":"develop","last_synced_at":"2024-06-25T05:06:04.573Z","etag":null,"topics":["graphql","laravel","php","relay"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/folkloreinc.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-13T15:02:04.000Z","updated_at":"2024-06-24T05:41:09.000Z","dependencies_parsed_at":"2022-09-11T04:30:39.188Z","dependency_job_id":null,"html_url":"https://github.com/folkloreinc/laravel-graphql","commit_stats":null,"previous_names":["folkloreatelier/laravel-graphql"],"tags_count":65,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folkloreinc%2Flaravel-graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folkloreinc%2Flaravel-graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folkloreinc%2Flaravel-graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folkloreinc%2Flaravel-graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/folkloreinc","download_url":"https://codeload.github.com/folkloreinc/laravel-graphql/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221417353,"owners_count":16816867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","laravel","php","relay"],"created_at":"2024-07-30T18:00:42.784Z","updated_at":"2024-10-25T10:31:54.006Z","avatar_url":"https://github.com/folkloreinc.png","language":"PHP","funding_links":[],"categories":["Popular Packages","Libraries","PHP"],"sub_categories":["PHP Libraries"],"readme":"# Laravel GraphQL\n\n----\n\n### This package is no longuer maintained. Please use [rebing/graphql-laravel](https://github.com/rebing/graphql-laravel) or [other Laravel GraphQL packages](https://github.com/search?q=laravel+graphql\u0026type=Repositories)\n\n----\n\nUse Facebook GraphQL with Laravel 5 or Lumen. It is based on the PHP implementation [here](https://github.com/webonyx/graphql-php). You can find more information about GraphQL in the [GraphQL Introduction](http://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html) on the [React](http://facebook.github.io/react) blog or you can read the [GraphQL specifications](https://facebook.github.io/graphql/). This is a work in progress.\n\nThis package is compatible with Eloquent model (or any other data source). See the example below.\n\n[![Latest Stable Version](https://poser.pugx.org/folklore/graphql/v/stable.svg)](https://packagist.org/packages/folklore/graphql)\n[![Build Status](https://travis-ci.org/Folkloreatelier/laravel-graphql.png?branch=master)](https://travis-ci.org/Folkloreatelier/laravel-graphql)\n[![Total Downloads](https://poser.pugx.org/folklore/graphql/downloads.svg)](https://packagist.org/packages/folklore/graphql)\n\n----\n### To use laravel-graphql with Relay, check the [feature/relay](https://github.com/Folkloreatelier/laravel-graphql/tree/feature/relay) branch.\n----\n\n## Installation\n\nVersion 1.0 is released. If you are upgrading from older version, you can check [Upgrade to 1.0](docs/upgrade.md).\n\n#### Dependencies:\n\n* [Laravel 5.x](https://github.com/laravel/laravel) or [Lumen](https://github.com/laravel/lumen)\n* [GraphQL PHP](https://github.com/webonyx/graphql-php)\n\n\n**1-** Require the package via Composer in your `composer.json`.\n```json\n{\n  \"require\": {\n    \"folklore/graphql\": \"~1.0.0\"\n  }\n}\n```\n\n**2-** Run Composer to install or update the new requirement.\n\n```bash\n$ composer install\n```\n\nor\n\n```bash\n$ composer update\n```\n\n### Laravel \u003e= 5.5.x\n\n**1-** Publish the configuration file\n\n```bash\n$ php artisan vendor:publish --provider=\"Folklore\\GraphQL\\ServiceProvider\"\n```\n\n**2-** Review the configuration file\n\n```\nconfig/graphql.php\n```\n\n### Laravel \u003c= 5.4.x\n\n**1-** Add the service provider to your `config/app.php` file\n```php\nFolklore\\GraphQL\\ServiceProvider::class,\n```\n\n**2-** Add the facade to your `config/app.php` file\n```php\n'GraphQL' =\u003e Folklore\\GraphQL\\Support\\Facades\\GraphQL::class,\n```\n\n**3-** Publish the configuration file\n\n```bash\n$ php artisan vendor:publish --provider=\"Folklore\\GraphQL\\ServiceProvider\"\n```\n\n**4-** Review the configuration file\n\n```\nconfig/graphql.php\n```\n\n### Lumen\n\n**1-** Load the service provider in `bootstrap/app.php`\n```php\n$app-\u003eregister(Folklore\\GraphQL\\LumenServiceProvider::class);\n```\n\n**2-** For using the facade you have to uncomment the line `$app-\u003ewithFacades();` in `bootstrap/app.php`\n\nAfter uncommenting this line you have the `GraphQL` facade enabled\n\n```php\n$app-\u003ewithFacades();\n```\n\n**3-** Publish the configuration file\n\n```bash\n$ php artisan graphql:publish\n```\n\n**4-** Load configuration file in `bootstrap/app.php`\n\n*Important*: this command needs to be executed before the registration of the service provider\n\n```php\n$app-\u003econfigure('graphql');\n...\n$app-\u003eregister(Folklore\\GraphQL\\LumenServiceProvider::class)\n```\n\n**5-** Review the configuration file\n\n```\nconfig/graphql.php\n```\n\n## Documentation\n\n- [Upgrade to 1.0](docs/upgrade.md)\n\n## Usage\n\n- [Schemas](#schemas)\n- [Creating a query](#creating-a-query)\n- [Creating a mutation](#creating-a-mutation)\n- [Adding validation to mutation](#adding-validation-to-mutation)\n\n#### Advanced Usage\n- [Query variables](docs/advanced.md#query-variables)\n- [Query nested resource](docs/advanced.md#query-nested-resource)\n- [Enums](docs/advanced.md#enums)\n- [Interfaces](docs/advanced.md#interfaces)\n- [Custom field](docs/advanced.md#custom-field)\n- [Eager loading relationships](docs/advanced.md#eager-loading-relationships)\n\n### Schemas\nStarting from version 1.0, you can define multiple schemas. Having multiple schemas can be useful if, for example, you want an endpoint that is public and another one that needs authentication.\n\nYou can define multiple schemas in the config:\n\n```php\n'schema' =\u003e 'default',\n\n'schemas' =\u003e [\n    'default' =\u003e [\n        'query' =\u003e [\n            //'users' =\u003e 'App\\GraphQL\\Query\\UsersQuery'\n        ],\n        'mutation' =\u003e [\n            //'updateUserEmail' =\u003e 'App\\GraphQL\\Query\\UpdateUserEmailMutation'\n        ]\n    ],\n    'secret' =\u003e [\n        'query' =\u003e [\n            //'users' =\u003e 'App\\GraphQL\\Query\\UsersQuery'\n        ],\n        'mutation' =\u003e [\n            //'updateUserEmail' =\u003e 'App\\GraphQL\\Query\\UpdateUserEmailMutation'\n        ]\n    ]\n]\n```\n\nOr you can add schema using the facade:\n\n```php\nGraphQL::addSchema('secret', [\n    'query' =\u003e [\n        'users' =\u003e 'App\\GraphQL\\Query\\UsersQuery'\n    ],\n    'mutation' =\u003e [\n        'updateUserEmail' =\u003e 'App\\GraphQL\\Query\\UpdateUserEmailMutation'\n    ]\n]);\n```\n\nAfterwards, you can build the schema using the facade:\n\n```php\n// Will return the default schema defined by 'schema' in the config\n$schema = GraphQL::schema();\n\n// Will return the 'secret' schema\n$schema = GraphQL::schema('secret');\n\n// Will build a new schema\n$schema = GraphQL::schema([\n    'query' =\u003e [\n        //'users' =\u003e 'App\\GraphQL\\Query\\UsersQuery'\n    ],\n    'mutation' =\u003e [\n        //'updateUserEmail' =\u003e 'App\\GraphQL\\Query\\UpdateUserEmailMutation'\n    ]\n]);\n```\n\nOr you can request the endpoint for a specific schema\n\n```\n// Default schema\nhttp://homestead.app/graphql?query=query+FetchUsers{users{id,email}}\n\n// Secret schema\nhttp://homestead.app/graphql/secret?query=query+FetchUsers{users{id,email}}\n```\n\n### Creating a query\n\nFirst you need to create a type.\n\n```php\nnamespace App\\GraphQL\\Type;\n\nuse GraphQL\\Type\\Definition\\Type;\nuse Folklore\\GraphQL\\Support\\Type as GraphQLType;\n\nclass UserType extends GraphQLType\n{\n    protected $attributes = [\n        'name' =\u003e 'User',\n        'description' =\u003e 'A user'\n    ];\n\n    /*\n    * Uncomment following line to make the type input object.\n    * http://graphql.org/learn/schema/#input-types\n    */\n    // protected $inputObject = true;\n\n    public function fields()\n    {\n        return [\n            'id' =\u003e [\n                'type' =\u003e Type::nonNull(Type::string()),\n                'description' =\u003e 'The id of the user'\n            ],\n            'email' =\u003e [\n                'type' =\u003e Type::string(),\n                'description' =\u003e 'The email of user'\n            ]\n        ];\n    }\n\n    // If you want to resolve the field yourself, you can declare a method\n    // with the following format resolve[FIELD_NAME]Field()\n    protected function resolveEmailField($root, $args)\n    {\n        return strtolower($root-\u003eemail);\n    }\n}\n```\n\nAdd the type to the `config/graphql.php` configuration file\n\n```php\n'types' =\u003e [\n    'User' =\u003e 'App\\GraphQL\\Type\\UserType'\n]\n```\n\nYou could also add the type with the `GraphQL` Facade, in a service provider for example.\n\n```php\nGraphQL::addType('App\\GraphQL\\Type\\UserType', 'User');\n```\n\nThen you need to define a query that returns this type (or a list). You can also specify arguments that you can use in the resolve method.\n```php\nnamespace App\\GraphQL\\Query;\n\nuse GraphQL;\nuse GraphQL\\Type\\Definition\\Type;\nuse Folklore\\GraphQL\\Support\\Query;\nuse App\\User;\n\nclass UsersQuery extends Query\n{\n    protected $attributes = [\n        'name' =\u003e 'users'\n    ];\n\n    public function type()\n    {\n        return Type::listOf(GraphQL::type('User'));\n    }\n\n    public function args()\n    {\n        return [\n            'id' =\u003e ['name' =\u003e 'id', 'type' =\u003e Type::string()],\n            'email' =\u003e ['name' =\u003e 'email', 'type' =\u003e Type::string()]\n        ];\n    }\n\n    public function resolve($root, $args)\n    {\n        if (isset($args['id'])) {\n            return User::where('id' , $args['id'])-\u003eget();\n        } else if(isset($args['email'])) {\n            return User::where('email', $args['email'])-\u003eget();\n        } else {\n            return User::all();\n        }\n    }\n}\n```\n\nAdd the query to the `config/graphql.php` configuration file\n\n```php\n'schemas' =\u003e [\n    'default' =\u003e [\n        'query' =\u003e [\n            'users' =\u003e 'App\\GraphQL\\Query\\UsersQuery'\n        ],\n        // ...\n    ]\n]\n```\n\nAnd that's it. You should be able to query GraphQL with a request to the url `/graphql` (or anything you choose in your config). Try a GET request with the following `query` input\n\n```\nquery FetchUsers {\n  users {\n    id\n    email\n  }\n}\n```\n\nFor example, if you use homestead:\n```\nhttp://homestead.app/graphql?query=query+FetchUsers{users{id,email}}\n```\n\n### Creating a mutation\n\nA mutation is like any other query, it accepts arguments (which will be used to do the mutation) and return an object of a certain type.\n\nFor example a mutation to update the password of a user. First you need to define the Mutation.\n\n```php\nnamespace App\\GraphQL\\Mutation;\n\nuse GraphQL;\nuse GraphQL\\Type\\Definition\\Type;\nuse Folklore\\GraphQL\\Support\\Mutation;\nuse App\\User;\n\nclass UpdateUserPasswordMutation extends Mutation\n{\n    protected $attributes = [\n        'name' =\u003e 'updateUserPassword'\n    ];\n\n    public function type()\n    {\n        return GraphQL::type('User');\n    }\n\n    public function args()\n    {\n        return [\n            'id' =\u003e ['name' =\u003e 'id', 'type' =\u003e Type::nonNull(Type::string())],\n            'password' =\u003e ['name' =\u003e 'password', 'type' =\u003e Type::nonNull(Type::string())]\n        ];\n    }\n\n    public function resolve($root, $args)\n    {\n        $user = User::find($args['id']);\n\n        if (!$user) {\n            return null;\n        }\n\n        $user-\u003epassword = bcrypt($args['password']);\n        $user-\u003esave();\n\n        return $user;\n    }\n}\n```\n\nAs you can see in the `resolve` method, you use the arguments to update your model and return it.\n\nYou then add the mutation to the `config/graphql.php` configuration file\n\n```php\n'schema' =\u003e [\n    'default' =\u003e [\n        'mutation' =\u003e [\n            'updateUserPassword' =\u003e 'App\\GraphQL\\Mutation\\UpdateUserPasswordMutation'\n        ],\n        // ...\n    ]\n]\n```\n\nYou should then be able to use the following query on your endpoint to do the mutation.\n\n```\nmutation users {\n  updateUserPassword(id: \"1\", password: \"newpassword\") {\n    id\n    email\n  }\n}\n```\n\nif you use homestead:\n```\nhttp://homestead.app/graphql?query=mutation+users{updateUserPassword(id: \"1\", password: \"newpassword\"){id,email}}\n```\n\n#### Adding validation to mutation\n\nIt is possible to add validation rules to mutation. It uses the laravel `Validator` to performs validation against the `args`.\n\nWhen creating a mutation, you can add a method to define the validation rules that apply by doing the following:\n\n```php\nnamespace App\\GraphQL\\Mutation;\n\nuse GraphQL;\nuse GraphQL\\Type\\Definition\\Type;\nuse Folklore\\GraphQL\\Support\\Mutation;\nuse App\\User;\n\nclass UpdateUserEmailMutation extends Mutation\n{\n    protected $attributes = [\n        'name' =\u003e 'UpdateUserEmail'\n    ];\n\n    public function type()\n    {\n        return GraphQL::type('User');\n    }\n\n    public function args()\n    {\n        return [\n            'id' =\u003e ['name' =\u003e 'id', 'type' =\u003e Type::string()],\n            'email' =\u003e ['name' =\u003e 'email', 'type' =\u003e Type::string()]\n        ];\n    }\n\n    public function rules()\n    {\n        return [\n            'id' =\u003e ['required'],\n            'email' =\u003e ['required', 'email']\n        ];\n    }\n\n    public function resolve($root, $args)\n    {\n        $user = User::find($args['id']);\n\n        if (!$user) {\n            return null;\n        }\n\n        $user-\u003eemail = $args['email'];\n        $user-\u003esave();\n\n        return $user;\n    }\n}\n```\n\nAlternatively you can define rules with each args\n\n```php\nclass UpdateUserEmailMutation extends Mutation\n{\n    //...\n\n    public function args()\n    {\n        return [\n            'id' =\u003e [\n                'name' =\u003e 'id',\n                'type' =\u003e Type::string(),\n                'rules' =\u003e ['required']\n            ],\n            'email' =\u003e [\n                'name' =\u003e 'email',\n                'type' =\u003e Type::string(),\n                'rules' =\u003e ['required', 'email']\n            ]\n        ];\n    }\n\n    //...\n}\n```\n\nWhen you execute a mutation, it will returns the validation errors. Since GraphQL specifications define a certain format for errors, the validation errors messages are added to the error object as a extra `validation` attribute. To find the validation error, you should check for the error with a `message` equals to `'validation'`, then the `validation` attribute will contain the normal errors messages returned by the Laravel Validator.\n\n```json\n{\n  \"data\": {\n    \"updateUserEmail\": null\n  },\n  \"errors\": [\n    {\n      \"message\": \"validation\",\n      \"locations\": [\n        {\n          \"line\": 1,\n          \"column\": 20\n        }\n      ],\n      \"validation\": {\n        \"email\": [\n          \"The email is invalid.\"\n        ]\n      }\n    }\n  ]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffolkloreinc%2Flaravel-graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffolkloreinc%2Flaravel-graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffolkloreinc%2Flaravel-graphql/lists"}