github.com/nathanstitt/genqlient@v0.3.1-0.20211028004951-a2bda3c41ab8/docs/genqlient_directive.graphql (about)

     1  # The quasi-directive @genqlient is used to configure genqlient on a
     2  # query-by-query basis.
     3  #
     4  # The syntax of the directive is just like a GraphQL directive (as defined
     5  # below), except it goes in a comment on the line immediately preceding the
     6  # field.  (This is because GraphQL expects directives in queries to be defined
     7  # by the server, not by the client, so it would reject a real @genqlient
     8  # directive as nonexistent.)
     9  #
    10  # Directives may be applied to fields, arguments, or the entire query or named
    11  # fragment.  Directives on the line preceding the query or a named fragment
    12  # apply to all relevant nodes in the query; other directives apply to all nodes
    13  # on the following line.  (In all cases it's fine for there to be other
    14  # comments in between the directive and the node(s) to which it applies.)  For
    15  # example, in the following query:
    16  #  # @genqlient(n: "a")
    17  #
    18  #  # @genqlient(n: "b")
    19  #  #
    20  #  # Comment describing the query
    21  #  #
    22  #  # @genqlient(n: "c")
    23  #  query MyQuery(arg1: String,
    24  #    # @genqlient(n: "d")
    25  #    arg2: String, arg3: MyInput,
    26  #    arg4: String,
    27  #  ) {
    28  #    # @genqlient(n: "e")
    29  #    field1, field2
    30  #    # @genqlient(n: "f")
    31  #    field3 {
    32  #      field4
    33  #    }
    34  #  }
    35  # the directive "a" is ignored, "b" and "c" apply to all relevant nodes in the
    36  # query, "d" applies to arg2 and arg3, "e" applies to field1 and field2, and
    37  # "f" applies to field3.
    38  #
    39  # Except as noted below, directives on nodes take precedence over ones on the
    40  # entire query (so "d", "e", and "f" take precedence over "b" and "c"), and
    41  # multiple directives on the same node ("b" and "c") must not conflict.  Note
    42  # that directives on nodes do *not* apply to their "children", so "d" does not
    43  # apply to the fields of MyInput, and "f" does not apply to field4.  (But
    44  # directives on operations and fragments do: both "b" and "c" apply to fields
    45  # of MyInput and to field4.)
    46  directive genqlient(
    47  
    48    # If set to a string "MyType.myField", this entire @genqlient directive
    49    # will be treated as if it were applied to the specified field of the
    50    # specified type.  It must be applied to an entire operation or fragment.
    51    #
    52    # This is especially useful for input-type options like omitempty and
    53    # pointer, which are equally meaningful on input-type fields as on arguments,
    54    # but there's no natural syntax to put them on fields.
    55    #
    56    # Note that for input types, unless the type has the "typename" option set,
    57    # all operations and fragments in the same package which use this type should
    58    # have matching directives.  (This is to avoid needing to give them more
    59    # complex type-names.)  This is not currently validated, but will be
    60    # validated in the future (see issue #123).
    61    #
    62    # For example, given the following query:
    63    #  # @genqlient(for: "MyInput.myField", omitempty: true)
    64    #  # @genqlient(for: "MyInput.myOtherField", pointer: true)
    65    #  # @genqlient(for: "MyOutput.id", bind: "path/to/pkg.MyOutputID")
    66    #  query MyQuery($arg: MyInput) { ... }
    67    # genqlient will generate a type
    68    #  type MyInput struct {
    69    #    MyField      <type>  `json:"myField,omitempty"`
    70    #    MyOtherField *<type> `json:"myField"`
    71    #    MyThirdField <type>  `json:"myThirdField"`
    72    #  }
    73    # and use it for the argument to MyQuery, and similarly if `MyOutput.id` is
    74    # ever requested in the response, it will be set to use the given type.
    75    for: String
    76  
    77    # If set, this argument (or input-type field, see "for") will be omitted if
    78    # it has an empty value, defined (the same as in encoding/json) as false, 0,
    79    # a nil pointer, a nil interface value, and any empty array, slice, map, or
    80    # string.
    81    #
    82    # For example, given the following query:
    83    #  # @genqlient(omitempty: true)
    84    #  query MyQuery($arg: String) { ... }
    85    # genqlient will generate a function
    86    #  MyQuery(ctx context.Context, client graphql.Client, arg string) ...
    87    # which will pass {"arg": null} to GraphQL if arg is "", and the actual
    88    # value otherwise.
    89    #
    90    # Only applicable to arguments of nullable types.  Ignored for types with
    91    # custom marshalers (see their documentation in genqlient.yaml for details).
    92    omitempty: Boolean
    93  
    94    # If set, this argument or field will use a pointer type in Go.  Response
    95    # types always use pointers, but otherwise we typically do not.
    96    #
    97    # This can be useful if it's a type you'll need to pass around (and want a
    98    # pointer to save copies) or if you wish to distinguish between the Go
    99    # zero value and null (for nullable fields).
   100    pointer: Boolean
   101  
   102    # If set, this field will use a struct type in Go, even if it's an interface.
   103    #
   104    # This is useful when you have a query like
   105    #  query MyQuery {
   106    #    myInterface { myField }
   107    #  }
   108    # where you are requesting only shared fields of an interface.  By default,
   109    # genqlient still generates an interface type, for consistency.  But this
   110    # isn't necessary: a struct would do just fine since there are no
   111    # type-specific fields.  Setting `struct: true` tells genqlient to do that.
   112    #
   113    # Note that this is only allowed when there are no fragments in play, such
   114    # that all fields are on the interface type.  Note that if you later add a
   115    # fragment, you'll have to remove this option, and the types will change.
   116    struct: Boolean
   117  
   118    # If set, this field's selection must contain a single fragment-spread; we'll
   119    # use the type of that fragment-spread as the type of the field.
   120    #
   121    # For example, given a query like
   122    #  query MyQuery {
   123    #    myField {
   124    #      ...MyFragment
   125    #    }
   126    #  }
   127    # by default genqlient will generate these types:
   128    #  type MyQueryResponse struct {
   129    #    MyField MyQueryMyFieldMyType
   130    #  }
   131    #  type MyQueryMyFieldMyType struct {
   132    #    MyFragment
   133    #  }
   134    # If we instead do:
   135    #  query MyQuery {
   136    #    # @genqlient(flatten: true)
   137    #    myField {
   138    #      ...MyFragment
   139    #    }
   140    #  }
   141    # genqlient will simplify things:
   142    #  type MyQueryResponse struct {
   143    #    MyField MyFragment
   144    #  }
   145    #
   146    # This is only applicable to fields whose selection is a single
   147    # fragment-spread, such that the field-type implements the fragment-type
   148    # (i.e. we can't do this if MyFragment is on one implementation of the type
   149    # of MyField; what if we got back the other type?).
   150    flatten: Boolean
   151  
   152    # If set, this argument or field will use the given Go type instead of a
   153    # genqlient-generated type.
   154    #
   155    # The value should be the fully-qualified type name to use for the field,
   156    # for example:
   157    #  time.Time
   158    #  map[string]interface{}
   159    #  []github.com/you/yourpkg/subpkg.MyType
   160    # Note that the type is the type of the whole field, e.g. if your field in
   161    # GraphQL has type `[DateTime]`, you'd do
   162    #  # @genqlient(bind: "[]time.Time")
   163    # (But you're not required to; if you want to map to some type DateList,
   164    # you can do that, as long as its UnmarshalJSON method can accept a list
   165    # of datetimes.)
   166    #
   167    # Note that the type you bind to must be defined elsewhere in your code.
   168    # If you want genqlient to create the type definition, use "typename"
   169    # instead.
   170    #
   171    # See bindings in genqlient.yaml for more details; this is effectively to a
   172    # local version of that global setting and should be used with similar care.
   173    # If set to "-", overrides any such global setting and uses a
   174    # genqlient-generated type.
   175    bind: String
   176  
   177    # If set, the type of this field will have the given name in Go.
   178    #
   179    # For example, given the following query:
   180    #  # @genqlient(typename: "MyResp")
   181    #  query MyQuery {
   182    #    # @genqlient(typename: "User")
   183    #    user {
   184    #      id
   185    #    }
   186    #  }
   187    # genqlient will generate
   188    #  type Resp struct {
   189    #    User User
   190    #  }
   191    #  type User struct {
   192    #    Id string
   193    #  }
   194    # instead of its usual, more verbose type names.
   195    #
   196    # You may also use "typename" on basic types, and Go will create a
   197    # type definition for that basic type.  For instance:
   198    #  query MyQuery {
   199    #    user {
   200    #      # @genqlient(typename: "NameType")
   201    #      name
   202    #    }
   203    #  }
   204    # will cause gnqlient to generate:
   205    #  type Resp struct {
   206    #    User User
   207    #  }
   208    #  type NameType string
   209    #  type User struct {
   210    #    Name NameType
   211    #  }
   212    # (Compare this to @genqlient(bind: "path/to/pkg.NameType"), which does
   213    # something similar but depends on "NameType" being defined in some
   214    # other package, rather than having genqlient define it for you.)
   215    #
   216    # With great power comes great responsibility: when using typename you'll
   217    # need to avoid comments; genqlient will complain if you use the same
   218    # type-name in multiple places unless they request the exact same fields, or
   219    # if your type-name conflicts with an autogenerated one (again, unless they
   220    # request the exact same fields).  They must even have the fields in the
   221    # same order.  They should also have matching @genqlient directives, although
   222    # this is not currently validated (see issue #123).  Fragments are often
   223    # easier to use (see the discussion of code-sharing in FAQ.md, and the
   224    # "flatten" option above).
   225    #
   226    # Note that unlike most directives, if applied to the entire operation,
   227    # typename affects the overall response type, rather than being propagated
   228    # down to all child fields (which would cause conflicts).
   229    typename: String
   230  
   231  # Multiple genqlient directives are allowed in the same location, as long as
   232  # they don't have conflicting options.
   233  ) repeatable on
   234    # genqlient directives can go almost anywhere, although some options are only
   235    # applicable in certain locations as described above.
   236    | QUERY
   237    | MUTATION
   238    | SUBSCRIPTION
   239    | FIELD
   240    | FRAGMENT_DEFINITION
   241    | VARIABLE_DEFINITION