8000 Created TaggedInputObject by leoloso · Pull Request #1226 · GatoGraphQL/GatoGraphQL · GitHub
[go: up one dir, main page]

Skip to content

Conversation

leoloso
Copy link
Collaborator
@leoloso leoloso commented Nov 14, 2021

This is a feature currently under consideration to be included in the GraphQL spec: graphql/graphql-spec#733, and already implemented for the GraphQL API for WordPress.

The Tagged Input Object introduces a form of polymorphism for inputs in GraphQL. It defines a list of named members each with an associated type (like the fields in Object types and Input Object types), where exactly one of those members must be provided as an input when resolving the query (or otherwise it returns a validation error).

The benefit is that a single mutation field can be used to tackle different use cases, so we can avoid creating a different mutation field for each use case.

For instance, the field Root.loginUser now receives a field argument credentials, which is a tagged input object allowing to provide several methods to log into the website. Currently it accepts only 1 method, via the named member website (which accepts the usernameOrEmail and password entries), and other methods can be added in the future (such as jwt and applicationPasswords):

mutation {
  loginUser(
    credentials: {
      website: {
        usernameOrEmail: "admin",
        password: "pachonga"
      },
    }
  ) {
    id
    name
  }
}

This way, loginUser can tackle all methods of logging the user, which makes the GraphQL schema leaner and more elegant (otherwise, we would need fields loginUserByWebsiteData, loginUserByJWT and loginUserByApplicationPassword).

@leoloso leoloso merged commit 2000026 into master Nov 14, 2021
@leoloso leoloso deleted the Create-TaggedInputObject branch November 14, 2021 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0