{"id":3497,"date":"2022-01-13T12:34:44","date_gmt":"2022-01-13T12:34:44","guid":{"rendered":"http:\/\/the-codest.localhost\/blog\/polymorphism-in-ruby-and-graphql\/"},"modified":"2026-04-28T14:06:49","modified_gmt":"2026-04-28T14:06:49","slug":"polymorfismus-v-jazycich-ruby-a-graphql","status":"publish","type":"post","link":"https:\/\/thecodest.co\/cs\/blog\/polymorphism-in-ruby-and-graphql\/","title":{"rendered":"Polymorfismus v jazyce Ruby a GraphQL"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Polymorphism<\/h2>\n\n\n\n<p><strong>Polymorphism<\/strong> is a key component of <strong>object-oriented programming<\/strong>. To simplify things, it is based on the fact that objects of different classes have access to the same interface, which means that we can expect from each of them the same functionality but not necessarily implemented in the same way. In <strong><a href=\"https:\/\/thecodest.co\/cs\/blog\/hire-ror-developer\/\">Ruby<\/a> developers<\/strong> can obtain <strong>polymorphism<\/strong> in three ways:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inheritance<\/h3>\n\n\n\n<p><strong>Inheritance<\/strong> consists in creating a parent class and child classes (i.e., inheriting from the parent class). Subclasses receive the functionality of the parent class and also allow you to change and add a functionality.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">class Document\n  attr_reader :name\nend\n\nclass PDFDocument &lt; Document\n  def extension\n    :pdf\n  end\nend\n\nclass ODTDocument &lt; Document\n  def extension\n    :odt\n  end\nend<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Modules<\/h3>\n\n\n\n<p>Modules in <strong>Ruby<\/strong> have many uses. One of them is mixins (read more about mixins in <a href=\"https:\/\/thecodest.co\/blog\/ruby-vs-python\">The Ultimate Breakdown: Ruby vs. Python<\/a>). Mixins in Ruby can be used similarly to interfaces in other <a href=\"https:\/\/thecodest.co\/cs\/blog\/top-programming-languages-to-build-e-commerce\/\">programming languages<\/a> (e.g., in <a href=\"https:\/\/thecodest.co\/cs\/blog\/find-the-right-java-developer-now\/\">Java<\/a>), for instance, you can define in them methods common to objects that will contain a given mixin. It is a good practice to include read-only methods in modules, so methods that will not modify the state of this object.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Taxable\n  def tax\n\n     price * 0.23\n  end\nend\n\nclass Car\n  include Taxable\n attr_reader :price\nend\n\nclass Book\n  include Taxable\n\n attr_reader :price\nend<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Duck typing<\/h3>\n\n\n\n<p>This is one of the key features of dynamically typed languages. The name comes from the famous test if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. The <strong>programmer<\/strong> does not have to be interested to which class the given object belongs. What is important are the methods that can be called on this object.<\/p>\n\n\n\n<p>Using the classes defined in the example above:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">class Car\n  attr_reader :price\n\n def initialize(price)\n    @price = price\n   end\nend\n\nclass Book\n  attr_reader :price\n\n def initialize(price)\n    @price = price\n  end\nend\n\ncar = Car.new(20.0)\nbook = Book.new(10.0)\n\n[car, book].map(&amp;:price<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">GrapQL<\/h3>\n\n\n\n<p><strong>GraphQL<\/strong> is a relatively new query language for APIs. Its advantages include the fact that it has a very simple syntax and, in addition, the client decides what exactly they want to get as each customer gets exactly what they want and nothing else.<\/p>\n\n\n\n<p>Sample query in <strong>GraphQL<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">{\n  allUsers {\n     users {\n        id\n        login\n        email\n\n       }\n     }\n   }<\/code><\/pre>\n\n\n\n<p>Sample response:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">{\n  \"allUsers\": {\n    \"users\": [\n     {\n        \"id\": 1,\n        \"login\": \"user1\",\n        \"email\": \"user1@te.st\"\n      },\n      {\n        \"id\": 2,\n        \"login\": \"user2\",\n        \"email\": \"user2@te.st\"\n      },\n    ]\n  }\n}<\/code><\/pre>\n\n\n\n<p>This is probably all we need to know at the moment. So, let&#8217;s get to the point.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Presentation of the problem<\/h2>\n\n\n\n<p>To best understand the problem and its solution, let&#8217;s create an example. It would be good if the example was both original and fairly down-to-earth. One that each of <a href=\"https:\/\/thecodest.co\/cs\/blog\/why-us-companies-are-opting-for-polish-developers\/\">us<\/a> can encounter someday. How about&#8230; animals? Yes! Great idea!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/app\/uploads\/2024\/05\/animals_graphql_ruby.png\" alt=\"ruby and grapql polymorphism - animals\" title=\"ruby and grapql polymorphism - animals example\"\/><\/figure>\n\n\n\n<p>Suppose we have a backend application written in <strong><a href=\"https:\/\/thecodest.co\/cs\/case-studies\/delivering-ruby-on-rails-talent-for-fast-integration\/\">Ruby on Rails<\/a><\/strong>. It is already adapted to handle the above scheme. Let&#8217;s also assume that we already have <strong>GraphQL<\/strong> configured. We want to enable the client to make an inquiry within the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">{\n allZoos : {\n    zoo: {\n      name\n      city\n      animals: {\n        ...\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>What should be put instead of the three dots in order to obtain the missing information \u2013 we will find out later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<p>Below I will present the steps needed to achieve the goal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding a query to QueryType<\/h3>\n\n\n\n<p>First, you need to define what exactly the query allZoos means. To do this, we need to visit the file<code>app\/graphql\/types\/query_type.rb<\/code> and define the query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">   module Types\n      class QueryType &lt; Types::BaseObject\n       field :all_zoos, [Types::ZooType], null: false\n\n       def all_zoos\n          Zoo.all\n       end\n    end\n end<\/code><\/pre>\n\n\n\n<p>The query is already defined. Now it&#8217;s time to define the return types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Definition of types<\/h3>\n\n\n\n<p>The first type required will be ZooType. Let&#8217;s define it in the file <code>app\/graphql\/types\/ zoo_type.rb<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Types\n  class ZooType &lt; Types::BaseObject\n    field :name, String, null: false\n    field :city, String, null: false\n    field :animals, [Types::AnimalType], null: false\n  end\nend<\/code><\/pre>\n\n\n\n<p>Now it&#8217;s time to define the type AnimalType:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Types\n  class AnimalType &lt; Types::BaseUnion\n   possible_types ElephantType, CatType, DogType\n\n     def self.resolve_type(obj, ctx)\n       if obj.is_a?(Elephant)\n          ElephantType\n       elsif obj.is_a?(Cat)\n         CatType\n       elsif obj.is_a?(Dog)\n        DogType\n      end\n    end\n  end\nend<\/code><\/pre>\n\n\n\n<p>What do we see in the <a href=\"https:\/\/thecodest.co\/cs\/dictionary\/what-is-code-refactoring\/\">code<\/a> above?<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The AnimalType inherits from <code>Types::BaseUnion<\/code>.<\/li>\n\n\n\n<li>We have to list all types that can make up a given union.<br>3.We override the function <code>self.resolve_object(obj, ctx),<\/code>which must return the type of a given object.<\/li>\n<\/ol>\n\n\n\n<p>The next step is to define the types of animals. However, we know that some fields are common to all animals. Let&#8217;s include them in type AnimalInterface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Types\n  module AnimalInterface\n    include Types::BaseInterface\n\n    field :name, String, null: false\n    field :age, Integer, null: false\n  end\nend<\/code><\/pre>\n\n\n\n<p>Having this interface, we can proceed to define the types of specific animals:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Types\n  class ElephantType &lt; Types::BaseObject\n    implements Types::AnimalInterface\n\n    field :trunk_length, Float, null: false\n  end\nend\n\nmodule Types\n  class CatType &lt; Types::BaseObject\n   implements Types::AnimalInterface\n\n   field :hair_type, String, null: false\n  end\nend\n\nmodule Types\n  class DogType &lt; Types::BaseObject\n    implements Types::AnimalInterface\n\n     field :breed, String, null: false\n  end\nend<\/code><\/pre>\n\n\n\n<p>That&#8217;s it! Ready! One last question: how can we use what we have done from the client\u2019s side?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Building the query<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">{\n allZoos : {\n   zoo: {\n      name\n      city\n      animals: {\n        __typename\n\n        ... on ElephantType {\n          name\n          age\n          trunkLength\n        }\n\n         ... on CatType {\n          name\n          age\n          hairType\n         }\n         ... on DogType {\n          name\n          age\n          breed\n         }\n       }\n     }\n   }\n }<\/code><\/pre>\n\n\n\n<p>We can use an additional __typename field here, which will return the exact type of a given element (e.g., CatType). What will a sample answer look like?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">{\n  \"allZoos\": [\n\n   {\n      \"name\": \"Natura Artis Magistra\",\n      \"city\": \"Amsterdam\",\n      \"animals\": [\n        {\n          \"__typename\": \"ElephantType\"\n          \"name\": \"Franco\",\n          \"age\": 28,\n          \"trunkLength\": 9.27\n         },\n         {\n         \"__typename\": \"DogType\"\n         \"name\": \"Jack\",\n         \"age\": 9,\n         \"breed\": \"Jack Russell Terrier\"\n        },\n      ]\n    }\n  ]\n} <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Analysis<\/h3>\n\n\n\n<p>One drawback of this approach is apparent. In the query, we must enter the name and age in each type, even though we know that all animals have these fields. This is not bothersome when the collection contains completely different objects. In this case, however, the animals share almost all fields. Can it be improved somehow?<\/p>\n\n\n\n<p>Of course! We make the first change in the file <code>app\/graphql\/types\/zoo_type.rb<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Types\n  class ZooType &lt; Types::BaseObject\n    field :name, String, null: false\n    field :city, String, null: false\n    field :animals, [Types::AnimalInterface], null: false\n  end\nend<\/code><\/pre>\n\n\n\n<p>We no longer need the union we have defined before. We change <code>Types::AnimalType<\/code> to <code>Types::AnimalInterface<\/code>.<\/p>\n\n\n\n<p>The next step is to add a function that returns a type from <code>Types :: AnimalInterface<\/code> and also add a list of orphan_types, so types that are never directly used:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">module Types\n  module AnimalInterface\n    include Types::BaseInterface\n\n   field :name, String, null: false\n   field :age, Integer, null: false\n\n   definition_methods do\n      def resolve_type(obj, ctx)\n        if obj.is_a?(Elephant)\n          ElephantType\n        elsif obj.is_a?(Cat)\n          CatType\n        elsif obj.is_a?(Dog)\n          DogType\n        end\n      end\n    end\n    orphan_types Types::ElephantType, Types::CatType, Types::DogType\n  end\nend<\/code><\/pre>\n\n\n\n<p>Thanks to this simple procedure, the query has a less complex form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">{\n  allZoos : {\n   zoo: {\n      name\n      city\n      animals: {\n        __typename\n        name\n        age\n\n       ... on ElephantType {\n          trunkLength\n\n       }\n       ... on CatType {\n          hairType\n\n       }\n       ... on DogType {\n          breed\n\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p><strong>GraphQL<\/strong> is a really great solution. If you don&#8217;t know it yet, try it. Trust me, it&#8217;s worth it. It does a great job in solving problems appearing in, for example, REST APIs. As I showed above, <strong>polymorphism<\/strong> is not a real obstacle for it. I presented two methods to tackle it.<br>Reminder:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you operate on a list of objects with a common base or a common interface \u2013 use the interfaces,<\/li>\n\n\n\n<li>If you operate on a list of objects with a different structure, use a different interface \u2013 use the union<\/li>\n<\/ul>\n\n\n\n<p><strong>Read More<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/graphql-ruby-what-about-performance\">GraphQL Ruby. What about performance?<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/rails-and-other-means-of-transport\">Rails and Other Means of Transport<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/rails-development-with-tmux-vim-fzf-ripgrep\">Rails Development with TMUX, Vim, Fzf + Ripgrep<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>V tomto \u010dl\u00e1nku p\u0159edstav\u00edm pou\u017eit\u00ed polymorfismu v jazyce GraphQL. Ne\u017e v\u0161ak za\u010dnu, je vhodn\u00e9 p\u0159ipomenout, co polymorfismus a GraphQL jsou.<\/p>","protected":false},"author":2,"featured_media":3498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3497","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Polymorphism in Ruby and GraphQL - The Codest<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/cs\/blog\/polymorfismus-v-jazycich-ruby-a-graphql\/\" \/>\n<meta property=\"og:locale\" content=\"cs_CZ\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Polymorphism in Ruby and GraphQL\" \/>\n<meta property=\"og:description\" content=\"In this article, I will present the use of polymorphism in GraphQL. Before I start, however, it is worth recalling what polymorphism and GraphQL are.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/cs\/blog\/polymorfismus-v-jazycich-ruby-a-graphql\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-13T12:34:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T14:06:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/polymorphism_in_ruby_and_graphql.png\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"thecodest\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"thecodest\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minut\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/\"},\"author\":{\"name\":\"thecodest\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\"},\"headline\":\"Polymorphism in Ruby and GraphQL\",\"datePublished\":\"2022-01-13T12:34:44+00:00\",\"dateModified\":\"2026-04-28T14:06:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/\"},\"wordCount\":881,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/polymorphism_in_ruby_and_graphql.png\",\"articleSection\":[\"Software Development\"],\"inLanguage\":\"cs\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/\",\"name\":\"Polymorphism in Ruby and GraphQL - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/polymorphism_in_ruby_and_graphql.png\",\"datePublished\":\"2022-01-13T12:34:44+00:00\",\"dateModified\":\"2026-04-28T14:06:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#breadcrumb\"},\"inLanguage\":\"cs\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/polymorphism_in_ruby_and_graphql.png\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/polymorphism_in_ruby_and_graphql.png\",\"width\":960,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/polymorphism-in-ruby-and-graphql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Polymorphism in Ruby and GraphQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"cs\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"width\":144,\"height\":36,\"caption\":\"The Codest\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/pl.linkedin.com\\\/company\\\/codest\",\"https:\\\/\\\/clutch.co\\\/profile\\\/codest\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\",\"name\":\"thecodest\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"caption\":\"thecodest\"},\"url\":\"https:\\\/\\\/thecodest.co\\\/cs\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Polymorfismus v jazyce Ruby a GraphQL - The Codest","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thecodest.co\/cs\/blog\/polymorfismus-v-jazycich-ruby-a-graphql\/","og_locale":"cs_CZ","og_type":"article","og_title":"Polymorphism in Ruby and GraphQL","og_description":"In this article, I will present the use of polymorphism in GraphQL. Before I start, however, it is worth recalling what polymorphism and GraphQL are.","og_url":"https:\/\/thecodest.co\/cs\/blog\/polymorfismus-v-jazycich-ruby-a-graphql\/","og_site_name":"The Codest","article_published_time":"2022-01-13T12:34:44+00:00","article_modified_time":"2026-04-28T14:06:49+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/polymorphism_in_ruby_and_graphql.png","type":"image\/png"}],"author":"thecodest","twitter_card":"summary_large_image","twitter_misc":{"Written by":"thecodest","Est. reading time":"5 minut"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#article","isPartOf":{"@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/"},"author":{"name":"thecodest","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76"},"headline":"Polymorphism in Ruby and GraphQL","datePublished":"2022-01-13T12:34:44+00:00","dateModified":"2026-04-28T14:06:49+00:00","mainEntityOfPage":{"@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/"},"wordCount":881,"commentCount":0,"publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"image":{"@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/polymorphism_in_ruby_and_graphql.png","articleSection":["Software Development"],"inLanguage":"cs","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/","url":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/","name":"Polymorfismus v jazyce Ruby a GraphQL - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#primaryimage"},"image":{"@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/polymorphism_in_ruby_and_graphql.png","datePublished":"2022-01-13T12:34:44+00:00","dateModified":"2026-04-28T14:06:49+00:00","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#breadcrumb"},"inLanguage":"cs","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/"]}]},{"@type":"ImageObject","inLanguage":"cs","@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#primaryimage","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/polymorphism_in_ruby_and_graphql.png","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/polymorphism_in_ruby_and_graphql.png","width":960,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/blog\/polymorphism-in-ruby-and-graphql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"Polymorphism in Ruby and GraphQL"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"The Codest","description":"","publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"cs"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"cs","@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","width":144,"height":36,"caption":"The Codest"},"image":{"@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/pl.linkedin.com\/company\/codest","https:\/\/clutch.co\/profile\/codest"]},{"@type":"Person","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76","name":"thecodest","image":{"@type":"ImageObject","inLanguage":"cs","@id":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","caption":"thecodest"},"url":"https:\/\/thecodest.co\/cs\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/comments?post=3497"}],"version-history":[{"count":15,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3497\/revisions"}],"predecessor-version":[{"id":7948,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3497\/revisions\/7948"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media\/3498"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media?parent=3497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/categories?post=3497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/tags?post=3497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}