Hey guys, dumb question... given this type in my s...
# random
a
Hey guys, dumb question... given this type in my schema:
Copy code
type User {
  id: ID!
  oid: String!
  role: Role!
  roleName: String!
}
and this code:
Copy code
const attachRoleName = user => ({
  ...user,
  roleName: user.role.name,
});

const Query = {
  ...
  async currentUser(parent, args, context, info) {
    const { request } = context;
    const user = await context.db.query.user({ where: { oid: request.user.oid } }, info);
    return attachRoleName(user);
  },
  ...
};
I can't seem to request
roleName
from the clientside without requesting
role { name }
as well–which makes sense since I'm just passing
info
straight through–so how would I add the equiv of
role { name }
to info if I detect that they are requesting
roleName
?
b
is there a reason to have the roleName on the User type? It seems a bit non-standard IMHO - I think if you want the name of the user’s role in your frontend, just query the relation explicitly - eg. your query would be:``` type User { id role { name } }```
if you’re absolutely sure you want a field like roleName, I would have a specific resolver for that field, and not try and modify the query info as it passes:
Copy code
const Query = {
	…
	User: {
		roleName:  async (user, context, info) => {
			const role = await context.db.query.role({ where: { id: user.roleId })
			return role.name
		}
	}
}
ooh - or you could use fragments, like this example: https://github.com/prismagraphql/graphql-server-example/blob/master/src/resolvers/Home.ts#L17 - doesn't seem to be documented though, just found it myself
a
I'm developing the api side and there's someone else working on the frontend and I was trying to make him happy by adding a shortcut way to get the roleName.. Trying to solve the issue made me realize that I have no idea how to expand the query though so I'm mostly just trying to figure out what the convention is there if you wanna pull more of the data than the frontend requests