Fork me on GitHub
#graphql
<
2017-08-18
>
abarylko00:08:06

I'm starting with GraphQL and I was wondering if it is possible that given TypeA, TypeB and TypeC to have a response that is like a tuple (in terms of fields) for (TypeA, TypeB, TypeC)

theikkila10:08:36

hmm, can you give more detailed example of that?

abarylko16:08:58

@theikkila Let's say that I have an object School with fields Address, Name, Capacity and another object SportField with fields Length, Width, Seats I would like to have a query that returns either one of them or both

domkm17:08:46

@abarylko Look into interfaces and unions

abarylko17:08:29

Actually unions will let me return one of them at a time, and I need a join

abarylko17:08:55

interfaces also let me send one concrete object at a time, and not a join

domkm17:08:59

You can return a list of interface/union

theikkila17:08:36

so you want to do query like this?

{
  allSportFields {
    id
    seats
    width
    length
  }
  allSchools {
    id
    name
    address
    capacity
  }
}
and want it return something like this: https://pastebin.com/8sBrk6rp

hmaurer17:08:49

@abarylko you could have a return type for your query with two fields, school and sportField?

hmaurer17:08:27

e.g.

type MyQueryResult {
  school: School
  sportField: SportField
}

hmaurer17:08:33

each of which can be null

hmaurer17:08:24

so your query can return one, or both

hmaurer17:08:37

Basically it’s a tuple with named fields

abarylko17:08:46

Yes, I think more like a tuple

abarylko17:08:31

However I wonder if it should be like a relation inside School

hmaurer17:08:36

GraphQL doesn’t have an actual tuple type, and it doesn’t have generics

abarylko17:08:40

because my goal is to join them

hmaurer17:08:42

so this is as close to it as you can get

hmaurer17:08:14

if a sports field belongs to a school, I think it makes sense to have it as a relation

hmaurer17:08:20

it depends on your application/data model

abarylko17:08:46

I think I'll explore that route

hmaurer17:08:17

if your model is “a school may or may not have a sports field”, then having it as a relation on “School” makes sense

abarylko17:08:21

if not I may try your suggestion @hmaurer

abarylko17:08:51

Yes, I need to validate/review better what kind of relationships I have

abarylko17:08:13

appreciate the help @hmaurer and @theikkila !

hmaurer17:08:44

@abarylko You’re welcome 🙂 Don’t hesitate to ask if you have any questions on your data model