Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Query nested array

default discord avatar
dnl.krmr2 years ago

So a part of my user object looks like this:



{
      name: 'sites',
      label: 'Seitenrechte',
      saveToJWT: true,
      required: false,
      type: 'array',
      access: {
        // Only admins can create or update a value for this field
        create: isAdminFieldLevel,
        update: isAdminFieldLevel,
      },
      admin: {
        condition: ({ isSystemAdmin }) => !isSystemAdmin,
        description: 'This field sets which sites that this user has access to.',
      },
      fields: [ // required
        {
          name: 'siteId',
          type: 'relationship',
          relationTo: 'sites',
          required: true,
          access: {
            // Only admins can create or update a value for this field
            create: isAdminFieldLevel,
            update: isAdminFieldLevel,
          },
        },
        {
          name: 'role',
          // Save this field to JWT so we can use from `req.user`
          saveToJWT: true,
          type: 'select',
          hasMany: false,
          defaultValue: 'editor',
          access: {
            // Only admins can create or update a value for this field
            create: isAdminFieldLevel,
            update: isAdminFieldLevel,
          },
          required: true,
          options: [
            {
              label: 'Admin',
              value: 'admin',
            },
            {
              label: 'Editor',
              value: 'editor',
            },
            {
              label: 'API',
              value: 'api',
            },
          ],
        },
      ],
    },


As my user sites relationship is an object not an array I don't think that the demo example works for me, but I didn't tried it yet



While trying to understand the query from the access-control-demo, I tried writing a graphql query and encountered a problem



query {
  Users(where:
        {
        sites__site: { in: ["SITEID"] },
        sites__role: {equals: admin}
      }) {
    docs {
      email,
      firstName,
      sites {site{title}}
    }
    totalDocs
  }
}


I need to know if the user has a configuration for the given SITEID AND has role admin for this page. But how do I query this?



ok. figured out that this won't work 😄

    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get dedicated engineering support directly from the Payload team.