Giter VIP home page Giter VIP logo

Comments (4)

SimonCropp avatar SimonCropp commented on May 18, 2024 1

UserContextExtensions.GetClaims is called within the resolve. BTW we are well off track from this having anything to do with GraphQL.EntityFramework, ie your current problem could be reproduced without using GraphQL.EntityFramework. Perhaps move the question to stackoverflow? Or i can quote you an hourly rate to help you debug further

from graphql.entityframework.

SimonCropp avatar SimonCropp commented on May 18, 2024

By passing the DataContext in on the UserContext, does this make it impossible to use authentication

yes. but nothing about GraphQL.EntityFramework forces u to take that approach. what u place in the graphql context, and how u read from it is up to u. So at my current client we have this model for the user context

public class GraphQlUserContext
{
    public ElectorateDbContext DbContext { get; set; }
    public ClaimsPrincipal User { get; set; }
}

then in the graphql controller we do

       var options = new ExecutionOptions
        {
            Schema = schema,
            Query = query,
            OperationName = operationName,
            Inputs = variables?.ToInputs(),
            UserContext = new GraphQlUserContext
            {
                DbContext = dataContext,
                User = User
            },
            CancellationToken = cancellation,
#if (DEBUG)
            ExposeExceptions = true,
            EnableMetrics = true,
#endif
        };

then we have some helper extension methods


static class UserContextExtensions
{
    public static ElectorateDbContext GetDataContext(this ResolveFieldContext<object> context)
    {
        var userContext = (GraphQlUserContext) context.UserContext;
        return userContext.DbContext;
    }

    public static ElectorateDbContext GetDataContext<T>(this ResolveFieldContext<T> context)
    {
        var userContext = (GraphQlUserContext) context.UserContext;
        return userContext.DbContext;
    }

    public static List<Claim> GetClaims(this ResolveFieldContext<object> context)
    {
        var userContext = (GraphQlUserContext)context.UserContext;
        return userContext.User.Claims.ToList();
    }
}

which enables a query like this


public class Query : EfObjectGraphType
{
    public Query(IEfGraphQLService efGraphQlService) : base(efGraphQlService)
    {
        AddSingleField<ProgramGraph, Program>(
            name:"program",
            resolve: context =>
            {
                var dataContext = context.GetDataContext();
                return dataContext.Programs;
            });

from graphql.entityframework.

jandrews377 avatar jandrews377 commented on May 18, 2024

Hi Simon,

Thank you for the ultra-quick response :)
I have plumbed up as per your example, the dbcontext is working correctly.
I am missing something with the UserClaims as I keep getting the error message on my mutation:

GraphQL.Validation.ValidationError: You are not authorized to run this mutation.\nRequired claim 'role' with any value of 'Admin' is not present.

When I set a breakpoint on my:
var options = new ExecutionOptions

I can see that the User object has the correct claims, and my ValidationRule is being populated.
In you code, where are calling UserContextExtensions.GetClaims from?

Many thanks.
Jeremy

from graphql.entityframework.

SimonCropp avatar SimonCropp commented on May 18, 2024

closing this for now

from graphql.entityframework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.