Hello! I’m trying to build a query on using GraphQ...
# orm-help
p
Hello! I’m trying to build a query on using GraphQL Yoga that calls Prisma and returns results. There is, however, a considerable difference in the object queried and that returned. Consider that the the Prisma query would be as follows:
Copy code
query {
  id
  multilingualData {
    language
    title
  }
}
where info would be an array. I want the user to query GraphQL Yoga as follows:
Copy code
query {
  id
  title
}
It is, of course, easy to map the Prisma result to another object and return it to the user. But then the
info
argument comes into play. In order for me to fetch
multilingualData
I have to pass an
info
object to the Prisma query, the following string for example:
{ id multilingualData { language title } }
which is of course error-prone. Any thoughts on how to better achieve this?
l
So I'm following, is the issue that you ned the language and title returned?
p
The issue is the maintainability of the code
Right now, I’m providing the
info
object to Prisma as text
1. It’s tedious! 2. What happens if I add or change a data type and forget to update this? No type checking for text!
l
I think we've had the same issue, and I think I know where this is headed, but I'm trying to make sure before popping off. 🙂
p
uh oh…
This doesn’t sound reassuring lol
l
It's not an issue, but I've spent my last 3 weeks in front end land, so I'm putting my Prisma hat back on. 🙂 So you're worried if you change the multilingual type?
p
For instance. It’s just that passing a parameter like this as a text value looks alarming to me.
Needless to say, it’s not just the above, it’s much more
l
It's not necessarily if there are values that are needed from a type you don't want to directly expose. Nikolas has a similar example in his
Info
blog
p
Yup, I read that. I actually need to expose like half the schema!
I was hoping I can find a value for
info
that would just say “return everything”
l
So if a user queries
query { id title ...}
, is that returning everything? Is the problem that there are fields nested in multilingual data that may need to be returned?
p
Well, the issue is that I have to build the info text myself to expose everything I want to return from Prisma, because the info object provided in the user query doesn’t make sense to Prisma, since it does not map 1-to-1. See what I mean?
l
I do. So I'm wondering why you can't leave off
info
to get all of the scalars. Does
multilingual
data have nested queryable types that could also be returned?
p
If I leave off info, multilingualData does not get returned
…and that’s normal since it’s not a scalar, right?
l
Can you post your resolver?
p
Copy code
import { Context } from '../../context';

export const families = async (parent, { filters }, ctx: Context) => {
  return ctx.db.query
    .families(
      {
        where: {
          accommodationOptions_some: {
            price_lte: filters.maxRate,
            type_in: filters.accommodationOptionTypes
          }
        }
      },
      `{
        id
        oldId
        info {
          language
          name
          description
        }
        type
        children
        pets
        genderAccepted
        canHostOtherGuests
        photos {
          storeId
        }
        accommodationOptions {
          id
          type
          price
        }
      }`
    )
    .then(result =>
      result.map(family => {
        const familyInfo = family.info.find(item => item.language === 'en');
        return {
          id: family.id,
          oldId: family.oldId,
          name: familyInfo.name,
          description: familyInfo.description,
          type: family.type,
          children: family.children,
          pets: family.pets,
          genderAccepted: family.genderAccepted,
          canHostOtherGuests: family.canHostOtherGuests,
          photos: family.photos,
          accommodationOptions: family.accommodationOptions
        };
      })
    );
};
👀 1
The thing here is: the family object queries from Prisma does not map 1-to-1 to the family object returned to the user.
The above works well… but just bothers me 😄
l
I'm chewing on it. These questions help me so much because I know I've faced this, but seeing it in a different context forces you to really know it. 🙂
🙂 1
So in the
result.map
, you're returning everything that could be asked for?
p
Yes, but that’s not everything I can get from Prisma, I’m holding off certain information.
and transforming others, such as returning a single language instead of the array of languages stored.
l
Ok, think I got it
Ok, I'm pretty sure the pattern here is...
Copy code
export const families = async (parent, { filters }, ctx: Context, info) => {
  return ctx.db.query
    .families(
      {
        where: {
          accommodationOptions_some: {
            price_lte: filters.maxRate,
            type_in: filters.accommodationOptionTypes
          }
        }
      }, info)
}
...
Family = {
   accomodationOptions: async (dataReturnedFromFamliesQuery, args, ctx, info) => {
        return ctx.db.accmmodationOptions({...}, info)
   },
   info: ...,
   etc
}
p
I’ll have to look at this closely. Did you add a different resolver to the type Family? Is that it?
l
Exactly...
p
I’ll try this out.
Thank you Dennis 🙂
I’ll let you know
l
I'm trying to find some examples to show, but I have all of this file loader crap, so my stuff doesn't look right
p
LOL
I think I got the gist of it though; I guess you’re right, the blog post written by Nikolas hinted to something of that sort.
l
And if you need specific data to make the query, use a fragment, e.g...
Copy code
Family: {
    accomodationOptions: {
        fragment: `fragment needFamilyId on Family {id}`,
        resolve: async(...) => { ... }
    }
}
👍 2
To me, it initially felt bad because I'm old school SQL and I want to ask for it all in one hit, and it's obvious that the server needs to break down the queries using this approach. But this is what gives us truly modular and flexible queries
p
Haha, I understand you!
c
c
ah, wonderful! thanks @picosam! (i was looking for articles where nikolas was the author and couldn't find it... 🙂 )