Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL - Embeds #898

Open
wants to merge 10 commits into
base: the-future
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/graphql/concerns/optional_embed_description.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module OptionalEmbedDescription
extend ActiveSupport::Concern

AUDIO_DESCRIPTION = 'A URL to an audio file to accompany this object.'
DESCRIPTION = 'A one to two sentence description of your object.'
DETERMINER_DESCRIPTION = %q[
The word that appears before this object's title in a sentence.
An enum of (a, an, the, "", auto). If auto is chosen,
the consumer of your data should chose between "a" or "an". Default is "" (blank).
]
LOCALE_DESCRIPTION = %[
The locale these tags are marked up in.
Of the format language_TERRITORY. Default is en_US.
]
LOCALE_ALTERNATIVE_DESCRIPTION = 'An array of other locales this page is available in.'
SITE_NAME_DESCRIPTION = %[
If your object is part of a larger web site,
the name which should be displayed for the overall site.
]
VIDEO_DESCRIPTION = 'A URL to a video file that complements this object.'
end
toyhammered marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions app/graphql/types/embed/website_embed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Types::Embed::WebsiteEmbed < Types::BaseObject
include OptionalEmbedDescription
implements Types::Interface::RequiredEmbed

field :site_name, String,
description: SITE_NAME_DESCRIPTION,
null: false
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final newline missing. (https://rubystyle.guide#newline-eof)

28 changes: 28 additions & 0 deletions app/graphql/types/interface/required_embed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Types::Interface::RequiredEmbed
toyhammered marked this conversation as resolved.
Show resolved Hide resolved
include Types::Interface::Base
description 'Required fields for an Embed based off the Open Graph protocol'

field :title, String,
null: true,
description: ''

field :kind, String,
null: false,
description: ''

field :description, String,
null: true,
description: ''

field :site, String,
null: true,
description: ''

field :url, String,
null: true,
description: ''

# field :image, Types::EmbedImage,
# null: true,
# description: ''
end
4 changes: 4 additions & 0 deletions app/graphql/types/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Types::Post < Types::BaseObject
null: true,
description: 'The reason why this post was locked.'

field :embed, Types::Union::EmbedItem,
null: true,
description: ''

field :comments, Types::Comment.connection_type,
null: false,
description: 'All comments related to this post.'
Expand Down
5 changes: 5 additions & 0 deletions app/graphql/types/union/embed_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Types::Union::EmbedItem < Types::Union::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good argument to using a union vs just referencing the base embed interface that all embeds inherit from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure. I've always preferred using a union vs referencing the base embed, I think it reads nicer. But both function the same way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading this: https://artsy.github.io/blog/2019/01/14/graphql-union-vs-interface/ I think I prefer Unions.

description 'All the different Embed types'

possible_types Types::Embed::WebsiteEmbed
end