I am trying to pass the correct format for GraphQL. I can get this to work through Postman without issues:
{
tblprojects
{
id
name
}
}
I am lost on how to apply operators, filters, etc. When I try this it gives an error:
{
tblprojects (name:"Test")
{
id
name
}
}
"message": "Unknown argument \"name\" on field \"tblprojects\" of type \"Query\".",
This is the backend:
$queryFields = [];
foreach ($tables as $table) {
$queryFields[$table] = [
'type' => Type::listOf(new ObjectType([
'name' => ucfirst($table),
'fields' => function() use ($table) {
return $this->getFieldsFromTable($table);
},
])),
'args' => [
'id_in' => [
'type' => Type::listOf(Type::int()), // Allow filtering by an array of integers
],
],
'resolve' => function($root, $args) use ($table) {
if (isset($args['id_in']) && !empty($args['id_in'])) {
// Apply filtering if 'id_in' argument is passed
$this->db->where_in('id', $args['id_in']);
}
return $this->db->get($table)->result();
},
];
}
It looks like I need to apply filtering if the id_in
argument is passed
How do I do this? Can you give me a few examples of applying operators and filtering etc so I can try in Postman?
I am trying to pass the correct format for GraphQL. I can get this to work through Postman without issues:
{
tblprojects
{
id
name
}
}
I am lost on how to apply operators, filters, etc. When I try this it gives an error:
{
tblprojects (name:"Test")
{
id
name
}
}
"message": "Unknown argument \"name\" on field \"tblprojects\" of type \"Query\".",
This is the backend:
$queryFields = [];
foreach ($tables as $table) {
$queryFields[$table] = [
'type' => Type::listOf(new ObjectType([
'name' => ucfirst($table),
'fields' => function() use ($table) {
return $this->getFieldsFromTable($table);
},
])),
'args' => [
'id_in' => [
'type' => Type::listOf(Type::int()), // Allow filtering by an array of integers
],
],
'resolve' => function($root, $args) use ($table) {
if (isset($args['id_in']) && !empty($args['id_in'])) {
// Apply filtering if 'id_in' argument is passed
$this->db->where_in('id', $args['id_in']);
}
return $this->db->get($table)->result();
},
];
}
It looks like I need to apply filtering if the id_in
argument is passed
How do I do this? Can you give me a few examples of applying operators and filtering etc so I can try in Postman?
Share Improve this question edited Jan 20 at 13:06 lmtaq 5204 silver badges23 bronze badges asked Jan 17 at 18:20 Mr CastrovinciMr Castrovinci 12711 bronze badges 1- Where does that backend code come from? Do you try to implement a GraphQL resolver on your own? Usually, that query looks ok to me, and the code you've shared does not throw the exception you've shared – Nico Haase Commented Jan 20 at 13:10
1 Answer
Reset to default 0There is some confusion there regarding the returned fields and the query arguments.
Your PHP code shows that the only arg it takes is id_in
, which corresponds to a list of integer IDs.
Therefore, with the current implementation, it's impossible to filter the results by name, as you're trying to do in your query in Postman.
Without any changes on the BE, you have 2 possibilities for that query:
- Without passing any params (as you just did in your first example)
- Passing a list of IDs
query {
tblprojects (id_in:[1, 2, 3]) {
id
name
}
}
If you need to apply other filters, the args definition and the resolver will require additional adjustments on your PHP implementation.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349218a4623725.html
评论列表(0条)