hi! is it possible to have a field named different...
# orm-help
l
hi! is it possible to have a field named differently in the Prisma schema and in my own graphql server schema. E.g. I’ve got
uniqueId
in my data model but I’d like it to be
id
in my schema. I feel like the solution is easy but I can’t crack it! Thanks!
j
Sure you can. When receiving the data, i.e. await the return, deconstruct the user, rename the field, and spread the rest.
Copy code
const user = async (root, { userId }, { db }, info) => {
  const { uniqueId: id, ...rest } = await db.query.user(
    {
      where: {
        uniqueId: userId,
      },
    },
    info,
  );

  return {
    id,
    ...rest,
  };
};
should maybe work? I'd still suggest sticking to the same naming-convention. For simplicity. This is not as easy when querying multiple of them.
l
thanks for your answer! In your example
uniqueId
wouldn’t exist since it would not be in the user query (the schema has
id
and not
uniqueId
).
j
True, of course. I'm not sure how you would solve it then.
l
no worries, thanks a lot for trying 👍
j
No worries! I'll holla if I stumble over a solution 🙂
👍 1
l
Copy code
const challenge = await ctx.db.query.challenge({ where: { uniqueId: args.id } }, info);
const { uniqueId: id } = await ctx.db.query.challenge(
    { where: { uniqueId: args.id } },
    `{uniqueId}`
  );
  return {
    ...challenge,
    id,
  };
works but I wish I didn’t have to make two calls
j
Hooole up. Since you already have the ID, if the first query returned something, then that id exists...
Copy code
const challenge = await ctx.db.query.challenge({ where: { uniqueId: args.id } }, info);
return {
    ...challenge,
    id: args.id,
  };
👍 1
n
This is the general approach for implementing "computed fields": - add a field resolver for
uniqueId
- add
id
to
fragments
- in field resolver, return
parent.id
. https://github.com/prismagraphql/prisma-binding/issues/194#issuecomment-402656939
l
thanks @nilan, I was going the
fragments
path earlier today without success, I will dig deeper and look at the github comment you shared! Thanks a lot for your help!
👍 1