question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to match a resolver to a Node.Field

See original GitHub issue

Not sure if the best place for this issue is here or at graphene repo (https://github.com/graphql-python/graphene), but here we go …

I have a Node.Field that I want to match to a resolver in order to apply token authentication/authorization. But it seems the resolver is completely ignored. Here is a code sample:

from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from graphene import ObjectType, Node, String

from core.utils import validate_token

from .models import Paciente


class PacienteType(DjangoObjectType):

    class Meta:
        model = Paciente
        interfaces = (Node, )
        filter_fields = ['id', 'nome']


class Query(ObjectType):

    paciente = Node.Field(PacienteType, id=String(), token=String())
    paciente_all = DjangoFilterConnectionField(PacienteType, token=String())

    def resolve_paciente(self, info, id=None, token=None):
        usuario_id = validate_token(token)
        if usuario_id is None:
            return None

        try:
            return Paciente.objects.get(pk=id)
        except:
            return None

    def resolve_paciente_all(self, info, token=None):
        usuario_id = validate_token(token)
        if usuario_id is None:
            return []
        return Paciente.objects.all()

Then I issue the following query

{
    paciente(id: "UGFjaWVudGVUeXBlOjE=") {
      id
    }
}

Which I expected to be blank, since I’m not supplying any auth token, but it does return with one result. Am I missing something here that I have not seem, or this may be a bug?

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
mraakcommented, Nov 10, 2017

Try this, instead of:

paciente = Node.Field(PacienteType, id=String(), token=String())

do:

paciente = graphene.Field(PacienteType, id=String(), token=String())

or simply:

paciente = graphene.Field(PacienteType)

The documentation is non existent, and examples are very inconsistent, lack descriptions, lack the “why’s”, versions mixed, relay vs non-relay confused, etc. Looks like nobody is really in charge of this project so get ready for trial and error unfortunately.

2reactions
Fire-Houndcommented, May 10, 2020

to resolve a global id, inside a normal field do relay.Node.get_node_from_global_id(info, id)

I wish I knew how this worked, really is like magic to me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing query resolvers | Full-Stack Quickstart - Apollo GraphQL
A resolver function returns one of the following: Data of the type required by the resolver's corresponding schema field (string, integer, object, etc.)...
Read more >
write field level resolver for a field in user type in graphql
I am trying to generate user id using uuid npm package for the id field in the user type. I am not sure...
Read more >
Resolvers | NestJS - A progressive Node.js framework
To create a resolver, we create a class with resolver functions as methods, and annotate the class with the @Resolver() decorator.
Read more >
Resolvers - Server-Side GraphQL in Node.js - Frontend Masters
Check out a free preview of the full Server-Side GraphQL in Node.js course: ... Resolvers must return the value type declared for the...
Read more >
Tutorial: Lambda resolvers - AWS AppSync
You can use AWS Lambda with AWS AppSync to resolve any GraphQL field. ... The following example shows a Lambda function written in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found