lawjolla
03/08/2018, 3:55 PMid
to always be returned, so ideally, I'd like to combine these into one...
db.query.users({}, `{ id }`)
db.query.users({}, info)
nilan
03/08/2018, 4:16 PMlawjolla
03/08/2018, 4:17 PMnilan
03/08/2018, 4:18 PMnilan
03/08/2018, 4:19 PMnilan
03/08/2018, 4:20 PMagartha
03/08/2018, 4:20 PMagartha
03/08/2018, 4:21 PMlawjolla
03/08/2018, 4:21 PMemail: String @isOwner
on the User
type. But if a query requests users { email }
, the id
is not available to the directive resolver, so there's no way to check if that's the owner of the email. In the User resolver, you can `db.query.users({}, { id, email}
) ` but that's obviously error prone and inartfulagartha
03/08/2018, 4:22 PMmax
03/08/2018, 4:24 PMlawjolla
03/08/2018, 4:24 PMlawjolla
03/08/2018, 4:24 PMlawjolla
03/08/2018, 4:25 PMnilan
03/08/2018, 4:25 PMagartha
03/08/2018, 4:26 PMnilan
03/08/2018, 4:26 PMlawjolla
03/08/2018, 4:26 PMagartha
03/08/2018, 4:26 PMagartha
03/08/2018, 4:27 PMquery Post {
title
mySpecialField
}
agartha
03/08/2018, 4:27 PMmySpecialField
specifies a fragment fragment MyFragment on Post { id }
agartha
03/08/2018, 4:28 PMquery Post {
...MyFragment
title
mySpecialField
}
fragment MyFragment on Post {
id
}
agartha
03/08/2018, 4:29 PMquery Post {
id
title
mySpecialField
}
agartha
03/08/2018, 4:29 PMmySpecialField
, the `parent`/`root` argument (the first one) will have the value for Post.id
as well that you need.lawjolla
03/08/2018, 4:30 PMnilan
03/08/2018, 4:30 PM{ id }
as the first argument to the resolve
function, which gives access to id
. That's probably not sometihng @lawjolla needs in his specific case, but is generally a super useful pattern.agartha
03/08/2018, 4:30 PMagartha
03/08/2018, 4:31 PM(parent, args, context, info)
for clarity, and just use parent.id
instead.agartha
03/08/2018, 4:31 PMlawjolla
03/08/2018, 4:32 PMnilan
03/08/2018, 4:32 PMid
in info
, not the actual id
for his resolver logic. So in his case he doesn't need { id }
as a first argagartha
03/08/2018, 4:33 PMagartha
03/08/2018, 4:33 PMlawjolla
03/08/2018, 4:33 PMid
into the directive resolver.lawjolla
03/08/2018, 4:33 PMinfo
agartha
03/08/2018, 4:33 PMtheis not available to the directive resolver, so there's no way to check if that's the owner of the email.id
agartha
03/08/2018, 4:34 PMnilan
03/08/2018, 4:34 PMmax
03/08/2018, 4:39 PMlawjolla
03/08/2018, 4:40 PMlastmjs
03/08/2018, 6:55 PMisOwner
directive to work arbitrarily for any type. If we have to declare the fragment before runtime, that's not going to worklastmjs
03/08/2018, 6:56 PMagartha
03/08/2018, 6:56 PMfragment
accepts a function, you should probably dive into the sources for thatagartha
03/08/2018, 6:57 PMagartha
03/08/2018, 6:57 PMagartha
03/08/2018, 6:57 PMlastmjs
03/08/2018, 6:57 PMagartha
03/08/2018, 6:58 PMagartha
03/08/2018, 6:58 PMlastmjs
03/08/2018, 6:58 PMagartha
03/08/2018, 7:00 PMlastmjs
03/08/2018, 7:39 PMfragmentReplacements
agartha
03/08/2018, 7:40 PMagartha
03/08/2018, 7:40 PMagartha
03/08/2018, 7:40 PMagartha
03/08/2018, 7:41 PMagartha
03/08/2018, 7:41 PMlastmjs
03/08/2018, 7:52 PMconst resolvers = {
Query: {
testUsers: {
fragment: `fragment Test on User { id }`,
resolve: async (parent, args, context, info) => {
console.log('parent', parent);
return await context.db.query.users({}, info);
}
}
}
};
const fragmentReplacements = extractFragmentReplacements(resolvers);
const server = new GraphQLServer({
typeDefs: `
type User {
id: ID!
email: String!
}
type Query {
testUsers: [User!]!
}
`,
resolvers,
context: (req) => {
return {
...req,
db: new Prisma({
endpoint: '<http://127.0.0.1:4466/backend/dev>', // the endpoint of the Prisma DB service
secret: 'mysecret123', // specified in database/prisma.yml //TODO obviously this should be controlled with environment variables
debug: true, // log all GraphQL queries & mutations
fragmentReplacements
})
};
}
});
server.start(() => console.log('Server is running on <http://localhost:4000'>));
lastmjs
03/08/2018, 7:53 PMagartha
03/08/2018, 7:56 PMlastmjs
03/08/2018, 7:59 PMlastmjs
03/08/2018, 8:01 PMlastmjs
03/08/2018, 9:09 PMlawjolla
03/08/2018, 9:23 PM@isOwner(type: String)
. Then made a wrapping function const isRequestingUserAlsoOwner = ({ ctx, userId, type, typeId }) => ctx.db.exists[type]({ id: typeId, user: {id: userId}})
For every type that includes @isOwner
and you don't control the client (to ensure id is queried), you'd have to make a fragment for the id
field on the data resolverlastmjs
03/08/2018, 9:51 PMlawjolla
03/08/2018, 9:52 PMlawjolla
03/08/2018, 9:53 PMlastmjs
03/08/2018, 9:54 PMlawjolla
03/08/2018, 9:54 PMlawjolla
03/08/2018, 9:56 PMlastmjs
03/08/2018, 10:03 PMlastmjs
03/09/2018, 12:56 AMlastmjs
03/09/2018, 12:57 AMagartha
03/09/2018, 12:57 AMlastmjs
03/09/2018, 8:18 PMlastmjs
03/12/2018, 10:42 PM