Skip to content

Conversation

@mcarleio
Copy link

Add new configuration generateDataFetchersAsInterfaces and generation code for Java and Kotlin to generate data fetchers as interfaces (instead of generating data fetchers with some default example implementation).

❗ Based on #327

@mcarleio
Copy link
Author

mcarleio commented Feb 16, 2022

The following will show the usage on a small example:

Assume the following schema:

type Query {
  people: [Person]
}

type Person {
  name: String
  partner: Person
}

This will generate the following two interfaces:

public interface QueryDataFetcher {
  @DgsQuery(
      field = "people"
  )
  List<Person> people(DgsDataFetchingEnvironment dataFetchingEnvironment);
}

public interface PersonDataFetcher {
  @DgsData(
      field = "partner",
      parentType = "Person"
  )
  CompletableFuture<Person> partner(DgsDataFetchingEnvironment dataFetchingEnvironment);
}

The only open step is to implement these interfaces and add the @DgsComponent annotation:

@DgsComponent
public class QueryDataFetcherImpl implements QueryDataFetcher {
    @Override
    public List<Person> people(DgsDataFetchingEnvironment dataFetchingEnvironment) {
        // load persons e.g. from DB
    }
}

@DgsComponent
public class PersonDataFetcherImpl implements PersonDataFetcher {
    @Override
    public CompletableFuture<Person> partner(DgsDataFetchingEnvironment dataFetchingEnvironment) {
        // implement as needed (e.g. make a DB call, use a dataloader, ...)
    }
}

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

Successfully merging this pull request may close these issues.

1 participant