简体   繁体   English

无法在 GraphQL 中查询字段(平台 API,Symfony)

[英]Cannot query field in GraphQL (Platform API, Symfony)

I am working with Symfony and Platform API to implement a GraphQL API.我正在使用 Symfony 和 Platform API 来实现 GraphQL API。 My goal is to expose different fields, depending on whether the user is logged in. The email should only be displayed if the user is logged in and is the user.我的目标是根据用户是否登录来公开不同的字段。只有当用户登录并且是用户时才应显示电子邮件。 Therefore I added the group user:read:self.因此我添加了组 user:read:self。

/**
 * @ORM\Column(type="string", length=180, unique=true)
 * @Groups({"user:read:self"})
 */
private $email;

Then I implemented a normalizer which adds the group to the context if the user is logged in:然后我实现了一个规范化器,如果用户登录,它会将组添加到上下文中:

public function normalize($object, string $format = null, array $context = [])
{
    echo "test";

    // checks whether the currently logged in user is
    // this user

    $isCurrentUser = $this->currentUser() == $object;

    if ($isCurrentUser) {
        $context['groups'][] = 'user:read:self';
    }

    $context[self::ALREADY_CALLED] = true;

    return $this->normalizer->normalize($object, $format, $context);
}

The normalizer is in fact getting called for the JSON API and GraphQL API.规范化器实际上是为 JSON API 和 GraphQL API 调用的。 This works in the JSON API:这适用于 JSON API:

{
  "@context": "/api/contexts/User",
  "@id": "/api/users",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/api/users/1",
      "@type": "User",
      "email": "test@example.com",
      "animals": [],
      "friends": []
    },
    {
      "@id": "/api/users/2",
      "@type": "User",
      "animals": [],
      "friends": []
    }
  ],
  "hydra:totalItems": 2
}

I am logged in as user with id 1 and I can see the email.我以 ID 为 1 的用户身份登录,我可以看到电子邮件。 I can't see the email of user 2.我看不到用户 2 的电子邮件。

Then in GraphQLi I query the following and an error, even though I am logged in as user 1. I checked that the normalizer is getting called for the GraphQL query.然后在 GraphQLi 中,即使我以用户 1 身份登录,我也会查询以下内容并出现错误。我检查了 GraphQL 查询是否正在调用规范化器。 The normalizer works and adds the group, however it appears that GraphQL does not read the context correctly:规范器工作并添加组,但是 GraphQL 似乎没有正确读取上下文:

{
  user(id: "/api/users/1") {
    email
  }
}

...
"message": "Cannot query field \"email\" on type \"User\".",
...

The short answer: You have to add a getter.简短的回答:你必须添加一个吸气剂。

The long answer: from Symfonycast (excellent course by the way) There are actually a bunch of different "normalizer" classes that help with this job - like one that's really good at converting DateTime objects to a string and back.长答案:来自Symfonycast (顺便说一句,很棒的课程)实际上有一堆不同的“规范化器”类可以帮助完成这项工作——就像一个非常擅长将 DateTime 对象转换为字符串并返回的类。 But the main class - the one at the heart of this process - is called the ObjectNormalizer.但是主类——这个过程的核心——被称为 ObjectNormalizer。 Behind the scenes, it uses another Symfony component called PropertyAccess, which has one superpower: if you give it a property name, like title, it's really good at finding and using getter and setter methods to access that property.在幕后,它使用另一个名为 PropertyAccess 的 Symfony 组件,它有一个超能力:如果你给它一个属性名称,比如标题,它真的很擅长查找和使用 getter 和 setter 方法来访问该属性。

In other words, when API platform tries to "normalize" an object into an array, it uses the getter and setter methods to do that!换句话说,当 API 平台尝试将对象“规范化”为数组时,它会使用 getter 和 setter 方法来做到这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM