Field Arguments

Learn defining field arguments for GraphQL.

We'll cover the following

Defining field arguments

GraphQL documents are made up of fields. The user lists the fields they would like, and the schema uses its definition of those fields to resolve the pieces of data that match. However, the system would be inflexible if it did not allow users to provide additional parameters to clarify exactly what information each field needs to find. A user requesting information about menuItems, for instance, may want to see certain menu items or a certain number of them. For this reason, GraphQL has the concept of field arguments, which are defined as “a way for users to provide input to fields that can be used to parameterize their queries.”

Let’s look at our example application and see how we can extend our Absinthe schema by defining the arguments our API will accept for a field. Then, we can see how we can use those arguments to tailor the result for users.

We’ve already built a field in the schema.ex file of our API, but we could make it more flexible. To do so, we’ll accept user input, which in this case is the list of menu items. Our schema’s menuItems field looks something like this:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy