How to add a schema in my Schema.graphql and then pass resolvers. #1602
Unanswered
wajidakhterabbasi
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I just need some help as I am working on a project where I have to write my type, query and mutation file in the schema.graphql. How can I stitch them all together ? I am trying to write a controller and the structure looks like this
`<?php
declare(strict_types=1);
namespace Api\GraphQL\Controller;
use GraphQL\Language\Parser;
use GraphQL\Utils\BuildSchema;
use Psr\Container\ContainerInterface;
final class GraphQLControllerFactory
<?php
{
private const SCHEMA_FILE_PATH = "/Schema/Schema.graphql";
public function __invoke(ContainerInterface $container): GraphQLController
{
$schemaContent = file_get_contents(
dirname(DIR) . self::SCHEMA_FILE_PATH
);
return new GraphQLController(
BuildSchema::build(Parser::parse($schemaContent))
);
}
}
declare(strict_types=1);
namespace Api\GraphQL\Controller;
use Exception;
use GraphQL\GraphQL;
use GraphQL\Language\Parser;
use GraphQL\Type\Schema;
use Laminas\View\Model\JsonModel;
use Api\Shared\Controller\CommonController;
class GraphQLController extends CommonController
{
public function __construct(private readonly Schema $schema)
{
}
}
My Schema.graphql file is like this
type Query {
user(id: ID!): User
posts: [Post]
}
type User {
id: ID!
name: String!
email: String!
}
type Post {
id: ID!
title: String!
content: String!
author: User!
}
The error that I am getting is when I am trying to query my end point reads as
"message": "An error occurred; GraphQL\GraphQL::executeQuery(): Argument #7 ($fieldResolver) must be of type ?callable, array given, called in /src/GraphQL/Controller/GraphQLController.php on line 27",
The query is
query GetUserAndPosts {user(id: 1) {
id
name
email
}
posts {
id
title
content
author {
id
name
}
}
}`
Any help will be much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions