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

how to point __resolveReference method to _resolveReference? #65

Open
girishramnani opened this issue Jan 23, 2023 · 9 comments
Open

how to point __resolveReference method to _resolveReference? #65

girishramnani opened this issue Jan 23, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@girishramnani
Copy link

Hi,
So as per this doc https://www.apollographql.com/docs/apollo-server/using-federation/api/apollo-subgraph/#__resolvereference the __resolveReference has to be called ( and it cannot be changed ) and not _resolveReference so I am not able to make it work, is there a config need to be changed?

@kdawgwilk
Copy link
Collaborator

kdawgwilk commented Jan 23, 2023

The gateway should never be calling that resolver directly but should use the _entities(...) field instead. Are you seeing the gateway calling __resolveReference when trying to resolve entities? If that's the case it would be a breaking change to the federation spec and I am not aware of any breaking changes. What version of the gateway/router are you running?

@girishramnani
Copy link
Author

We are using apollographql/router:v1.2.0 so 1.2.0 version and yeah I can see that __resolveReference is being called. But also isn't that what the docs say that thats what will be called?

@kzlsakal
Copy link
Collaborator

__resolveReference is a function implementation specific to Apollo Server. Apollo Router shouldn't be making any GQL queries for a __resolveReference field. Field names that start with __ are reserved for introspection only as it's stated here and here.

If you follow the "Defining an entity" link in that Apollo Server documentation, it says ⚠️ This section describes how to create reference resolvers in Apollo Server. If you're using another subgraph-compatible library, see its documentation for creating reference resolvers. here.

There is an example repository with multiple entity examples and tests in Elixir if it would be helpful.

@abhilashr2021
Copy link

abhilashr2021 commented Jan 27, 2023

When i tried following the above approach, it throw me with below error

  ** (FunctionClauseError) no function clause matching in Absinthe.Middleware.unshim/2

     The following arguments were given to Absinthe.Middleware.unshim/2:
     
         # 1
         [{Absinthe.Middleware.Telemetry, []}, {{Absinthe.Resolution, :call}, &MyApp.V2.Graphql.Resolvers.Books.get_books/3}]

There was a similar issue faced by another person, and he mentioned the below approach
https://github.com/tobiasbernet/elixir_federation

i had to change the resolver enclosed inside anonymnous function call.

field(:_resolve_reference, :book) do
      resolve(fn _, args, _ -> Books.get_books(nil, args, nil) end)
    end

but the resulting output is %{data: %{"_entities" => [%{}]}}
instead of respective book object

@kzlsakal
Copy link
Collaborator

kzlsakal commented Feb 3, 2023

where is the field definition of that _resolve_reference?

If you define it under the correct object definition like this

query do
end

object :book do
  extends()
  key_fields("id")

  field :id, :id do
    external()
  end

  field :name, :string

  field :_resolve_reference, :book do
    resolve(fn _, args, _ -> Books.get_book(nil, args, nil) end)
  end
end

then run a query like this:

query GetBookEntities($representations: [_Any]) {
  _entities(representations: $representations) {
    ... on Book {
      name
      id
      __typename
    }
  }
}

with variables: {"representations": [{"id": "foo", "__typename": "Book"}]}

it would resolve the entity's fields

@kzlsakal
Copy link
Collaborator

kzlsakal commented Feb 22, 2023

We added a test to demonsrate how _resolve_reference works but we can keep this issue open to address the case with the named function usage for the resolver.

@kzlsakal kzlsakal added the bug Something isn't working label Feb 22, 2023
@tristantreb
Copy link
Contributor

I had the same issue and solved it by using an anonymous function (as per @abhilashr2021's comment - thanks!)

Why doesn't it work with function capturing?

resolve(&Books.get_books/3)

@kdawgwilk
Copy link
Collaborator

@tristantreb its likely due to the complexities of the compile-time magic that absinthe is doing and how it captures those references using macros. I do think its solvable and we should see if we can get a fix for it

@kzlsakal
Copy link
Collaborator

Why doesn't it work with function capturing?

This will be fixed by #104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants