Skip to content

Version 0.9.0

Compare
Choose a tag to compare
@muuki88 muuki88 released this 10 Aug 09:57
· 36 commits to main since this release
6203fa7

Interfaces & types

Pull Request: #45

The ApolloSourceGenerator now generates an additional file Interfaces.scala with the following shape:

object types {
   // contains all defined types like enums and aliases
}
// all used fragments and interfaces are generated as traits here

We generate the types, aliases and interfaces by

  1. Merging all graphql documents into a single one. Sangria takes care of fragment deduplication. The behaviour when a fragment has the same name but different contents is at the moment unspecified.
  2. From the "master document" we generate all interfaces, types and aliases that are actually used
  3. We import types._ in all generated code. The interfaces are already in the package scope. so no additional import is required

Use case

Share common business logic around a fragment that shouldn't be a directive

You can now do this by defining a fragment and include it in every query that
requires to apply this logic. sbt-graphql will generate the common trait?,
all generated case classes will extend this fragment trait.

Limitations

You need to copy the fragments into every graphql query that should use it.
If you have a lot of queries that reuse the fragment and you want to apply changes,
this is cumbersome.

You cannot nest fragments. The code generation isn't capable of naming the nested data structure. This means that you need create fragments for every nesting.

Invalid

query HeroNestedFragmentQuery {
  hero {
    ...CharacterInfo
  }
  human(id: "Lea") {
    ...CharacterInfo
  }
}

# This will generate code that may compile, but is not usable
fragment CharacterInfo on Character {
    name
     friends {
        name
    }
}

correct

query HeroNestedFragmentQuery {
  hero {
    ...CharacterInfo
  }
  human(id: "Lea") {
    ...CharacterInfo
  }
}

# create a fragment for the nested query
fragment CharacterFriends on Character {
    name
}

fragment CharacterInfo on Character {
    name
    friends {
        ...CharacterFriends
    }
}