graphql-compose / graphql-compose-elasticsearch

Hide Elastic Search REST API behind GraphQL.

Home Page:https://graphql-compose.herokuapp.com/elasticsearch/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Relations added to typeComposer do not appear in findById resolver output

vecernik opened this issue · comments

I love this project, just found a minor bug.

const typeComposer = composeWithElastic({
  graphqlTypeName: 'Room',
  elasticIndex: 'room-index-v1',
  elasticType: 'doc',
  elasticMapping,
  elasticClient: dao.client,
})

typeComposer.setTypeName('Room')  // prevent naming the type RoomRoom by default 

typeComposer.addRelation('Chairs', {
  name: 'RoomChair',
  type: [ 'Chair' ],              // Chair typeComposer is already initialised
  resolve: (source) => ...
})

schemaComposer.Query.addFields({
  Room: typeComposer.getResolver('search').getFieldConfig(),
  RoomById: typeComposer.getResolver('findById').getFieldConfig()
})

const server = new ApolloServer({
  schema: schemaComposer.buildSchema()
})

With a setup above, following query doesn't work as expected, RoomById doesn't contain relations, even though Room type does:

{
  Room(sort: [id__desc]) {
    hits {
      _source {   // Room type
        id
        Chairs {    // WORKS OK
          id
        }
      }
    }
  }
  RoomById(id: "1234") {   // RoomFindByIdOutput type
    id
    Chairs {       // ERROR HERE
      id
    }
  }
}

I tried to debug through the code, but I'm still missing something. A type RoomFindByIdOutput lacks relations defined in Room type. Can possibly setTypeName() cause that?

Types Room and RoomFindByIdOutput are different. So you need to addRelation two times for every type.

Or maybe try to replace output type for 'findById' resolver by Room type:

typeComposer.getResolver('findById').setType('Room'); // will replace existed `RoomFindByIdOutput` by `Room` type

PS.
Codestyle fix: you don't need to call getFieldConfig graphql-compose may do it implicitly 😉

schemaComposer.Query.addFields({
-  Room: typeComposer.getResolver('search').getFieldConfig(),
+  Room: typeComposer.getResolver('search'),
-  RoomById: typeComposer.getResolver('findById').getFieldConfig()
+  RoomById: typeComposer.getResolver('findById')
})

Thank you @nodkz , setType('Room') did the trick.