aaronwlee/oak-graphql

authentication middleware for auth errors

Closed this issue · 7 comments

unable to send authentication error from context middleware or from app.use middleware,
can you suggest me the better way for handling the authentication, might be missing something to accomplish that.

version up to 0.3, here is some example

when global context error

const GraphQLService = await applyGraphQL({
  typeDefs: types,
  resolvers: resolvers,
  context: (ctx) => {
    if(...check cookie) {
      throw new GQLError("Authentication error")
    }
    return { user: "Aaron" };
  }
})

simple resolver level error handling

const resolvers = {
  Query: {
    getUser: (parent: any, { id }: any, context: any, info: any) => {
      if(context.user !== "Aaron") {
        throw new GQLError("Authentication error")
      }
      return {
        firstName: "wooseok",
        lastName: "lee",
      };
    },
  },
  Mutation: {
    setUser: (parent: any, { input: { firstName, lastName } }: any, context: any, info: any) => {
      console.log("input:", firstName, lastName);
      return {
        done: true,
      };
    },
  },
};

where do i get GQLError
error: TS2305 [ERROR]: Module '"https://deno.land/x/oak_graphql/mod"' has no exported member 'GQLError'.
import { applyGraphQL, GQLError } from "https://deno.land/x/oak_graphql/mod.ts";

hey can you also help me out, how can i get the query name or the info in context
const GraphQLService = await applyGraphQL({
typeDefs: types,
resolvers: resolvers,
context: (ctx) => {
if(...check cookie) {
throw new GQLError("Authentication error")
}
return { user: "Aaron" };
}
})
so that i can only perform authentication selected api's

you must reload the module, this is the basis of the deno.

deno run --reload https://deno.land/x/oak_graphql/mod.ts

the query name is in the request body

const { response, request } = ctx;
const body = (await request.body()).value;
body.operationName
body.variables
body.query

if you want to select to apply the authentication, then I'd like to recommend checking the context inside of the resolvers level.

hey sorry but once reload, now its giving error on routes and allowedMethods
app.use(GraphQLService.routes(), GraphQLService.allowedMethods());

error: TS2339 [ERROR]: Property 'routes' does not exist on type 'Promise<Router<Record<string | number, string | undefined>, Record<string, any>>>'.
app.use(GraphQLService.routes(), GraphQLService.allowedMethods());

Please update the Oak framework to latest version too

Thanks a lot @aaronwlee for your quick response