graphql icon indicating copy to clipboard operation
graphql copied to clipboard

If an output object is not referenced as an output type it is not inserted into the schema

Open grkek opened this issue 3 years ago • 0 comments

An example like this:

require "graphql"

@[GraphQL::Object]
class Wallet < GraphQL::BaseObject
  @[GraphQL::Field]
  property id : String

  def initialize(@id : String)
  end
end

@[GraphQL::Object]
class User < GraphQL::BaseObject
  @[GraphQL::Field]
  property wallets : Array(Wallet)

  def initialize(@wallets : Array(Wallet))
  end
end

@[GraphQL::Object]
class Query < GraphQL::BaseQuery
  @[GraphQL::Field]
  def list_users : Array(User)
    [User.new([Wallet.new("1"), Wallet.new("2")])] of User
  end
end

schema = GraphQL::Schema.new(Query.new)

puts schema.document.to_s

Will generate a schema document like this:

type Query {
  listUsers: [User!]!
}

"The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text."
scalar String

type User {
  wallets: [Wallet!]!
}

type __Directive {
  args: [__InputValue!]!
  description: String
  locations: [String!]!
  name: String!
}

type __EnumValue {
  deprecationReason: String
  description: String
  isDeprecated: Boolean!
  name: String!
}

type __Field {
  args: [__InputValue!]!
  deprecationReason: String
  description: String
  isDeprecated: Boolean!
  name: String!
  type: __Type!
}

type __InputValue {
  defaultValue: String
  description: String
  name: String!
  type: __Type!
}

type __Schema {
  directives: [__Directive!]!
  mutationType: __Type
  queryType: __Type!
  subscriptionType: __Type
  types: [__Type!]!
}

type __Type {
  description: String
  enumValues(includeDeprecated: Boolean! = false): [__EnumValue!]
  fields(includeDeprecated: Boolean! = false): [__Field!]
  inputFields: [__InputValue!]
  interfaces: [__Type!]
  kind: __TypeKind!
  name: String
  ofType: __Type
  possibleTypes: [__Type!]
}

enum __TypeKind {
  ENUM
  INPUT_OBJECT
  INTERFACE
  LIST
  NON_NULL
  OBJECT
  SCALAR
  UNION
}

The Wallet class clearly has inherited from the object, but it is not serialised into the schema.

grkek avatar Nov 19 '22 09:11 grkek