Skip to content

GraphQL

Maxim edited this page Nov 14, 2019 · 4 revisions
String query = @"
    {
        human(id: ""1"") {
          name
          friends {
            name
            appearsIn {
               name
            }
          }
       }
    }
";

//create data adapter
var dataAdapter = new StarWars.StarWarsDataAdapter(false, "test");
//build odata model
IEdmModel edmModel = dataAdapter.BuildEdmModelFromEfCoreModel();
//create graphql query parser
var parser = new OeGraphqlParser(edmModel);

//get graphql result
ExecutionResult executionResult = await parser.Execute(query);
//serialize json
using (var stream = new MemoryStream())
{
    await new DocumentWriter(true).WriteAsync(stream, executionResult);
    stream.Position = 0;
    using (var reader = new StreamReader(stream))
        Console.WriteLine(reader.ReadToEnd());
}

GraphQL query translate to OData query:
Human?$filter=Id eq '1'&$select=Name&$expand=Friends($select=Name;$expand=AppearsIn($select=Name))

OData query translate to SQL (SQLite):

SELECT
"h"."Name" AS "Item1",
"h"."Id" AS "Item2",
CASE
    WHEN "t"."Id" IS NULL
    THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END,
"t"."Name" AS "Item10",
"t"."Id" AS "Item20",
CASE
    WHEN "EpisodeEnum"."Value" IS NULL
    THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END,
"EpisodeEnum"."Name" AS "Item11",
"EpisodeEnum"."Value" AS "Item21"
FROM "Hero" AS "h"
LEFT JOIN "HeroToHero" AS "CharacterToCharacter" ON "h"."Id" = "CharacterToCharacter"."CharacterId"
LEFT JOIN (
    SELECT "Hero".*
    FROM "Hero" AS "Hero"
    WHERE "Hero"."CharacterType" IN (1, 2)
) AS "t" ON "CharacterToCharacter"."FriendId" = "t"."Id"
LEFT JOIN "HeroToEpisode" AS "CharacterToEpisode" ON "t"."Id" = "CharacterToEpisode"."CharacterId"
LEFT JOIN "Episodes" AS "EpisodeEnum" ON "CharacterToEpisode"."EpisodeId" = "EpisodeEnum"."Value"
WHERE ("h"."CharacterType" = 1) AND ("h"."Id" = @__Item1_0)

GraphQL test