Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typesafety when a plugin adds a field to context #1013

Open
ahmedosama7450 opened this issue Nov 13, 2021 · 0 comments
Open

Typesafety when a plugin adds a field to context #1013

ahmedosama7450 opened this issue Nov 13, 2021 · 0 comments

Comments

@ahmedosama7450
Copy link
Contributor

ahmedosama7450 commented Nov 13, 2021

Hi there!

I am making a nexus plugin, which computes expensive operations (e.g. database fetch) and passes them into context to be used by different sub-resolvers

An example usage of the plugin can be something like:

queryField("postQuery", {
    ...
    
    // This is the field that my plugin adds which return an object of the values to be computed
    compute: (_, { id }, { db })=> {
      return {
        post: fetchPost(db);
      }
    },
    
    authorize: (_, __, {db, computed}, )=> {
      // use computed.post           (not type-safe)
    },
    
    validate: (_,__,{computed})=> {
       // use computed.post      (not type-safe)
    },
    
    resolver: (_,__,)=> {
      // use computed.post      (not type-safe)
    }
  
}
)

Everything is working fine except that It's not type-safe.

Here is how the context is typed right now

export interface Context  {
  ...
  computed?: Record<string, any>;
}


Proposed Solution

The plugin factory method should accept a parameter called additonalContextType which is a string e.g.

type SomeType<T> =  {
 computed: T
}

plugin(
  {
    additionlContextType: "SomeType<T>"
  }
)

Then all additionalContextType parameters from all plugins should be merged with the original context type

So, Right now one problem is solved, the plugin is able to add an additional type to context, but still this T generic should be the return type of the compute function. How can that be achieved ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant