Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason I am getting back the query name in the GraphQL results?

Using the makeExecutableSchema with the following Query definition:

# Interface for simple presence in front-end.
type AccountType {
    email: Email!
    firstName: String!
    lastName: String!
}

# The Root Query
type Query {
    # Get's the account per ID or with an authToken.
    getAccount(
        email: Email
    )   : AccountType!
}

schema {
    query: Query
}

And the following resolver:

export default {
    Query: {
        async getAccount(_, {email}, { authToken }) {
            /**
             * Authentication
             */
            //const user = security.requireAuth(authToken)

            /**
             * Resolution
             */
            const account = await accounts.find({email})
            if (account.length !== 1) {
                throw new GraphQLError('No account was found with the given email.', GraphQLError.codes.GRAPHQL_NOT_FOUND)
            }
            return account
        }
    }
}

When I query with:

query {
  getAccount(email: "[email protected]") {
    firstName
    lastName
  }
}

I am getting the following result in GraphiQL:

{
  "data": {
    "getAccount": {
      "firstName": "John",
      "lastName": "Doe"
    }
  }
}

So, any reason I am getting this "getAccount" back in the result?

like image 787
Dallas Avatar asked Oct 16 '25 14:10

Dallas


1 Answers

Because getAccount is not a query name. It's just a regular field on the root query type Query.

And having results on the exact same shape as the query is one of the core design principles of GraphQL:

enter image description here Screenshot from http://graphql.org/ site

Query name in GraphQL goes after query keyword:

query myQueryName {
  getAccount(email: "[email protected]") {
    firstName
    lastName
  }
}
like image 115
RomanHotsiy Avatar answered Oct 18 '25 13:10

RomanHotsiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!