Infer Source

Infer Source Type

In GraphQL usually there is no way to know the source type from incoming data to the resolver. This is because you as a developer control the way everything is resolved.

schema.graphql
type Query{
  onlySomeCanPass: World
}
type World{
  restOfTheStory: String
}

So this way the type of source will contain some user data which is not known when you look at the graph and it is internal backend information.

So imagine you create resolvers this way:

query.ts
export default createResolvers({
  Query:{
    onlyUserCanPass: async (input,args) =>{
      return {
        username: "Gandalf"
      }
    }
  }
})

So when we create a new resolver for World.restOfTheStory there will be different stories for different users. So we need to know what has been returned.

world.ts
import QueryResolvers from './query.js'
import { setSourceTypeFromResolver } from '@aexol/axolotl-core'
 
const convertSource = setSourceTypeFromResolver(QueryResolvers.onlyUserCanPass)
 
export default createResolvers({
  World:{
    restOfTheStory: async (input,args) =>{
      // this is type-safe now. And we know that user is logged in.
      // the inferred source type is { userId: string; username:string } 
      const src = convertSource(input.source)
      if(src.username === "Balrog") return "Die"
      if(src.username === "Gandalf") return "Fall down and return later."
      return "Go safely through the bridge"
    }
  }
})