1

I am having some issues with querying an array of Enums in a GraphQL query. I am expecting that the array or permissions will be returned with the user as per the type

Bizarrely, when I make the same query in the playground (for either my Prisma or Apollo-Server servers) I do get back the array.

My query looks like this:

const user = await ctx.db.query.user({
  where: {
    id: ctx.userId
  }
});

My type definition looks like this:

type User {
    id: ID! @id
    name: String!
    email: String! @unique
    password: String!
    club: String!
    permissions: [Permission!]! @scalarList(strategy: RELATION)
    createdAt: DateTime! @createdAt
    updatedAt: DateTime! @updatedAt
}

permissions looking like

enum Permission {
    ADMIN
    CLUB_ADMIN
    USER
    FRIEND
}

I haven’t included my query resolver as I have just forwarded that to the DB using “forward-to”

But the CL of user is

{ id: 'cjxm9ohfqvkvh0b5963iy734i',
  name: 'BERTIE BOBBINS',
  email: 'BERTIE@DOGS.COM',
  password: '$2b$10$eLPoBuuenogLabiFb4tRFu0KV7LI4LxARhHecPYVbP0qnt5VvcZ3W',
  club: 'Dog Club',
  createdAt: '2019-07-02T20:30:49.670Z',
  updatedAt: '2019-07-02T20:30:49.670Z' }

So not including the Permissions array.

Andrew1325
  • 3,519
  • 1
  • 14
  • 25
Matthew Player
  • 318
  • 4
  • 17
  • If it works in playground it works, so it must be your client-side query that is the issue. Why does a user have an array of permissions? Wouldn't one user have one permission type? – Andrew1325 Jul 03 '19 at 08:09
  • Each permission relates to a different set of permissions, could be a user and also a club_admin, admin, etc. I haven’t got onto the client side query yet. This is just querying the prisma db and Apollo server db and the query is directly taken from the generated schema. I think that when a record is created with an array of enums you have toy use a “set”, do I need to explicitly “get” the contents of the array and if so, how, and how come the playground works? – Matthew Player Jul 03 '19 at 08:21
  • Ran into this same issue. I haven't found a fix for it, but if you want the permissions field to be returned, you need to pass it in as a return field, and also pass in the `info` object as the second argument to the `ctx.db.query.user` function – Temi Lajumoke Dec 19 '19 at 01:44

2 Answers2

0

Introspection system this introspection types

  1. __Schema
  2. __Type
  3. __TypeKind
  4. __Field
  5. __InputValue
  6. __EnumValue
  7. __Directive

To query the enums you need to use the __Type query resolver.

enum Permission {
    ADMIN
    CLUB_ADMIN
    USER
    FRIEND
}

query for enum

query 
{
 __type(name: "Permission") {
   name
   enumValues {
     name
   }
 }
}

The __type query resolver requires the name parameter. This parameter allows you to search your schema for items such as objects and enums based on their name.

{
 "data": {
   "__type": {
   "name": "Permission", 
   "enumValues" : [
     {
       name: "ADMIN" 
     },
     { 
       name: "CLUB_ADMIN" 
     },
     { 
       name: "USER" 
     },
     { 
       name: "FRIEND"
     }
   ]
  }
 }
}

This returns the name of the enum with all of its values. To use these values, store them in a variable when querying the enums.

PermissionsList = data.__type.enumValues

If you need enum inside user object ref this

If you use apollo server then read apollo-server and graphql-tools

Ashok
  • 2,846
  • 1
  • 12
  • 20
  • Is this to get the array of permissions for a specific user or to introspect what the possible values of the enum are? – Matthew Player Jul 03 '19 at 20:48
  • For now, you can return enum. If you need to permissions for a specific user you can return an array. Please check your Query resolver and return [array of string](https://stackoverflow.com/questions/55018992/how-to-iterate-json-nested-array-in-graphql-scheme/55019511#55019511). – Ashok Jul 04 '19 at 04:56
0

I don't quite understand: you mention you simply used the prisma-binding forwardTo function, but then provide your sample query where you forward to the db from the context using ctx.db.query.user. Did you have the issue in both cases?

For the forwardTo instance, I do not know the answer. But for the second case, I also had this issue, and was able to resolve it by passing in the info object from the resolver function signature as the second argument to the query.

const user = await ctx.db.query.user(
  {
    where: {
      id: ctx.request.userId
    }
  },
  info
);
Trace
  • 1