Skip to content

Interfaces

Stanislav Silin edited this page Apr 17, 2023 · 1 revision

For example, we have the following schema:

schema {
  query: Query
}

interface IFigure {
  perimeter: Float!
}

type Circle implements IFigure {
  center: Point!
  radius: Float!
  perimeter: Float!
}

type Point implements IFigure {
  x: Float!
  y: Float!
  perimeter: Float!
}

type Square implements IFigure {
  topLeft: Point!
  bottomRight: Point!
  perimeter: Float!
}

type Query {
  figures: [IFigure!]!
}

To get figures we can use next request:

 var response = await qlClient.Query(static q => q
    .Figures(o => new
    {
        o.Perimeter,
        Circle = o.On<Circle>().Select(oo => new
        {
            oo.Radius,
            Center = oo.Center(ooo => new { ooo.X, ooo.Y })
        }),
        Square = o.On<Square>().Select(oo => new
        {
            TopLeft = oo.TopLeft(ooo => new { ooo.X, ooo.Y }),
            BottomRight = oo.BottomRight(ooo => new { ooo.X, ooo.Y })
        })
    }));

Console.WriteLine(JsonSerializer.Serialize(response)); 
// {
//   "Query": "query { figures { perimeter ... on Circle { radius center { x y }  } ... on Square { topLeft { x y }  topLeft { x y }  } __typename } }",
//   "Data": [
//     {
//       "Perimeter": 6.2831854820251465,
//       "Circle": {
//         "Radius": 1,
//         "Center": {
//           "X": 1,
//           "Y": 1
//         }
//       }
//     },
//     {
//       "Perimeter": 40,
//       "Square": {
//         "TopLeft": {
//           "X": 1,
//           "Y": 1
//         },
//         "BottomRight": {
//           "X": 11,
//           "Y": 11
//         }
//       }
//     }
//   ]
// }