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

Provide validate types only response function #9

Open
aexol opened this issue Apr 21, 2024 · 0 comments
Open

Provide validate types only response function #9

aexol opened this issue Apr 21, 2024 · 0 comments

Comments

@aexol
Copy link
Contributor

aexol commented Apr 21, 2024

Sometimes it would we useful to validate response type. The function should also give a way to transform the validated type:

import { getUserOrThrow } from '@/src/auth.js';
import { createResolvers } from '@/src/axolotl.js';
import { GraphQLError } from 'graphql';

export const Mutation = createResolvers({
  Mutation: {
    public:() => {
      return {}
    },
    user:async ([,,context],args,validate) => {
      const authHeader = context.request.headers.get("Authorization")
      if(!authHeader) throw new GraphQLError("You must be logged in to use this resolver")
      return validate(getUserOrThrow(authHeader))
    }
  },
})

This should be identity function only used for type check without real validation of response only to help developer experience

const validate = <Z={}>(o:T extends OUR_RESPONSE_TYPE):T => o

Overloading would look like that

import { getUserOrThrow } from '@/src/auth.js';
import { createResolvers } from '@/src/axolotl.js';
import { GraphQLError } from 'graphql';

export const Mutation = createResolvers({
  Mutation: {
    public:() => {
      return {}
    },
    user:async ([,,context],args,validate) => {
      const authHeader = context.request.headers.get("Authorization")
      if(!authHeader) throw new GraphQLError("You must be logged in to use this resolver")
      return validate<ReturnType<typeof validate> & { smth:string }>(getUserOrThrow(authHeader))
    }
  },
})
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