Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get user context inside afterFetch callback or filter GraphQL output? #4509

Closed
n8tb1t opened this issue Nov 12, 2019 · 1 comment
Closed

Comments

@n8tb1t
Copy link

n8tb1t commented Nov 12, 2019

Hi I need to filter graphQL results on server side and replace some object values with default parameters based on current user and some DB settings.

The problem is that is that if I try to filter the result array in a controller, the relational fields queried by GRAPHQL using some inner queries.

I can do what I need in afterFetch callback, but I NEED an access to user context, is there a way to get it?

{
  "data": {
    "users": [
      {
        "email": "user1@localhost.localhost",
        "turtle_wallets": [
          {
            "owner": {
              "email": "user1@localhost.localhost"
            },
            "ins": [],
            "id": "5dc96922a9370b24cc914618",
            "address": "turtle_walllet_one"
          }
        ],
        "articles": [
          {
            "author": {
              "email": "user1@localhost.localhost"
            }
          }
        ]
      },
      {
        "email": "user2@localhost.localhost",
        "turtle_wallets": [
          {
            "owner": {
              "email": "user2@localhost.localhost"
            },
            "ins": [
              {
                "from": {
                  "owner": {
                    "email": "user1@localhost.localhost"
                  }
                }
              },
              {
                "from": {
                  "owner": {
                    "email": "user1@localhost.localhost"
                  }
                }
              }
            ],
            "id": "5dc96938a9370b24cc914619",
            "address": "turtle_wallet_two"
          }
        ],
        "articles": [
          {
            "author": {
              "email": "user2@localhost.localhost"
            }
          }
        ]
      }
    ]
  }
}

my object look like this, I can sanitize it and replace the email with custom value in User.js controller, but graphQL will resolve all the relational objects data by some inner method and display the initial values.
System

  • Node.js version: v10
  • NPM version: v6
  • Strapi version: v3.0.0-beta.17.5
  • Database: MongoDB
  • Operating system: macOS
@lauriejim
Copy link
Contributor

Hello! For this kind of need, I suggest you use a custom Query that manages your data as you want.
Here is the documentation for that https://strapi.io/documentation/3.0.0-beta.x/plugins/graphql.html#customise-the-graphql-schema

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

No branches or pull requests

2 participants