I have a question regarding the <https://www.prism...
# orm-help
r
I have a question regarding the https://www.prisma.io/blog/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a -post. For instance, one can add a custom resolver for a field, lets say
Post.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?
h
you can solve this issue by doing something like this:
Copy code
const resolver = {
  Post: {
    firstTen : (parent) => {
      return parent.substring(10);
    }
  },
  User:{
    posts: (parent, args, ctx) => {
      return prisma.user({ id: parent.id }).posts()
    }
  }
}
That is the reason we encourage type safety. GraphqlGen will assist you in these type of situations

https://www.youtube.com/watch?v=TqPBUXUHQx8&amp;t=12s

r
Thank you for your quick reply, @Harshit, but I don't quite see how that snippet makes the
title
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?
h
that snippet will resolve post on the user type, and firstTen for every post
it will go
User -> posts -> Post -> firstTen
r
ok, thank you. But we still need to add the same resolver for other relations, like UserGroup?
h
it is a resolver for a type, mean
User
thing will run for everything returning a User type
no basically
Can you share your schema?
r
I'll try to recreate a smaller example from our actual code.
h
I have a good example for you
have a look , no need of sharing schema if it requires some work from your end
General rule: each type resolves it fields
r
yes, I might be a bit unclear. The resolver do get called from relations as expected, but the fragment needed is not available.
Copy code
// typeResolvers.ts
import { PermissionAccess } from '../schemas/generated/app'

export default {
  MediaData: {
    userPermissions: (root, args, context, info) => {
      return root.permissions.map(({ access }) => access)
    },
  },
}
Copy code
//index.ts
import Query from './queries'
import Mutation from './mutations'
import typeResolvers from './typeResolvers'

const resolvers = {
  Query,
  Mutation,
  ...typeResolvers,
}
Copy code
// 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,
}
Copy code
///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!]
}
Copy code
# working graphqlQuery
query {
  mediaDatas{
    edges{node{id name userPermissions archived}}
  }
}
`
Copy code
# non-working graphqlQuery (permissions not available)
query {
  albums{
    edges{node{name mediaData{name userPermissions}}}
  }
}
`
h
are you using prisma-bindings or prisma-client
r
prisma-bindings
h
Ok i was telling you about Prisma client till now 🤦
This has been resolved in a DM