been banging my head on a wall for days, hoping so...
# orm-help
d
been banging my head on a wall for days, hoping someone can help me. I'm querying a legacy mysql database (not the prisma service) to get a list of users. I format the results to match my prisma schema and I have the following model defined in my graphql application
Copy code
export const User = prismaObjectType({
  name: "User",
  definition(t) {
    t.prismaFields(["id", "email", "first_name", "last_name", "address"]);
  }
});
This works fine except 'address' is returning as null. Address is a nested object type so I'm not sure how/why that's causing issues. Can anyone help?
j
keep banging
d
@Jorge ughhh, because there's no good way to do it or you don't know how to do it off the top of your head ?
j
I dont know
im new to nexus
j
Does it work to query the address when using the Prisma GraphQL Playground?
d
@Jenkins so i can't query address on the prisma service because the address data is stored in a separate mysql database. I want to use the prisma schema, but resolve the query against my mysql database
j
Ok, I see. What you could potentially do is query the Prisma client like normal and if successful, query the mysql field and attach the response data from the database. I'll try and write up a quick resolver.
d
ok that would be a huge help, thank you !
j
Btw, quick question. Is
address
a field in your
datamodel.prisma
?
Because, as it exists in a different database, it shouldn't be. And you should be able to just add
address
as a 'computed field' in your GraphQL Schema. That resolver would look something like: Assume
ctx: { dbp: PrismaClient, dbm: MySQLClient  }
Copy code
export const User = prismaObjectType({
  name: "User",
  definition(t) {
    t.prismaFields(["id", "email", "first_name", "last_name"]);
    t.field({
      name: 'address',
      type: 'Address', // You have to define this somewhere and add it to the types array during `makeSchema`
      resolve: (parent, args, ctx) => ctx.dbm.addressByUserId({ id: parent.id })
    });
  }
});
Or something along those lines. Not 100% sure as I'm still fairly new to Nexus (but loving it so far).
d
ok so this makes a lot of sense, i didn't know i could pass in two database instances to my context. One question though. When i go to retrieve all the users (lets say 500 of them). Does that then mean this query will fire off 500 addressByUserID() requests ?
j
Yes, that's the unfortunate part...
At least as far as I know.
d
damn alright, don't think it should be a huge issue if I just throw in some pagination, i'll give this a go though, thanks for the help!
j
You could , instead of adding a resolver to the field, have it added per query resolver. Then you would first query the prisma data, and then (in the userS case) send a query with an array of user IDs. It would require some mapping of the data, but it would then not be N queries to the MySQL database.
I still think you should be alright if you add pagination, yeah.
And no worries - I'm happy to help! 🦜
Also, fyi, you can put whatever the f you want in your context 🙂 It's very modifiable.
d
ok i think i like that approach even more... and makes sense about the context, haven't done a ton of work with graphql so still trying to wrap my head around the integration of prisma/nexus/graphql
j
Whatever floats your boats, man! 🙂 Yeah, I was in your shoes last spring. Been working with GraphQL ever since. GraphQL Nexus has really rekindled my love for it though. It's amazing how nice and easy it is to work with.
d
yeah ive been loving it so far... still seems like prisma/nexus is in it's infancy but i see a lot of potential with them