grails / gorm-graphql

An automatic GraphQL schema generator for GORM
Apache License 2.0
80 stars 23 forks source link

Custom Data Fetcher in gorm-graphql 2.0.0 + MultiTenancy project - Fails Startup #49

Open ianibo opened 4 years ago

ianibo commented 4 years ago

heya - 2.0.0 looks really good and MultiTenancy support is definitely better - out of the box confirmed working with Database-Per-Tenant mappings too - so that's great.

Trying to add in a custom data fetcher when working in Multi-Tenancy mode seems to encounter a similar problem to v1 with static access at applicaiton startup:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphQL': Cannot resolve reference to bean 'graphQLSchema' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphQLSchema': Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.schema.GraphQLSchema]: Factory method 'generate' threw exception; nested exception is org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException: Tenant could not be resolved outside a web request

The project works perfectly with the out-of-the-box config until we declare a custom fetcher (Simplified example):

static graphql = GraphQLMapping.lazy {
  query('generalAlertBaseQuery', pagedResult(AlertBase)) {
    defaultListArguments()
    argument('qterm', String) {
      nullable true
      description "Left anchored alert name to search for"
    }
    dataFetcher(new PaginatedEntityDataFetcher<List<AlertBase>>(AlertBase.gormPersistentEntity)     {
      @Override
      protected DetachedCriteria buildCriteria(DataFetchingEnvironment environment) {
        def q = new DetachedCriteria(AlertBase).build {
          rlike('name',/${environment.getArgument('qterm')}/)
        }
        return q
      }
    })
  }
}

This code is yanked directly from a v1 test rig, and I've only scanned over the updated 2.0.0 docs so apologies if I've missed something critical.

Is it possible that custom fetchers can be made to work in the multi-tenant setup? WIll try and update the test project to recreate this in a simple env.