Search code examples
javagraphqlgraphql-java

Returned null on DataFetchingEnvironment .getUser() in Mutation class


Original Question: https://github.com/howtographql/graphql-java/issues/4

AuthContext context = env.getContext();
Link newLink = new Link(url, description, context.getUser().getId());

I do not get null on context variable, but I get a null on context.getUser()

code:https://github.com/howtographql/graphql-java/blob/master/src/main/java/com/howtographql/hackernews/Mutation.java or my code: https://github.com/JonathanSum/myOwnJavaGraphqlStarterKit/blob/master/src/main/java/com/howtographql/hackernews/Mutation.java


Solution

  • I'm the author of that tutorial, which is now unfortunately very outdated... I'm working on rewriting it in pure graphql-java (without graphql-java-tools), and this is what I'd recommend you try as well.

    For the time being, your issue is very likely either because the browser doesn't send the user ID (in the Authorization header) at all or, if it does, it doesn't match the ID in Mongo.

    Put a break point in createContext method in GraphQLEndpoint and inspect what happens.

    The tutorial explains the code you need to add to the client to start sending the Authorization header, so make sure you followed that. otherwise, just make sure the ID it sends matches what you have in Mongo.