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

Typescript potentially null #1403

Open
1 task done
luwol03 opened this issue Apr 7, 2022 · 1 comment
Open
1 task done

Typescript potentially null #1403

luwol03 opened this issue Apr 7, 2022 · 1 comment

Comments

@luwol03
Copy link

luwol03 commented Apr 7, 2022

Question about GraphQL Shield

In the following example typescript tells me, that ctx.user is potentially null as I defined the types for my context. However, my shield already filters that case out. But typescript doesn't recognize that. Is there some way to fix that without adding in every resolver a separate check and without using a ! After every ctx.user!.id?

Context:

export interface Context {
  request: Request;
  response: Response;
  prisma: PrismaClient;
  user: User | null;
}

export async function createContext(
  request: ExpressContext
): Promise<Partial<Context>> {
  const context: Context = {
    ...request,
    response: request.res,
    request: request.req,
    prisma: prisma,
    user: null,
  };

  const userId = getUserId(context);
  if (userId) {
    context.user = await prisma.user.findFirst({
      where: {
        id: userId,
      },
      rejectOnNotFound: true,
    });
  }

  return context;
}

Shields:

const isAuthenticated = rule({ cache: 'contextual' })(
  async (_parent, _args, ctx: Context) => {
    return ctx.user !== null;
  }
);

const permissions = shield({
  Query: {
    '*': isAuthenticated,
  },
  Mutation: {
    '*': isAuthenticated,
    login: allow,
    signup: allow,
    refreshAuth: allow,
    logout: allow,
  },
});

Mutation:

export const voteItem = mutationField('voteItem', {
  type: nullable(Vote),
  args: {
    where: nonNull(ItemWhereUniqueInput),
  },
  resolve: async (_root, args, ctx) => {
    return ctx.prisma.vote.create({
      data: {
        user: {
          connect: {
            id: ctx.user.id
          }
        },
        item: {
          connect: {
            id: args.where.id,
          },
        },
      },
    });
  },
});
  • I have checked other questions and found none that matches mine.
@open-collective-bot
Copy link

Hey @luwol03 👋,

Thank you for opening an issue. We will get back to you as soon as we can. Have you seen our Open Collective page? Please consider contributing financially to our project. This will help us involve more contributors and get to issues like yours faster.

https://opencollective.com/graphql-shield

We offer priority support for all financial contributors. Don't forget to add priority label once you become one! 😄

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