Skip to content

Commit ecdd26a

Browse files
committed
Adds descriptions to the schema language via docblocks
This adds descriptions to the `buildASTSchema` (string->schema) and `schemaPrinter` (schema->string) via walking the previous full-line contiguous comment block. This is dependent on #463.
1 parent 8a98799 commit ecdd26a

File tree

4 files changed

+301
-46
lines changed

4 files changed

+301
-46
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ type Hello {
6666
expect(output).to.equal(body);
6767
});
6868

69+
it('Supports descriptions', () => {
70+
const body = `
71+
schema {
72+
query: Hello
73+
}
74+
75+
# This is a directive
76+
directive @foo(
77+
78+
# It has an argument
79+
arg: Int
80+
) on FIELD
81+
82+
# With an enum
83+
enum Color {
84+
RED
85+
86+
# Not a creative color
87+
GREEN
88+
BLUE
89+
}
90+
91+
# What a great type
92+
type Hello {
93+
94+
# And a field to boot
95+
str: String
96+
}
97+
`;
98+
const output = cycleOutput(body);
99+
expect(output).to.equal(body);
100+
});
69101

70102
it('Maintains @skip & @include', () => {
71103
const body = `

src/utilities/__tests__/schemaPrinter-test.js

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ type Root {
587587
`);
588588
});
589589

590-
it('Print Introspection Schema', () => {
590+
it.only('Print Introspection Schema', () => {
591591
const Root = new GraphQLObjectType({
592592
name: 'Root',
593593
fields: {
@@ -601,50 +601,116 @@ schema {
601601
query: Root
602602
}
603603
604-
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
604+
# Directs the executor to include this field or fragment only when the `if` argument is true.
605+
directive @include(
605606
606-
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
607+
# Included when true.
608+
if: Boolean!
609+
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
607610
608-
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
611+
# Directs the executor to skip this field or fragment when the `if` argument is true.
612+
directive @skip(
609613
614+
# Skipped when true.
615+
if: Boolean!
616+
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
617+
618+
# Marks an element of a GraphQL schema as no longer supported.
619+
directive @deprecated(
620+
621+
# Explains why this element was deprecated, usually also including a suggestion
622+
# for how to access supported similar data. Formatted in
623+
# [Markdown](https://daringfireball.net/projects/markdown/).
624+
reason: String = "No longer supported"
625+
) on FIELD_DEFINITION | ENUM_VALUE
626+
627+
# A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
628+
#
629+
# In some cases, you need to provide options to alter GraphQL's execution behavior
630+
# in ways field arguments will not suffice, such as conditionally including or
631+
# skipping a field. Directives provide this by describing additional information
632+
# to the executor.
610633
type __Directive {
611634
name: String!
612635
description: String
613636
locations: [__DirectiveLocation!]!
614637
args: [__InputValue!]!
615-
onOperation: Boolean! @deprecated(reason: "Use \`locations\`.")
616-
onFragment: Boolean! @deprecated(reason: "Use \`locations\`.")
617-
onField: Boolean! @deprecated(reason: "Use \`locations\`.")
638+
onOperation: Boolean! @deprecated(reason: "Use `locations`.")
639+
onFragment: Boolean! @deprecated(reason: "Use `locations`.")
640+
onField: Boolean! @deprecated(reason: "Use `locations`.")
618641
}
619642
643+
# A Directive can be adjacent to many parts of the GraphQL language, a
644+
# __DirectiveLocation describes one such possible adjacencies.
620645
enum __DirectiveLocation {
646+
647+
# Location adjacent to a query operation.
621648
QUERY
649+
650+
# Location adjacent to a mutation operation.
622651
MUTATION
652+
653+
# Location adjacent to a subscription operation.
623654
SUBSCRIPTION
655+
656+
# Location adjacent to a field.
624657
FIELD
658+
659+
# Location adjacent to a fragment definition.
625660
FRAGMENT_DEFINITION
661+
662+
# Location adjacent to a fragment spread.
626663
FRAGMENT_SPREAD
664+
665+
# Location adjacent to an inline fragment.
627666
INLINE_FRAGMENT
667+
668+
# Location adjacent to a schema definition.
628669
SCHEMA
670+
671+
# Location adjacent to a scalar definition.
629672
SCALAR
673+
674+
# Location adjacent to an object type definition.
630675
OBJECT
676+
677+
# Location adjacent to a field definition.
631678
FIELD_DEFINITION
679+
680+
# Location adjacent to an argument definition.
632681
ARGUMENT_DEFINITION
682+
683+
# Location adjacent to an interface definition.
633684
INTERFACE
685+
686+
# Location adjacent to a union definition.
634687
UNION
688+
689+
# Location adjacent to an enum definition.
635690
ENUM
691+
692+
# Location adjacent to an enum value definition.
636693
ENUM_VALUE
694+
695+
# Location adjacent to an input object type definition.
637696
INPUT_OBJECT
697+
698+
# Location adjacent to an input object field definition.
638699
INPUT_FIELD_DEFINITION
639700
}
640701
702+
# One possible value for a given Enum. Enum values are unique values, not a
703+
# placeholder for a string or numeric value. However an Enum value is returned in
704+
# a JSON response as a string.
641705
type __EnumValue {
642706
name: String!
643707
description: String
644708
isDeprecated: Boolean!
645709
deprecationReason: String
646710
}
647711
712+
# Object and Interface types are described by a list of Fields, each of which has
713+
# a name, potentially a list of arguments, and a return type.
648714
type __Field {
649715
name: String!
650716
description: String
@@ -654,21 +720,47 @@ type __Field {
654720
deprecationReason: String
655721
}
656722
723+
# Arguments provided to Fields or Directives and the input fields of an
724+
# InputObject are represented as Input Values which describe their type and
725+
# optionally a default value.
657726
type __InputValue {
658727
name: String!
659728
description: String
660729
type: __Type!
730+
731+
# A GraphQL-formatted string representing the default value for this input value.
661732
defaultValue: String
662733
}
663734
735+
# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all
736+
# available types and directives on the server, as well as the entry points for
737+
# query, mutation, and subscription operations.
664738
type __Schema {
739+
740+
# A list of all types supported by this server.
665741
types: [__Type!]!
742+
743+
# The type that query operations will be rooted at.
666744
queryType: __Type!
745+
746+
# If this server supports mutation, the type that mutation operations will be rooted at.
667747
mutationType: __Type
748+
749+
# If this server support subscription, the type that subscription operations will be rooted at.
668750
subscriptionType: __Type
751+
752+
# A list of all directives supported by this server.
669753
directives: [__Directive!]!
670754
}
671755
756+
# The fundamental unit of any GraphQL Schema is the type. There are many kinds of
757+
# types in GraphQL as represented by the `__TypeKind` enum.
758+
#
759+
# Depending on the kind of a type, certain fields describe information about that
760+
# type. Scalar types provide no information beyond a name and description, while
761+
# Enum types provide their values. Object and Interface types provide the fields
762+
# they describe. Abstract types, Union and Interface, provide the Object types
763+
# possible at runtime. List and NonNull types compose other types.
672764
type __Type {
673765
kind: __TypeKind!
674766
name: String
@@ -681,17 +773,35 @@ type __Type {
681773
ofType: __Type
682774
}
683775
776+
# An enum describing what kind of type a given `__Type` is.
684777
enum __TypeKind {
778+
779+
# Indicates this type is a scalar.
685780
SCALAR
781+
782+
# Indicates this type is an object. `fields` and `interfaces` are valid fields.
686783
OBJECT
784+
785+
# Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.
687786
INTERFACE
787+
788+
# Indicates this type is a union. `possibleTypes` is a valid field.
688789
UNION
790+
791+
# Indicates this type is an enum. `enumValues` is a valid field.
689792
ENUM
793+
794+
# Indicates this type is an input object. `inputFields` is a valid field.
690795
INPUT_OBJECT
796+
797+
# Indicates this type is a list. `ofType` is a valid field.
691798
LIST
799+
800+
# Indicates this type is a non-null. `ofType` is a valid field.
692801
NON_NULL
693802
}
694803
`;
804+
console.error(output); // eslint-disable-line
695805
expect(output).to.equal(introspectionSchema);
696806
});
697807
});

0 commit comments

Comments
 (0)