rooneyK
01/02/2019, 9:10 AMPost.firstChars
, which returns the first 10 characters of the title
-field. This requires a fragment to be added on Query.posts
which ensures that the field title
is available. This works great when I am accessing the field through posts
, ut for relations accessing Posts.firstChars
, lets say through query{ users{ id posts{ title } } }
, the fragment is of course not available. Is it possible to add the fragment in some other way, so that the field is always available, no matter the root-resolver?Harshit
01/02/2019, 9:42 AMconst resolver = {
Post: {
firstTen : (parent) => {
return parent.substring(10);
}
},
User:{
posts: (parent, args, ctx) => {
return prisma.user({ id: parent.id }).posts()
}
}
}
Harshit
01/02/2019, 9:42 AMrooneyK
01/02/2019, 10:27 AMtitle
of the post available on parent
. Typesafe GraphQL is great stuff, and I will surely work it into our project, since it seems to fix a lot of our worries. Am I correct in my understanding of that you are suggesting that we should make sure that title
is available for every other resolver that has Post
as a relation?Harshit
01/02/2019, 10:29 AMHarshit
01/02/2019, 10:30 AMUser -> posts -> Post -> firstTen
rooneyK
01/02/2019, 10:32 AMHarshit
01/02/2019, 10:33 AMUser
thing will run for everything returning a User typeHarshit
01/02/2019, 10:33 AMHarshit
01/02/2019, 10:34 AMrooneyK
01/02/2019, 10:34 AMHarshit
01/02/2019, 10:35 AMHarshit
01/02/2019, 10:35 AMHarshit
01/02/2019, 10:36 AMHarshit
01/02/2019, 10:37 AMrooneyK
01/02/2019, 10:47 AMrooneyK
01/02/2019, 10:47 AM// typeResolvers.ts
import { PermissionAccess } from '../schemas/generated/app'
export default {
MediaData: {
userPermissions: (root, args, context, info) => {
return root.permissions.map(({ access }) => access)
},
},
}
//index.ts
import Query from './queries'
import Mutation from './mutations'
import typeResolvers from './typeResolvers'
const resolvers = {
Query,
Mutation,
...typeResolvers,
}
// queries.ts
// Adding fragment here to make `permissions` available for `userPermissions`-resolver
const mediaDatas: GraphQL<MediaDatasQueryArgs> =
(_, args, { db, user }, info) => {
const fragment = `fragment EnsurePermissions on MediaDataConnection {
edges{node{
permissions(
where:{user: {firstName: "myVeryFirstUser"}}
){ access }
}}
}`
return db.query.mediaDatasConnection(args, addFragmentToInfo(info, fragment))}
// has relations to mediaData, so fragment might be needed here as well, but
// we have lots of types with relations to mediaData. Don't want to add fragments
// everywhere if it can be done only when needed.
const albums: GraphQL<AlbumsQueryArgs> =
(_, args, { db, user }, info) => args, info)}
export default {
mediaDatas,
albums,
}
///MediaData.schema
type MediaData implements Node {
...other fields
userPermissions: [PermissionAccess!]!
permissions(where: PermissionWhereInput, orderBy: PermissionOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Permission!]
}
# working graphqlQuery
query {
mediaDatas{
edges{node{id name userPermissions archived}}
}
}
`
# non-working graphqlQuery (permissions not available)
query {
albums{
edges{node{name mediaData{name userPermissions}}}
}
}
`
Harshit
01/02/2019, 10:49 AMrooneyK
01/02/2019, 11:02 AMHarshit
01/02/2019, 11:04 AMHarshit
01/02/2019, 12:52 PM