Gusto/apollo-federation-ruby

How to test/confirm federation implementation?

kented opened this issue · 6 comments

Thanks for this gem, I think it will do just what I need. I've added the gem to my app and followed the steps for updating my schema and classes. Is there any documentation in regard to how I should test to confirm my changes?

@kented I don't think the project has documentation in regards to that, probably the best approach you can take is integrating your API with the Apollo Gateway and test your queries and mutations. You can also query the _service and _entities fields directly. You can take a look at the tests on how to do that.

Managed to test my changes. If it'd be helpful to others, I might be able to contribute some documentation in regard to what an rspec test could look like for apollo federation integration.

@kented Would you mind sharing your approach?

This is how I test the resolver for a model called 'User' in one of the apps I built:

gql_query = 
  <<~GQL
    query entities {
      _entities(representations: [ {__typename: User, id: "123"} ]) {
        ...on User { 
          id
          first_name
          last_name
        }
      }
    }
  GQL

User.create(id: 123, first_name: 'David', last_name: 'HH')
result = DiscussionSchema.execute(gql_query, context: context)
expected_result = [{ 'id' => '123', 'first_name' => 'David', 'last_name' => 'HH' }]
expect(result.dig('data', '_entities')).to eq(expected_result)

I extracted this answer from a more complex test suite I wrote several months ago, so it may contain some errors. Hopefully it provides enough guidance to be helpful.

The gem's test suite also has some examples you can see here: https://github.com/Gusto/apollo-federation-ruby/blob/master/spec/apollo-federation/entities_field_spec.rb#L164

Hi @kented , your approach was how I tested one of our internal app that's using federation. It works well but can be pretty confusing to someone who has less exposure to federation. But I do like that it creates a relatively honest test.

Maybe in the future, the maintainers might consider some test helpers. For now, I'll try to add a PR that highlights this as a testing strategy, save future readers some time hopefully.

Nice. I think what you've added to the README will be helpful to folks who are wondering how to test their implementation.