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 handle resolver for field with Array type? #12

Open
fresonn opened this issue Mar 27, 2024 · 2 comments
Open

How to handle resolver for field with Array type? #12

fresonn opened this issue Mar 27, 2024 · 2 comments

Comments

@fresonn
Copy link

fresonn commented Mar 27, 2024

For example we have:

# schema.graphql
type Todo {
  id: ID!
  text: String!
  done: Boolean!
  users: [User!]
}

type User {
  id: ID!
  name: String!
}
# gqlgen.yml
models:
  Todo:
    fields:
      users:
        resolver: true

In regular case I need a loader like this:

func (t *todoLoader) todoUsers(ctx context.Context, todoIDs []string) ([]User, []error) {...}

Function for gqlgen resolver

func GetTodoUsers(ctx context.Context, todoID string) ([]User, error) {
	loaders := For(ctx)
        // I can't use .Load() because it returns T not []T
	return loaders.TodoLoader.LoadAll(ctx, []string{productID})
}

But when todoUsers() returns the users slice []User with len > 1 there is always an error:

"message": "bug in loader: 2 values returned for 1 keys"

How can I get in result something like this?

{
  "id":"1",
  "text":"...",
  "done":false,
  "users":[
    {
      "id":"1",
      "name":"Bob"
    },
    {
      "id":"2",
      "name":"Alice"
    }
  ]
}
@vikstrous2
Copy link

vikstrous2 commented Apr 5, 2024

Your fetch function should return [][]User, not []User. Each of the returned results is interpreted as a response to the query for one key.

@fresonn
Copy link
Author

fresonn commented Apr 8, 2024

Yes, it next case question.
How can I use "many to one" case for now?
For example, I have a single entity query with loader.Load():
ID --> []T{T, T, T}
Field resolver gets one entity "id"

I think I am on wrong way...
Could you provide an example here: https://github.com/vikstrous/dataloadgen-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants