spec/graphql/schema/printer_spec.rb in graphql-1.3.0 vs spec/graphql/schema/printer_spec.rb in graphql-1.4.0
- old
+ new
@@ -334,49 +334,53 @@
end
end
describe ".print_schema" do
it "includes schema definition when query root name doesn't match convention" do
- schema.query.name = 'MyQueryRoot'
+ custom_query = schema.query.redefine(name: "MyQueryRoot")
+ custom_schema = schema.redefine(query: custom_query)
expected = <<SCHEMA
schema {
query: MyQueryRoot
mutation: Mutation
subscription: Subscription
}
SCHEMA
- assert_match expected, GraphQL::Schema::Printer.print_schema(schema)
+ assert_match expected, GraphQL::Schema::Printer.print_schema(custom_schema)
end
it "includes schema definition when mutation root name doesn't match convention" do
- schema.mutation.name = 'MyMutationRoot'
+ custom_mutation = schema.mutation.redefine(name: "MyMutationRoot")
+ custom_schema = schema.redefine(mutation: custom_mutation)
+
expected = <<SCHEMA
schema {
query: Query
mutation: MyMutationRoot
subscription: Subscription
}
SCHEMA
- assert_match expected, GraphQL::Schema::Printer.print_schema(schema)
+ assert_match expected, GraphQL::Schema::Printer.print_schema(custom_schema)
end
it "includes schema definition when subscription root name doesn't match convention" do
- schema.subscription.name = 'MySubscriptionRoot'
+ custom_subscription = schema.subscription.redefine(name: "MySubscriptionRoot")
+ custom_schema = schema.redefine(subscription: custom_subscription)
expected = <<SCHEMA
schema {
query: Query
mutation: Mutation
subscription: MySubscriptionRoot
}
SCHEMA
- assert_match expected, GraphQL::Schema::Printer.print_schema(schema)
+ assert_match expected, GraphQL::Schema::Printer.print_schema(custom_schema)
end
it "returns the schema as a string for the defined types" do
expected = <<SCHEMA
type Audio {
@@ -472,7 +476,118 @@
sub: [Sub]
}
SCHEMA
assert_equal expected.chomp, GraphQL::Schema::Printer.print_schema(schema)
end
+ end
+
+ it "applies an `only` filter" do
+ expected = <<SCHEMA
+enum Choice {
+ FOO
+ BAR
+}
+
+type Subscription {
+
+}
+
+input Varied {
+ int: Int
+ float: Float
+ bool: Boolean
+ enum: Choice = FOO
+}
+SCHEMA
+
+ only_filter = -> (member, ctx) {
+ case member
+ when GraphQL::ScalarType
+ true
+ when GraphQL::BaseType
+ ctx[:names].include?(member.name)
+ when GraphQL::Argument
+ member.name != "id"
+ else
+ member.deprecation_reason.nil?
+ end
+ }
+
+ context = { names: ["Varied", "Choice", "Subscription"] }
+ assert_equal expected.chomp, schema.to_definition(context: context, only: only_filter)
+ end
+
+
+ it "applies an `except` filter" do
+ expected = <<SCHEMA
+type Audio {
+ id: ID!
+ name: String!
+ duration: Int!
+}
+
+enum Choice {
+ FOO
+ BAR
+}
+
+# A blog comment
+type Comment implements Node {
+ id: ID!
+}
+
+# Autogenerated input type of CreatePost
+input CreatePostInput {
+ # A unique identifier for the client performing the mutation.
+ clientMutationId: String
+ title: String!
+ body: String!
+}
+
+# Autogenerated return type of CreatePost
+type CreatePostPayload {
+ # A unique identifier for the client performing the mutation.
+ clientMutationId: String
+ post: Post
+}
+
+# Media objects
+union Media = Audio
+
+type Mutation {
+ # Create a blog post
+ createPost(input: CreatePostInput!): CreatePostPayload
+}
+
+interface Node {
+ id: ID!
+}
+
+# A blog post
+type Post {
+ id: ID!
+ title: String!
+ body: String!
+ comments: [Comment!]
+}
+
+# The query root of this schema
+type Query {
+ post(
+ # Post ID
+ id: ID!
+ ): Post
+}
+
+type Subscription {
+ post(id: ID!): Post
+}
+SCHEMA
+
+ except_filter = -> (member, ctx) {
+ ctx[:names].include?(member.name) || (member.respond_to?(:deprecation_reason) && member.deprecation_reason)
+ }
+
+ context = { names: ["Varied", "Image", "Sub"] }
+ assert_equal expected.chomp, schema.to_definition(context: context, except: except_filter)
end
end