Graphql relationships: Difference between revisions
From wikinotes
Line 38: | Line 38: | ||
This is a working mental model, needs work }} | This is a working mental model, needs work }} | ||
{{ WARNING | | {{ WARNING | | ||
this might be incorrect. See [[graphql-ruby connections]] }} | this definition might be incorrect. See [[graphql-ruby connections]] }} | ||
{| class="wikitable" | {| class="wikitable" |
Revision as of 16:36, 6 September 2021
Interfaces
In graphql, an interface is a group of fields that multiple objects may use.
A type that implements that interface must define all fields required by the interface.
Note you can implement multiple interfaces.# schema interface Animal { species: String! age: Int! } type Duck implements Animal { species: String! age: Int! quack: String! }You can return an
Animal
(which really returns any of the subtypes).
When referring to fields not part of the return type, you need to specify the object it belongs to.{ animal { species age ... on Duck { quack } } }
Edges/Connections
WARNING:
This is a working mental model, needs work
WARNING:
this definition might be incorrect. See graphql-ruby connections
graphql docs https://graphql.org/learn/pagination/#end-of-list-counts-and-connections explaining connections https://www.apollographql.com/blog/graphql/explaining-graphql-connections/ graphql ruby connections https://graphql-ruby.org/pagination/using_connections.html Connections and Edges are not a part of graphql syntax,
they are a schema design-pattern that is sometimes implemented in API libraries.
- Connection describe a type of connection (ex: between two users)
- Edges are the connection itself
+------+ +------+ +------+ | Post | | User | | User | +------+ +------+ +------+ | | | AuthorConnection FriendConnection Friendconnection | | | +--------+--------+----------------+ | +------+ | User | +------+{ user(id: 123) { friendConnections { edges { user { id name } } } } }# NOTE: UNTESTED type Class { subject: String students( # a connection -- define supplemental query-params age: Int ) StudentConnection! } type StudentConnection { edges: [StudentEdge] nodes: [Student] } type StudentEdge { node: Student } type Student { firstname: String! lastname: String! age: Int! }# untested, probably wrong query MathClassStudents { Class(subject: "Math") { students(first: 2, age: 25) { edges { Student } } } }