grooviter / gql

Groovy GraphQL library
http://grooviter.github.io/gql/
Apache License 2.0
49 stars 6 forks source link

Add `typeResolver` method to `mergeSchema` DSL #20

Closed mariogarcia closed 6 years ago

mariogarcia commented 6 years ago

Having an schema with an interface and several implementations:

interface Raffle {
  id: String
  title: String
  names: [String]
}

type SimpleRaffle implements Raffle {
  id: String
  title: String
  names: [String]
  owner: String
}

type TwitterRaffle implements Raffle {
  id: String
  title: String
  hashTag: String
  names: [String]
}

type Queries {
  raffles(max: Int): [Raffle]
}

schema {
  query: Queries
}

We should be able to execute a query like this:

{
  raffles(max: 2) {
        title
        ... on TwitterRaffle { 
          hashTag
        }
      }
    }

A possible DSL implementation should look like:

GraphQLSchema proxySchema = DSL.mergeSchemas {
      byResource('gql/dsl/typeresolver.graphqls') {

        mapType('Queries') {
          link('raffles') {
            return [[title: 'T-Shirt', hashTag: '#greachconf']] 
          }
        }

        mapType('Raffle') {
          typeResolver { TypeResolutionEnvironment env ->
            def raffle = env.getObject() as Map  
            def schema = env.schema

            return raffle.containsKey('hashTag') ?
              schema.getObjectType('TwitterRaffle') : 
              schema.getObjectType('SimpleRaffle')
          }
        }

      }
    }

The method typeResolver should provide two different options:

typeResolver(Closure typeResolver) or typeResolver(TypeResolver typeResolver)