Skip to content

Merge schema.graphql files #1534

Description

@feeh27

Description

I'm trying to use multiple schema.graphql as is in issue #500, but I got these errors:

Type "Mutation" already exists in the schema. It cannot also be defined in this type definition.
Type "Query" already exists in the schema. It cannot also be defined in this type definition.

I'm using the following code:

use GraphQL\Language\Parser;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Schema;
use GraphQL\Utils\SchemaExtender;

function buildSchema() {
    $schema = new Schema(config: [
        'query' => new ObjectType(config: ['name' => 'Query', 'fields' => []]),
        'mutation' => new ObjectType(config: ['name' => 'Mutation', 'fields' => []]),
    ]);

    $schemaFiles = [
        __DIR__ . '/' . '../GraphQl/schema.graphql',
        __DIR__ . '/' . '../../Domains/Authors/schema.graphql',
        __DIR__ . '/' . '../../Domains/Books/schema.graphql',
    ];

    $sources = array_map(callback: 'file_get_contents', array: $schemaFiles);
    $extensionSource = implode(separator: "\n", array: $sources);

    $ast = Parser::parse(source: $extensionSource);

    return SchemaExtender::extend(schema: $schema, documentAST: $ast);
}

Version

PHP 8.3.2
webonyx/graphql-php 15.9.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions