Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Graphql Php Laravel Package

pixelandtonic/graphql-php

PHP implementation of the GraphQL specification (based on graphql-js). Build schemas, execute queries, and add custom types, fields, and resolvers. Install via Composer and explore full docs and ready-to-run examples.

View on GitHub
Deep Wiki
Context7

Type System

To start using GraphQL you are expected to implement a type hierarchy and expose it as Schema.

In graphql-php type is an instance of internal class from GraphQL\Type\Definition namespace: ObjectType, InterfaceType, UnionType, InputObjectType, ScalarType, EnumType (or one of subclasses).

But most of the types in your schema will be object types.

Type Definition Styles

Several styles of type definitions are supported depending on your preferences.

Inline definitions:

<?php
namespace MyApp;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

$myType = new ObjectType([
    'name' => 'MyType',
    'fields' => [
        'id' => Type::id()
    ]
]);

Class per type:

<?php
namespace MyApp;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

class MyType extends ObjectType
{
    public function __construct()
    {
        $config = [
            // Note: 'name' is not needed in this form:
            // it will be inferred from class name by omitting namespace and dropping "Type" suffix
            'fields' => [
                'id' => Type::id()
            ]
        ];
        parent::__construct($config);
    }
}

Using GraphQL Type language:

schema {
    query: Query
    mutation: Mutation
}

type Query {
    greetings(input: HelloInput!): String!
}

input HelloInput {
    firstName: String!
    lastName: String
}

Read more about type language definitions in a dedicated docs section.

Type Registry

Every type must be presented in Schema by a single instance (graphql-php throws when it discovers several instances with the same name in the schema).

Therefore if you define your type as separate PHP class you must ensure that only one instance of that class is added to the schema.

The typical way to do this is to create a registry of your types:

<?php
namespace MyApp;

class TypeRegistry
{
    private $myAType;
    private $myBType;
    
    public function myAType()
    {
        return $this->myAType ?: ($this->myAType = new MyAType($this));
    }
    
    public function myBType()
    {
        return $this->myBType ?: ($this->myBType = new MyBType($this));
    }
}

And use this registry in type definition:

<?php
namespace MyApp;
use GraphQL\Type\Definition\ObjectType;

class MyAType extends ObjectType
{
    public function __construct(TypeRegistry $types) 
    {
        parent::__construct([
            'fields' => [
                'b' => $types->myBType()                
            ]
        ]);
    }
}

Obviously, you can automate this registry as you wish to reduce boilerplate or even introduce Dependency Injection Container if your types have other dependencies.

Alternatively, all methods of the registry could be static - then there is no need to pass it in constructor - instead just use use TypeRegistry::myAType() in your type definitions.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/graphviz
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata