forked from Gusto/apollo-federation-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.rb
77 lines (63 loc) · 1.42 KB
/
products.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
require_relative './graphql_server'
# extend type Query {
# topProducts(first: Int = 5): [Product]
# }
# type Product @key(fields: "upc") {
# upc: String!
# name: String
# price: Int
# weight: Int
# }
PRODUCTS = [
{
upc: '1',
name: 'Table',
price: 899,
weight: 100,
},
{
upc: '2',
name: 'Couch',
price: 1299,
weight: 1000,
},
{
upc: '3',
name: 'Chair',
price: 54,
weight: 50,
},
].freeze
class Product < BaseObject
key fields: :upc
field :upc, String, null: false
field :name, String, null: true
field :price, Int, null: true
field :weight, Int, null: true
def self.resolve_reference(reference, _context)
PRODUCTS.find { |product| product[:upc] == reference[:upc] }
end
end
class Query < BaseObject
field :top_products, [Product], null: false do
argument :first, Int, required: false, default_value: 5
end
def top_products(first:)
PRODUCTS.slice(0, first)
end
end
class ProductSchema < GraphQL::Schema
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('1.12.0')
use GraphQL::Execution::Interpreter
use GraphQL::Analysis::AST
end
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.3.0')
use ApolloFederation::Tracing
else
trace_with ApolloFederation::Tracing::Tracer
end
include ApolloFederation::Schema
query(Query)
end
GraphQLServer.run(ProductSchema, Port: 5003)