

Boolean) Skip this field if the argument is true.ĭirectives can be useful to get out of situations where you otherwise would need to do string manipulation to add and remove fields in your query.Boolean) Only include this field in the result if the argument is true.The core GraphQL specification includes exactly two directives, which must be supported by any spec-compliant GraphQL server implementation: A directive can be attached to a field or fragment inclusion, and can affect execution of the query in any way the server desires. We needed to use a new feature in GraphQL called a directive. Try editing the variables above to instead pass true for withFriends, and see how the result changes. Default variables #ĭefault values can also be assigned to the variables in the query by adding the default value after the type declaration. The schema language is explained in detail on the Schema page. To learn more about the syntax for these variable definitions, it's useful to learn the GraphQL schema language. But if the field you are passing the variable into requires a non-null argument, then the variable has to be required as well. In the case above, since there isn't an ! next to the Episode type, it's optional. Variable definitions can be optional or required. Learn more about input object types on the Schema page. So if you want to pass a complex object into a field, you need to know what input type that matches on the server.

It lists all of the variables, prefixed by $, followed by their type, in this case Episode.Īll declared variables must be either scalars, enums, or input object types.

It works just like the argument definitions for a function in a typed language. The variable definitions are the part that looks like ($episode: Episode) in the query above. This is also in general a good practice for denoting which arguments in our query are expected to be dynamic - we should never be doing string interpolation to construct queries from user-supplied values. Now, in our client code, we can simply pass a different variable rather than needing to construct an entirely new query. Pass variableName: value in the separate, transport-specific (usually JSON) variables dictionary.Declare $variableName as one of the variables accepted by the query.Replace the static value in the query with $variableName.When we start working with variables, we need to do three things: Instead, GraphQL has a first-class way to factor dynamic values out of the query, and pass them as a separate dictionary.
GRAPHQL CONDITIONAL FRAGMENT CODE
It wouldn't be a good idea to pass these dynamic arguments directly in the query string, because then our client-side code would need to dynamically manipulate the query string at runtime, and serialize it into a GraphQL-specific format. But in most applications, the arguments to fields will be dynamic: For example, there might be a dropdown that lets you select which Star Wars episode you are interested in, or a search field, or a set of filters. So far, we have been writing all of our arguments inside the query string. In the same way, GraphQL query and mutation names, along with fragment names, can be a useful debugging tool on the server side to identify Think of this just like a function name in your favorite programming language.įor example, in JavaScript we can easily work only with anonymous functions, but when we give a function a name, it's easier to track it down, debug our code,Īnd log when it's called. When something goes wrong (you see errors either in your network logs, or in the logs of your GraphQL server) it is easier to identify a query in your codebase by name instead of trying to decipher the contents. It is only required in multi-operation documents, but its use is encouraged because it is very helpful for debugging and server-side logging. The operation name is a meaningful and explicit name for your operation. The operation type is required unless you're using the query shorthand syntax, in which case you can't supply a name or variable definitions for your operation. The operation type is either query, mutation, or subscription and describes what type of operation you're intending to do.
