Is there a way to query for an individual shop tha...
# orm-help
d
Is there a way to query for an individual shop that belongs to a user?
Copy code
type User {
  id: ID! @id
  shops: [Shop!]! @relation(name: "ShopsUsersRelation")
}

type Shop {
  id: ID! @id
  users: [User!]! @relation(name:"ShopsUsersRelation",link: INLINE)
 }
Its pretty common to get the shop for an id AND is also owned by the user requesting it
r
single query assumes you know that ID its that simple so you use the multi query that returns the array
in the future they might add more flexiblity to single item query
but i dont really see it as a big deal
just syntax
m
this should be something like
Copy code
query {
  shops(where:{
   id: "YOUR_SHOP_ID"
   user: {
     id: "YOUR_USERS_ID"
   }
  }) {
    id
    name
   user {
      id
   }
  }
}
if you check your docs on playground the user should accept and
UserWhereInput
nested in your
ShopsWhereInput
d
@mikedklein A user has many shops
I have now added a method to the top of my resolver to basically grab and return the shop by its ID and the user signed in
Copy code
const userHasAccess = async (ctx, id) => {
	const shops = await ctx.db.shops({ where: { id, users_some: { id: auth.getUserId(ctx) } } })
	if (shops.length > 0) {
		return shops[0]
	} else {
		return false
	}
}
m
yeah sorry I was a little unclear users_some is part of the UserWhereInput under your ShopsWhereInput
so you have what I am sugessting
you could also use the user_some with a unique constraint
because I thought you said that user id was part of the req
d
The user ID is part of it via JWT token.
I think what i have now is the only solution because of the missing API’s from prisma to mongo